JAVABean处理工具类,包括 Beanutils、PropertyUtils、CollectionUtils、ConvertUtils。
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
案例代码:
import lombok.Data;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class Demo {
@Data
public static class User {
private Integer id;
private String name;
private String email;
private String date;
}
public static void main(String[] args) {
Map<String, Object> paramsMap = new HashMap<>();
paramsMap.put("id", 123);
paramsMap.put("name", "张三");
paramsMap.put("email", "demo@163.com");
paramsMap.put("date", new Date());
User user = new User();
try {
org.Apache.commons.beanutils.BeanUtils.populate(user, paramsMap);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
System.out.println("user=>" + user);
}
}
输出:
user=>Demo.User(id=123, name=张三, email=demo@163.com, date=Tue Aug 10 13:14:36 CST 2021)