Bean工具-BeanUtil
# 📚介绍
Hutool针对JavaBean操作提供了非常便捷的封装,其中包括了Bean转Map、Map转Bean、复制属性等实用功能。
# 🍭方法
首先定义一个测试用的JavaBean:
@Data
public class Person {
private String name;
private int age;
}
# Bean和Map转换
# 1. BeanUtil.beanToMap bean转map
此方法会按照JavaBean的getter方法获取属性值,并转换成map。规则为字段名作为key,getter方法返回的值作为value。
Person person = new Person();
person.setName("测试A11");
person.setAge(24);
// {name=测试A11, age=24}
Map<String, Object> map = BeanUtil.beanToMap(person);
如果只是需要部分属性,可以使用BeanUtil.beanToMap(Object bean, String... keys)方法。
// {name=测试A11}
Map<String, Object> map = BeanUtil.beanToMap(person, "name");
如果想自定义是否转换为下划线模式或忽略null值,可以使用BeanUtil.beanToMap(Object bean, boolean isToUnderlineCase,boolean ignoreNullValue)方法。
Person person = new Person();
person.setName(null);
person.setAge(24);
// {age=24}
Map<String, Object> map = BeanUtil.beanToMap(person, false, true);
注意 如果Bean中有静态字段,则在转换时会被忽略。 如果Bean中的字段未提供getter方法,则在转换时会被忽略。 如果Bean中字段与Getter不匹配,则在转换时会被忽略。
# 2. BeanUtil.toBean Bean或Map转Bean
# Bean检查
# 复制属性
# 字段值编辑
# Bean数据填充
# BeanPath(Bean路径表达式)
# 常用方法
toBean对象或Map转BeanbeanToMap将bean的部分属性转换成mapcopyProperties复制Bean对象属性copyToList复制集合中的Bean属性
# 1. BeanUtil.toBean bean转map
public void toBeanTest() {
final SubPerson person = new SubPerson();
person.setAge(14);
person.setOpenid("11213232");
person.setName("测试A11");
person.setSubName("sub名字");
final Map<?, ?> map = BeanUtil.toBean(person, Map.class);
Assertions.assertEquals("测试A11", map.get("name"));
Assertions.assertEquals(14, map.get("age"));
Assertions.assertEquals("11213232", map.get("openid"));
// static属性应被忽略
Assertions.assertFalse(map.containsKey("SUBNAME"));
}
# 2. BeanUtil.toBean map转bean
public void mapToBeanIgnoreCaseTest() {
final HashMap<String, Object> map = MapUtil.newHashMap();
map.put("Name", "Joe");
map.put("aGe", 12);
final Person person = BeanUtil.toBean(map, Person.class, CopyOptions.of().setIgnoreCase(true));
Assertions.assertEquals("Joe", person.getName());
Assertions.assertEquals(12, person.getAge());
}
# 3. BeanUtil.beanToMap
public void beanToMapTest() {
final SubPerson person = new SubPerson();
person.setAge(14);
person.setOpenid("11213232");
person.setName("测试A11");
person.setSubName("sub名字");
final Map<String, Object> map = BeanUtil.beanToMap(person);
Assertions.assertEquals("测试A11", map.get("name"));
Assertions.assertEquals(14, map.get("age"));
Assertions.assertEquals("11213232", map.get("openid"));
// static属性应被忽略
Assertions.assertFalse(map.containsKey("SUBNAME"));
}
# 4. BeanUtil.copyToList
public void copyListTest() {
final Student student = new Student();
student.setName("张三");
student.setAge(123);
student.setNo(3158L);
final Student student2 = new Student();
student2.setName("李四");
student2.setAge(125);
student2.setNo(8848L);
final List<Student> studentList = ListUtil.view(student, student2);
final List<Person> people = BeanUtil.copyToList(studentList, Person.class);
Assertions.assertEquals(studentList.size(), people.size());
for (int i = 0; i < studentList.size(); i++) {
Assertions.assertEquals(studentList.get(i).getName(), people.get(i).getName());
Assertions.assertEquals(studentList.get(i).getAge(), people.get(i).getAge());
}
}
# 5. BeanUtil.copyProperties
public void copyPropertiesTest() {
final SubPerson person = new SubPerson();
person.setAge(14);
person.setOpenid("11213232");
person.setName("测试A11");
person.setSubName("sub名字");
final SubPerson person1 = BeanUtil.copyProperties(person, SubPerson.class);
Assertions.assertEquals(14, person1.getAge());
Assertions.assertEquals("11213232", person1.getOpenid());
Assertions.assertEquals("测试A11", person1.getName());
Assertions.assertEquals("sub名字", person1.getSubName());
}
# 更多方法
createDynaBean创建动态BeanfindEditor查找类型转换器getBeanDesc获取Bean描述信息descForEach遍历Bean的属性getPropertyDescriptors获得Bean字段描述数组getPropertyDescriptorMap获得字段名和字段描述MapgetPropertyDescriptor获得Bean类属性描述,大小写敏感getFieldValue获得字段值,通过反射直接获得字段值,并不调用getXXX方法setFieldValue设置字段值,通过反射设置字段值,并不调用setXXX方法getProperty获取Bean中的属性值setProperty设置Bean中的属性值getBeanDesc获取Bean描述信息fillBean填充Bean的核心方法fillBeanWithMap使用Map填充Bean对象isMatchName给定的Bean的类名是否匹配指定类名字符串edit编辑Bean的字段,static字段不会处理trimStrFields把Bean里面的String属性做trim操作。此方法直接对传入的Bean做修改isEmpty判断Bean是否为空对象,空对象表示本身为null或者所有属性都为nullisNotEmpty判断Bean是否为非空对象,非空对象表示本身不为null或者含有非null属性的对象isReadableBean判断是否为可读的Bean对象isWritableBean判断是否为可写Bean对象hasSetter判断是否有Setter方法hasGetter判断是否有Getter方法hasPublicField指定类中是否有public类型字段hasNullField判断Bean是否包含值为null的属性hasEmptyField判断Bean是否包含值为null的属性,或当字段为CharSequence时,是否为isEmpty(null或"")checkBean遍历Bean的字段并断言检查字段getFieldName获取Getter或Setter方法名对应的字段名称