1.BeanUtils组件

  1)使用:导入commons-beanutils-1.8.3.jar核心包,日志支持包: commons-logging-1.1.3.jar

      缺少日志的jar文件报错:java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

  2)用法:

    1.对象属性的拷贝

      BeanUtils.copyProperty(admin, "userName", "jack");

      BeanUtils.setProperty(admin, "age", 18);

    2.对象的拷贝

      BeanUtils.copyProperties(newAdmin, admin);

    3.map数据拷到javaBean中      

      【注意:map中的key要与javabean的属性名称一致】

      BeanUtils.populate(adminMap, map);

  3)例子:

public class App {

    //1. 对javabean的基本操作
    @Test
    public void test1() throws Exception {
        
        // a. 基本操作
        Admin admin = new Admin();
//        admin.setUserName("Jack");
//        admin.setPwd("999");
        
        // b. BeanUtils组件实现对象属性的拷贝
        BeanUtils.copyProperty(admin, "userName", "jack");
        BeanUtils.setProperty(admin, "age", 18);
        
        // 总结1: 对于基本数据类型,会自动进行类型转换!
        
        
        // c. 对象的拷贝
        Admin newAdmin = new Admin();
        BeanUtils.copyProperties(newAdmin, admin);
        
        // d. map数据,拷贝到对象中
        Admin adminMap = new Admin();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("userName", "Jerry");
        map.put("age", 29);
        // 注意:map中的key要与javabean的属性名称一致
        BeanUtils.populate(adminMap, map);
        
        // 测试
        System.out.println(adminMap.getUserName());
        System.out.println(adminMap.getAge());
    }
    
    
    //2. 自定义日期类型转换器
    @Test
    public void test2() throws Exception {
        // 模拟表单数据
        String name = "jack";
        String age = "20";
        String birth = "   ";
        
        // 对象
        Admin admin = new Admin();
        
        // 注册日期类型转换器:1, 自定义的方式
        ConvertUtils.register(new Converter() {
            // 转换的内部实现方法,需要重写
            @Override
            public Object convert(Class type, Object value) {
                
                // 判断
                if (type != Date.class) {
                    return null;
                }
                if (value == null || "".equals(value.toString().trim())) {
                    return null;
                }
                
                
                try {
                    // 字符串转换为日期
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    return sdf.parse(value.toString());
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
            }
        },Date.class);
        
        
        
        // 把表单提交的数据,封装到对象中
        BeanUtils.copyProperty(admin, "userName", name);
        BeanUtils.copyProperty(admin, "age", age);
        BeanUtils.copyProperty(admin, "birth", birth);
        
        //------ 测试------
        System.out.println(admin);
    }
    
    //2. 使用提供的日期类型转换器工具类
    @Test
    public void test3() throws Exception {
        // 模拟表单数据
        String name = "jack";
        String age = "20";
        String birth = null;
        
        // 对象
        Admin admin = new Admin();
        
        // 注册日期类型转换器:2, 使用组件提供的转换器工具类
        ConvertUtils.register(new DateLocaleConverter(), Date.class);
                
        // 把表单提交的数据,封装到对象中
        BeanUtils.copyProperty(admin, "userName", name);
        BeanUtils.copyProperty(admin, "age", age);
        BeanUtils.copyProperty(admin, "birth", birth);
        
        //------ 测试------
        System.out.println(admin);
    }
}
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-23
  • 2021-09-28
  • 2023-01-30
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-20
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案