第一步:引入依赖
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-parent</artifactId><version>5.7.17</version></dependency>
第二步:各种用法
①生成随机数
//生成验证码
String s = RandomUtil.randomNumbers(6);
System.out.println("s = " + s);
②两个对象之间信息过滤
//两个对象过滤信息
User user=new User(1L,"zzl",18,"12345678912");
UserDTO userDTO = BeanUtil.copyProperties(user, UserDTO.class);
System.out.println("userDTO = " + userDTO);
③生成UUID
//生成UUID
//生成的UUID是带-的字符串,类似于:a5c8a5e8-df2b-4706-bea4-08d0939410e3
String uuid = IdUtil.randomUUID();
System.out.println("uuid = " + uuid);//生成的是不带-的字符串,类似于:b17f24ff026d40949c85a24f4f375d42
String simpleUUID = IdUtil.simpleUUID();
System.out.println("simpleUUID = " + simpleUUID);
④MD5加密
//md5加密
String str = "123456";
String md5Str = SecureUtil.md5(str);
System.out.println("md5Str = " + md5Str);
⑤序列化(即JSON字符串与对象之间的转换)
//JSON字符串与对象之间的序列化
User user1=new User(1L,"zzl",18,"12345678912");
System.out.println("user1 = " + user1);//对象转换为JSON字符串
String object_json = JSONUtil.parse(user1).toString();
System.out.println("object_json = " + object_json);//JSON字符串转为对象
User user2 = JSONUtil.toBean(object_json, User.class);
System.out.println("user2 = " + user2);
⑥字段检验器
//判断是否为邮箱地址boolean result = Validator.isEmail("zuozewei@hotmail.com");log.info("Validator isEmail:{}", result);//判断是否为手机号码result = Validator.isMobile("18911111111");log.info("Validator isMobile:{}", result);//判断是否为汉字result = Validator.isChinese("你好");log.info("Validator isChinese:{}", result);//判断是否为身份证号码(18位中国)result = Validator.isCitizenId("123456");log.info("Validator isCitizenId:{}", result);//判断是否为URLresult = Validator.isUrl("http://www.7d.com");log.info("Validator isUrl:{}", result);//判断字符串是否为数字result = Validator.isNumber("4444444");System.out.println("result = " + result);
想了解了解更多可以去Hutool官网