• BeanUtils的使用


    Beanutils工具在使用时几乎只用到以下几个方法,其中一个方法通常情况下都是使用匿名内部类,用来注册日期类型转换器,以将字符串类型的数据转换成指定格式的日期类型。

    • BeanUtils.setProperty(bean, name, value):为指定bean实例的属性设值,等同于bean.setXXX()方法;其中bean是指你将要设置的对象,name指的是将要设置的属性(写成”属性名”),value(想要设置的属性值);
    • BeanUtils.copyProperties(bean, name, value):与上边的setProperty方法功能相同;
    • ConvertUtils.register(Converter converter , Class clazz):类型转换器注册方法,当需要将String数据转换成引用数据类型(自定义数据类型时,例如Date类型),需要使用此方法实现转换;
    • BeanUtils.populate(bean,Map):将Map集合中的数据注入到JavaBean的属性中去,其中Map中的key必须与目标对象中的属性名相同,否则不能实现拷贝;
    • BeanUtils.copyProperties(newObject,oldObject):实现对象之间的拷贝。

    使用方法展示:

    1.首先定义一个Bean对象

     1 package bean;
     2 
     3 public class UserBean {
     4     private int userId;
     5     private String userName;
     6     private String userPass;
     7 
     8     public int getUserId() {
     9         return userId;
    10     }
    11 
    12     public void setUserId(int userId) {
    13         this.userId = userId;
    14     }
    15 
    16     public String getUserName() {
    17         return userName;
    18     }
    19 
    20     public void setUserName(String userName) {
    21         this.userName = userName;
    22     }
    23 
    24     public String getUserPass() {
    25         return userPass;
    26     }
    27 
    28     public void setUserPass(String userPass) {
    29         this.userPass = userPass;
    30     }
    31 
    32 }
    View Code

    2.BeanUtils.populate(bean,Map)方法注入数据演示

     1 import java.lang.reflect.InvocationTargetException;
     2 import java.util.HashMap;
     3 import java.util.Map;
     4 
     5 import org.apache.commons.beanutils.BeanUtils;
     6 import org.apache.commons.beanutils.PropertyUtils;
     7 
     8 public class BeanUtil {
     9 
    10     public static void main(String[] args) throws NoSuchMethodException {
    11         UserBean user = new UserBean();
    12         Map<String, Object> prop = new HashMap<String, Object>();
    13         prop.put("userId", "8");
    14         prop.put("userName", "mapProp");
    15         prop.put("userPass", "root");
    16         try {
    17             // 将Map集合中的数据注入到JavaBean的属性中去,其中Map中的key必须与目标对象中的属性名相同,否则不能实现拷贝;
    18             BeanUtils.populate(user, prop);
    19             // 输出结果,观察是否成功向user中注入数据.
    20             System.out.println(user.getUserId() + ":" + user.getUserName() + ":" + user.getUserPass());
    21         } catch (IllegalAccessException | InvocationTargetException e) {
    22             e.printStackTrace();
    23         }
    24     }
    25 }
    View Code

    输出结果

    3.BeanUtils.setProperty(bean, name, value)方法注入数据演示

     1 import java.lang.reflect.InvocationTargetException;
     2 import java.util.HashMap;
     3 import java.util.Map;
     4 
     5 import org.apache.commons.beanutils.BeanUtils;
     6 import org.apache.commons.beanutils.PropertyUtils;
     7 
     8 public class BeanUtil {
     9 
    10     public static void main(String[] args) throws NoSuchMethodException {
    11         UserBean user = new UserBean();
    12         try {
    13         BeanUtils.setProperty(user, "userName", "anmin"); 
    14         BeanUtils.setProperty(user, "userId", 5); 
    15         BeanUtils.setProperty(user, "userPass", "password");
    16         // 用BeanUtils.getProperty获得数据
    17         System.out.println(BeanUtils.getProperty(user, "userId") + ":" 
    18                 + BeanUtils.getProperty(user, "userName") + ":" + BeanUtils.getProperty(user, "userPass") );
    19         } catch (IllegalAccessException | InvocationTargetException e) {
    20             e.printStackTrace();
    21         }
    22     }
    23 }
    View Code

    输出结果

    另一个工具PropertyUtilsBeanUtils的方法非常类似,不同之处在于BeanUtils  getProperty()方法的返回值通常是String类型,而PropertyUtils getProperty()方法返回的是bean中数据本来的类型,调用setProperty()方法时,赋的值也需要与bean中的数据类型相匹配,不然会报错。如下例中所示:

    BeanUtils

    import java.lang.reflect.InvocationTargetException;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.apache.commons.beanutils.BeanUtils;
    import org.apache.commons.beanutils.PropertyUtils;
    
    public class BeanUtil {
    
        public static void main(String[] args) throws NoSuchMethodException {
            UserBean user = new UserBean();
            try {
                //BeanUtils以字符串的形式set userId的值  不会出错
                BeanUtils.setProperty(user, "userId", "5");
                System.out.println(BeanUtils.getProperty(user, "userId"));
            } catch (IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
    View Code

    PropertyUtils

    import java.lang.reflect.InvocationTargetException;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.apache.commons.beanutils.BeanUtils;
    import org.apache.commons.beanutils.PropertyUtils;
    
    public class BeanUtil {
    
        public static void main(String[] args) throws NoSuchMethodException {
            UserBean user = new UserBean();
            try {
                //PropertyUtils以字符串的形式set userId的值  报错
                PropertyUtils.setProperty(user, "userId", "9");
                System.out.println(PropertyUtils.getProperty(user, "userId"));
            } catch (IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
    View Code

    修改之后:

     1 import java.lang.reflect.InvocationTargetException;
     2 import java.util.HashMap;
     3 import java.util.Map;
     4 
     5 import org.apache.commons.beanutils.BeanUtils;
     6 import org.apache.commons.beanutils.PropertyUtils;
     7 
     8 public class BeanUtil {
     9 
    10     public static void main(String[] args) throws NoSuchMethodException {
    11         UserBean user = new UserBean();
    12         try {
    13             //PropertyUtils以字符串的形式set userId的值  报错  必须按其原本的数据类型为其赋值
    14             PropertyUtils.setProperty(user, "userId", 9);
    15             System.out.println(PropertyUtils.getProperty(user, "userId"));
    16         } catch (IllegalAccessException | InvocationTargetException e) {
    17             e.printStackTrace();
    18         }
    19     }
    20 }
    View Code

  • 相关阅读:
    bootstrap-table对前台页面表格的支持
    解决拦截器对ajax请求的的拦截
    jQuery获取鼠标事件源(万能)
    HtmlAgilityPack 处理通配的contains
    【转】XPath的学习
    在IIS服务器上部署svg/woff/woff2字体
    【转】万网域名查询接口(API)的说明
    html5和css3的常用参考网
    【转】Xml序列化
    C#序列化s实体类成Xml,去除空格、换行符以及命名空间
  • 原文地址:https://www.cnblogs.com/ustc-anmin/p/10215669.html
Copyright © 2020-2023  润新知