廢話少說,show code:
public Map<String,Object> pojo2Map(ReportFloodControlDrainage pojo){
Map<String,Object> res = new HashMap<>();
Map<String, Object> source = BeanUtil.beanToMap(pojo);
source.forEach((paramKey,paramVal) ->{
res.put(StrUtil.toUnderlineCase(paramKey),paramVal);
});
return res;
}
效果如下:
實現原理:
Hutool 是一個 Java 工具庫,提供了許多方便的方法和類,包括駝峰字符串與下劃線字符串之間的轉換。如果你想要將一個駝峰字符串轉換為下劃線字符串,可以使用 Hutool 提供的方法來完成。
如果你想要將一個 POJO 對象轉換為 Map 對象,可以使用 Hutool 提供的方法來完成。
你可以使用 BeanUtil.beanToMap() 方法來實現 POJO 對象轉換為 Map 對象。
你可以使用 StrUtil.toUnderlineCase() 方法來實現駝峰字符串轉換為下劃線字符串。