SpringBoot Hutool各種用法示例小結(jié)
更新時間:2026年01月05日 11:09:19 作者:快看小腳魚
文章介紹了Hutool庫的使用,包括生成隨機數(shù)、對象信息過濾、生成UUID、MD5加密、JSON序列化和字段檢驗等功能,并提供了詳細的用法示例,感興趣的朋友跟隨小編一起看看吧
第一步:引入依賴
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-parent</artifactId>
<version>5.7.17</version>
</dependency>第二步:各種用法
①生成隨機數(shù)
//生成驗證碼
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字符串與對象之間的轉(zhuǎn)換)
//JSON字符串與對象之間的序列化
User user1=new User(1L,"zzl",18,"12345678912");
System.out.println("user1 = " + user1);
//對象轉(zhuǎn)換為JSON字符串
String object_json = JSONUtil.parse(user1).toString();
System.out.println("object_json = " + object_json);
//JSON字符串轉(zhuǎn)為對象
User user2 = JSONUtil.toBean(object_json, User.class);
System.out.println("user2 = " + user2);
//List轉(zhuǎn)換為JSON字符串
String s = JSONUtil.toJsonStr(list);
//JSON字符串轉(zhuǎn)為List
List<ShopType> shopTypes = JSONUtil.toList(shop_type_JSONS, ShopType.class);
System.out.println("從Redis中查到的數(shù)據(jù):shopTypes = " + shopTypes); 
⑥字段檢驗器
//判斷是否為郵箱地址
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);
//判斷是否為URL
result = Validator.isUrl("http://www.7d.com");
log.info("Validator isUrl:{}", result);
//判斷字符串是否為數(shù)字
result = Validator.isNumber("4444444");
System.out.println("result = " + result);想了解了解更多可以去Hutool官網(wǎng)
到此這篇關于SpringBoot Hutool各種用法示例小結(jié)的文章就介紹到這了,更多相關springboot hutool用法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
- SpringBoot3整合Hutool-captcha實現(xiàn)圖形驗證碼
- 基于SpringBoot和Hutool工具包實現(xiàn)驗證碼的案例
- SpringBoot使用hutool-captcha實現(xiàn)驗證碼生成與驗證
- SpringBoot整合Hutool實現(xiàn)文件上傳的使用示例
- Springboot+Hutool自定義注解實現(xiàn)數(shù)據(jù)脫敏
- SpringBoot 項目使用hutool 工具進行 http 接口調(diào)用的處理方法
- springboot?vue接口測試HutoolUtil?TreeUtil處理樹形結(jié)構(gòu)
- springboot layui hutool Excel導入的實現(xiàn)
- SpringBoot+Hutool+thymeleaf完成導出Excel的實現(xiàn)方法
- springboot+hutool批量生成二維碼壓縮導出功能
相關文章
java集合PriorityQueue優(yōu)先級隊列方法實例
這篇文章主要為大家介紹了java集合PriorityQueue優(yōu)先級隊列方法實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
使用Spring?Batch實現(xiàn)大數(shù)據(jù)處理的操作方法
通過使用Spring?Batch,我們可以高效地處理大規(guī)模數(shù)據(jù),本文介紹了如何配置和實現(xiàn)一個基本的Spring?Batch作業(yè),包括讀取數(shù)據(jù)、處理數(shù)據(jù)和寫入數(shù)據(jù)的全過程,感興趣的朋友跟隨小編一起看看吧2024-07-07
如何在SpringBoot項目中集成SpringSecurity進行權(quán)限管理
在本文中,我們將討論如何在Spring?Boot項目中集成權(quán)限管理,我們將使用Spring?Security框架,這是一個專門用于實現(xiàn)安全性功能的框架,包括認證和授權(quán),需要的朋友可以參考下2023-07-07

