BeanUtils 是 Apache commons组件的成员之一,主要用于简化JavaBean封装数据的操作。它可以给JavaBean封装成为一个字符串数据,也可以将一个表单提交的所有数据封装到JavaBean中。
BeanUtils常用的两个工具类
- BeanUtils: 用于封装数据
- ConvertUtils: 用于处理类型转换
实体类
public class User {
private String username;
private String password;
private int age;
private Date birthday;
// 省略构造方法
// 省略set、get方法
// 省略toString方法
}
封装表单数据,模拟使用request.getParameterMap()
@Test
public void demo01() throws Exception{
//将map数据封装给user
Map<String,String[]> properties = new HashMap<String,String[]>();
properties.put("username", new String[]{"jack"});
properties.put("password", new String[]{"1234"});
properties.put("age", new String[]{"18"});
User user = new User();
// 封装数据
BeanUtils.populate(user, properties);
System.out.println(user);
}
封装表单数据,使用类型转换器
@Test
public void demo02() throws Exception{
//转换器
Map<String,String[]> properties = new HashMap<String,String[]>();
properties.put("username", new String[]{"jack"});
properties.put("password", new String[]{"1234"});
properties.put("age", new String[]{"18"});
properties.put("birthday", new String[]{"2012-12-21"});
User user = new User();
//1 创建BeanUtils提供时间转换器
DateConverter dateConverter = new DateConverter();
//2 设置需要转换的格式
dateConverter.setPattern("yyyy-MM-dd");
//3 注册转换器
ConvertUtils.register(dateConverter, java.util.Date.class);
//4 封装数据
BeanUtils.populate(user, properties);
System.out.println(user);
工具类
编写工具类
public class MyBeanUtils {
/**
* 将数据封装给JavaBean,支持时间类型转换
* @param user
* @param properties
*/
public static void populate(Object user, Map<String,String[]> properties){
try {
//1 创建BeanUtils提供时间转换器
DateConverter dateConverter = new DateConverter();
//2 设置需要转换的格式
dateConverter.setPatterns(new String[]{"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"});
//3 注册转换器
ConvertUtils.register(dateConverter, java.util.Date.class);
//4 封装数据
BeanUtils.populate(user, properties);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
使用工具类
/**
* 使用工具类
*/
@Test
public void demo03() throws Exception{
Map<String,String[]> properties = new HashMap<String,String[]>();
properties.put("username", new String[]{"jack"});
properties.put("password", new String[]{"1234"});
properties.put("age", new String[]{"18"});
properties.put("birthday", new String[]{"2012-12-21"});
User user = new User();
// 封装数据
MyBeanUtils.populate(user, properties);
System.out.println(user);
}
工具类_反射
编写
public class MyBeanUtils2 {
/**
* 高级封装,不需要new javabean
* @param beanClass
* @param properties
* @return
*/
public static <T> T populate(Class<T> beanClass, Map<String,String[]> properties){
try {
//1 使用反射创建实例
T bean = beanClass.newInstance();
//2.1 创建BeanUtils提供时间转换器
DateConverter dateConverter = new DateConverter();
//2.2 设置需要转换的格式
dateConverter.setPatterns(new String[]{"yyyy-MM-dd","yyyy-MM-dd HH:mm:ss"});
//2.3 注册转换器
ConvertUtils.register(dateConverter, java.util.Date.class);
//3 封装数据
BeanUtils.populate(bean, properties);
return bean;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
使用
/**
* 使用工具类MyBeanUtils2
*/
@Test
public void demo04() throws Exception{
Map<String,String[]> properties = new HashMap<String,String[]>();
properties.put("username", new String[]{"jack"});
properties.put("password", new String[]{"1234"});
properties.put("age", new String[]{"18"});
properties.put("birthday", new String[]{"2012-12-21"});
// 封装数据
User user = MyBeanUtils2.populate(User.class, properties);
System.out.println(user);
}