zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    c#报错信息 显示详细行号
    分析器错误消息: “webgroup.admin.index”不扩展类“System.Web.UI.Page”,因此此处不允许
    ASP.NET下AJAX.AJAXMETHOD使用方法,微软ajax 异步 同步的修改方法
    Jquery实际应用,判断radio,selelct,checkbox是否选中及选中的值,jquery如果获取多个重复name的input的值
    utf8编码的mysql数据库 按照 中文来对 名称进行排序
    MVC新手教程二:Action使用非默认视图,强类型和强类型视图
    gcc 编译多个源文件
    嵌入式学习路径
    C语言基础知识:printf的输出格式
    C语言的数据类型
  • 原文地址:https://www.cnblogs.com/ustc-anmin/p/10215669.html
Copyright © 2011-2022 走看看