首页 > 代码库 > 使用beanUtils操纵javabean

使用beanUtils操纵javabean

Sun公司的内省API过于繁琐,所以Apache组织结合很多实际开发中的应用场景开发了一套简单、易用的API操作Bean的属性——BeanUtils,在Beanutil中可以直接进行类型的自动转换。

BeanUtil工具包下载:

  1,登录http://commons.apache.org/beanutils/

  2,  点击Download

  3, 点击commons-beanutils-1.9.2-bin.zip(commons-logging-1.1.3-src)进行下载就OK了

使用BeanUtil

在项目中导入commons-beanutils-1.9.2.jar包即可(PS:把此jar包复制到项目的lib文件夹,右击包,选build path==>add to build path 显示奶瓶即可)

另外:commons-beanutils-1.9.2.jar 要与 commons-logging-1.1.3.Jar共用。

  1 import java.lang.reflect.InvocationTargetException;  2 import java.text.ParseException;  3 import java.text.SimpleDateFormat;  4 import java.util.Date;  5 import java.util.HashMap;  6 import java.util.Map;  7   8 import org.apache.commons.beanutils.BeanUtils;  9 import org.apache.commons.beanutils.ConversionException; 10 import org.apache.commons.beanutils.ConvertUtils; 11 import org.apache.commons.beanutils.Converter; 12 import org.apache.commons.beanutils.locale.converters.DateLocaleConverter; 13 import org.junit.Test; 14  15  16  17 //使用beanUtils操纵bean属性(第三方) 18 public class Demo { 19  20     /** 21      *  22      * @throws Exception  23      * 24      */ 25     @Test 26     public void test1() throws  Exception{ 27          28         Person p = new Person(); 29          30         BeanUtils.setProperty(p, "name", "zero"); 31          32         System.out.println(p.getName()); 33          34     } 35      36     @Test 37     public void test2() throws  Exception{ 38          39         String name = "aaaa"; 40         String password = "123"; 41         String age = "34"; 42          43         Person p = new Person(); 44          45         BeanUtils.setProperty(p, "name", name);//只支持8种基本数据类型 46         BeanUtils.setProperty(p, "password", password); 47         BeanUtils.setProperty(p, "age", age); 48          49         System.out.println(p.getName()+"*"+p.getPassword()+"*"+p.getAge()); 50          51     } 52      53     @Test 54     public void test3() throws  Exception{ 55          56         String name = "aaaa"; 57         String password = "123"; 58         String age = "34"; 59         String birthday = "1900-1-1"; 60          61         //为了让日期赋到bean的birthday属性上,给beanUtils注册一个日期转换 62         ConvertUtils.register(new Converter(){ 63  64             @Override 65             public Object convert(Class type, Object value) { 66                  67                 if(value=http://www.mamicode.com/=null){ 68                     return null; 69                 } 70                 if(!(value instanceof String)){ 71                     throw new ConversionException("only support String"); 72                 } 73                  74                 String str = (String) value; 75                  76                 if(str.trim().equals("")){ 77                     return null; 78                 } 79                  80                 SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd"); 81                  82                 try { 83                     return df.parse(str); 84                 } catch (ParseException e) { 85                     throw new RuntimeException(e); 86                 } 87                  88             } 89              90         },Date.class); 91          92         Person p = new Person(); 93          94         BeanUtils.setProperty(p, "name", name);//只支持8种基本数据类型 95         BeanUtils.setProperty(p, "password", password); 96         BeanUtils.setProperty(p, "age", age); 97         BeanUtils.setProperty(p, "birthday", birthday); 98          99         System.out.println(p.getName()+"*"+p.getPassword()+"*"+p.getAge()+"*"+p.getBirthday());100         101     }102     103     @Test104     public void test4() throws  Exception{105         106         String name = "aaaa";107         String password = "123";108         String age = "34";109         String birthday = "1998-1-1";110         111         ConvertUtils.register(new DateLocaleConverter(), Date.class);112         113         Person p = new Person();114         115         BeanUtils.setProperty(p, "name", name);//只支持8种基本数据类型116         BeanUtils.setProperty(p, "password", password);117         BeanUtils.setProperty(p, "age", age);118         BeanUtils.setProperty(p, "birthday", birthday);119         120         Date date = p.getBirthday();121         122         System.out.println(p.getName()+"*"+p.getPassword()+"*"+p.getAge()+"*"+date.toLocaleString());123         124     }125     126     @Test127     public void test5() throws Exception{128         129         Map map = new HashMap();130         map.put("name", "zero");131         map.put("password", "521212");132         map.put("age", "33");133         map.put("birthday", "1998-1-1");134         135         ConvertUtils.register(new DateLocaleConverter(), Date.class);136  137         Person p = new Person();138         139         BeanUtils.populate(p, map);//用map集合的值,填充bean的属性值140         141         System.out.println(p.getName()+"*"+p.getPassword()+"*"+p.getAge()+"*"+p.getBirthday());142 143     }144     145 }
View Code
 1 import java.util.Date; 2  3  4 public class Person {// javabean 5  6     private String name; 7     private String password; 8     private int age; 9     private Date birthday;10 11     public Date getBirthday() {12         return birthday;13     }14 15     public void setBirthday(Date birthday) {16         this.birthday = birthday;17     }18 19     public String getName() {20         return name;21     }22 23     public void setName(String name) {24         this.name = name;25     }26 27     public String getPassword() {28         return password;29     }30 31     public void setPassword(String password) {32         this.password = password;33     }34 35     public int getAge() {36         return age;37     }38 39     public void setAge(int age) {40         this.age = age;41     }42     43     public String getAb(){44         return null;45     }46 47 48 }
View Code