1.BeanUtil.fillBeanWithMap使用Map填充bean
HashMap<String, Object> map = CollUtil.newHashMap();
map.put("name", "Joe");
map.put("age", 12);
map.put("openId", "DFDFSDFWERWER");
SubPerson person = BeanUtil.fillBeanWithMap(map, new SubPerson(), false);
2.BeanUtil.beanToMap方法則是將一個Bean對象轉為Map對象。
SubPerson person = new SubPerson();
person.setAge(14);
person.setOpenid("11213232");
person.setName("測試A11");
person.setSubName("sub名字");
Map<String, Object> map = BeanUtil.beanToMap(person);
3.BeanUtil.copyProperties方法同樣提供一個CopyOptions參數用於自定義屬性復制
SubPerson p1 = new SubPerson();
p1.setSlow(true);
p1.setName("測試");
p1.setSubName("sub測試");
Map<String, Object> map = MapUtil.newHashMap();
BeanUtil.copyProperties(p1, map);
4.Alias,通過此注解可以給Bean的字段設置別名。
// Lombok注解
@Getter
@Setter
public static class SubPersonWithAlias {
@Alias("aliasSubName")
private String subName;
private Boolean slow;
