使用beanUtils操纵bean的属性
注意:使用beanUtils操纵bean的属性时需要使用到的包有:commons-beanutils-1.8.3.jar commons-logging-1.1.1.jar
创建Demo1类来操作Person中的benan
View Code Person
1 package com.zyw.beanutils; 2 3 import java.util.Date; 4 5 public class Person { 6 private String name; 7 private String password; 8 private int age; 9 private Date birthday; 10 public Date getBirthday() { 11 return birthday; 12 } 13 public void setBirthday(Date birthday) { 14 this.birthday = birthday; 15 } 16 public String getName() { 17 return name; 18 } 19 public String getPassword() { 20 return password; 21 } 22 public int getAge() { 23 return age; 24 } 25 public void setName(String name) { 26 this.name = name; 27 } 28 public void setPassword(String password) { 29 this.password = password; 30 } 31 public void setAge(int age) { 32 this.age = age; 33 } 34 35 }
Demo1
View Code Demo1
1 package com.zyw.beanutils; 2 3 import java.lang.reflect.InvocationTargetException; 4 import java.text.ParseException; 5 import java.text.SimpleDateFormat; 6 import java.util.Date; 7 import java.util.HashMap; 8 import java.util.Locale; 9 import java.util.Map; 10 11 import org.apache.commons.beanutils.BeanUtils; 12 import org.apache.commons.beanutils.ConversionException; 13 import org.apache.commons.beanutils.ConvertUtils; 14 import org.apache.commons.beanutils.Converter; 15 import org.apache.commons.beanutils.locale.converters.DateLocaleConverter; 16 import org.junit.Test; 17 18 //使用beanUtils操纵bean的属性 ( 第三方) 19 public class Demo1 { 20 @Test 21 public void test1() throws Exception{ 22 Person p=new Person(); 23 BeanUtils.setProperty(p, "age", 456); 24 System.out.println(p.getAge());//456 25 } 26 @Test 27 public void test2() throws Exception{ 28 String name="aaaa"; 29 String age="123"; 30 String password="pw"; 31 32 Person p=new Person(); 33 //支持8种基本类型自动转换 34 BeanUtils.setProperty(p, "name", name); 35 BeanUtils.setProperty(p, "age", age); 36 BeanUtils.setProperty(p, "password", password); 37 38 System.out.println(p.getName());//aaaa 39 System.out.println(p.getAge());//123 40 System.out.println(p.getPassword());//pw 41 42 } 43 @Test 44 public void test3() throws Exception{ 45 46 String birthday="1983-12-1"; 47 48 //为了让日期赋值到bean的birthday属性上,给beanUtils注册一个日期转换器 49 //ConvertUtils.register(converter, Date.class); 50 ConvertUtils.register(new Converter(){ 51 52 public Object convert(Class type, Object value) { 53 if(value==null) return null; 54 if(!(value instanceof String)){ 55 throw new ConversionException("只支持String类型的转换"); 56 } 57 String str=(String)value; 58 if(str.trim().equals("")) return null; 59 SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd",Locale.US); 60 61 try { 62 return df.parse(str);//将String转化为日期 63 } catch (ParseException e) { 64 throw new RuntimeException(e); 65 } 66 } 67 }, Date.class); 68 Person p=new Person(); 69 BeanUtils.setProperty(p, "birthday", birthday); 70 SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss"); 71 72 System.out.println(df.format(p.getBirthday()));//pw 73 System.out.println("___"+BeanUtils.getProperty(p, "birthday")); 74 } 75 public static void main(String[] args) throws IllegalAccessException, InvocationTargetException { 76 77 Map map=new HashMap(); 78 map.put("name", "aaa"); 79 map.put("password", "123"); 80 map.put("birthday", "1980-09-09"); 81 map.put("age", "23"); 82 //自带的转化器 有一个bug 其中brithday 不能为“” 83 ConvertUtils.register(new DateLocaleConverter(), Date.class); 84 Person p=new Person(); 85 //用map集合填充bean属性,map关键字和bean属性要一致 86 BeanUtils.populate(p, map); 87 88 System.out.println("name:"+p.getName()); 89 System.out.println("age:"+p.getAge()); 90 System.out.println("passw:"+p.getPassword()); 91 System.out.println("bir:"+p.getBirthday()); 92 93 } 94 }
不要让昨天的沮丧 让今天的梦想黯然失色
成功的人总是修改方法而不修改目标
微信公众号:javenlife