就是方便我们为对象设置属性,不需要一个一个的set,提高编程效率。
1:准备测试beanpublic class MyDataBinderBean { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return JSON.toJSONString(this); } }2:main
public class MyMain { public static void main(String[] args) { MyDataBinderBean bean = new MyDataBinderBean(); DataBinder dataBinder = new DataBinder(bean); // 设置无效 dataBinder.setIgnoreInvalidFields(true); dataBinder.setIgnoreUnknownFields(true); Mapproperties = new HashMap<>(); properties.put("name", "张三"); properties.put("age", 60); MutablePropertyValues propertyValues = new MutablePropertyValues(properties); dataBinder.bind(propertyValues); System.out.println(bean); } }
运行:
{"age":60,"name":"张三"}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)