1 准备工作
<dependency>
<groupId></groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.5</version>
<scope>compile</scope>
</dependency>
@Data
public class User {
private String userId;
private String userName;
private int age;
}
2 beanToMap 使用,bean转成map
import ;
import ;
import ;
import ;
public class Test_2 {
public static void main(String[] args) {
// 创建用户对象
User user = new User();
("Tom");
(25);
// 最简单的bean转map,忽略null值
Map<String, Object> map_1 = (user);
("map_1 = " + map_1);
();
// isToUnderlineCase 表示是否将字段名转换为下划线命名法,ignoreNullValue 表示是否忽略null值
Map<String, Object> map_2 = (user, true, true);
("map_2 = " + map_2);
();
// 自定义key编辑器,将key转为全小写并添加长度后缀
Map<String, Object> map_3 = (user, new HashMap<>(), true, key -> () + "_" + ());
("map_3 = " + map_3);
();
// targetMap:表示要添加到的map,也就是可以自己传输一个map。
// 然后再将封装后的map键值对一一封装到里面返回。
// 将user对象的属性映射到targetMap中,同时应用上述转换和null值忽略规则
Map<String, Object> targetMap = new HashMap<>();
("address","china");
Map<String, Object> map_4 = (user, targetMap, true, false);
("map_4 = " + map_4);
();
// copyOptions:通过这个参数可以自定义任何转换规则,如“忽略某字段”、“设置editor等”
CopyOptions copyOptions = ().setIgnoreProperties("age").setFieldNameEditor(key-> ());
Map<String, Object> map_5 = (user, new HashMap<>(), copyOptions);
("map_5 = " + map_5);
();
}
}
效果如下
map_1 = {userId=null, userName=Tom, age=25}
map_2 = {user_name=Tom, age=25}
map_3 = {age_3=25, username_8=Tom}
map_4 = {address=china, user_id=null, user_name=Tom, age=25}
map_5 = {USERID=null, USERNAME=Tom}
2 toBean,将map转成bean
import ;
import ;
import ;
import ;
public class Test_3 {
public static void main(String[] args) {
// 创建一个Map对象,包含User对象的部分属性
Map<String, Object> map = new HashMap<>();
("userName", "Jerry");
("userId",1);
("age", 50);
// 使用默认方式将map转换为User对象,键必须与User类属性名一致
User userDefault = (map, );
("userDefault = " + userDefault);
();
// 使用CopyOptions自定义转换规则
CopyOptions options = ();
("age");
User userCustom = (map, , options);
("userCustom = " + userCustom);
}
}
效果如下
userDefault = User(userId=1, userName=Jerry, age=50)
userCustom = User(userId=1, userName=Jerry, age=0)
3 copyProperties ,拷贝属性
import ;
import ;
public class Test_4 {
public static void main(String[] args) {
// 创建源对象
User source = new User();
("5");
("Alice");
(30);
// 创建目标对象
User target = new User();
// 忽略年龄属性的复制
(source, target, "age");
("target = " + target);
// 或者使用CopyOptions忽略多个属性
CopyOptions options = ().setIgnoreProperties("age", "otherProperty");
(source, target, options);
("target = " + target);
}
}
效果如下
target = User(userId=5, userName=Alice, age=0)
target = User(userId=5, userName=Alice, age=0)
4 fillBeanWithMap,根据map填充生成bean
import ;
import ;
import ;
import ;
public class Test_5 {
public static void main(String[] args) {
// 同样创建一个Map对象和User对象
Map<String, Object> dataMap = new HashMap<>();
("userName", "Jane Doe"); // 键与User类属性名不完全匹配
("age", 25);
("userId","10");
User user = (dataMap, new User(), false);
("user = " + user);
();
// 创建一个CopyOptions实例以忽略'age'属性,并自定义键映射
CopyOptions options = ()
.setIgnoreProperties("age");
// 使用带有CopyOptions的fillBeanWithMap方法填充User对象
User user1 = (dataMap, new User(), options);
("user1 = " + user1);
}
}
效果如下
user = User(userId=10, userName=Jane Doe, age=25)
user1 = User(userId=10, userName=Jane Doe, age=0)