BeanUtils的简单使用
一个简单的People类:
import java.util.List;import java.util.Map;public class People { private int id; private String name; private int age; private String email; private double pay; private Listhobit; private Map scroe; @Override public String toString(){ return "People:{"+"id:"+id+";name:"+name+";age:"+age+";email:"+email+";pay:"+pay+";hobit:"+hobit+";scroe:"+scroe+"}"; }//get set..
BeanUtils
import org.apache.commons.beanutils.BeanUtils;import org.apache.commons.beanutils.PropertyUtils;import java.lang.reflect.InvocationTargetException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * @author Mr_Tank_ * */public class Main { public static void main(String[] args) throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException { //System.out.println("Hello World!"); People people=new People(); people.setId(1); people.setAge(20); people.setName("tanweijie"); //people.setEmail("474481263@qq.com"); people.setPay(3000); Listh=new ArrayList (); h.add("football"); h.add("basketball"); people.setHobit(h); Map scroe=new HashMap (); scroe.put("Math","80"); scroe.put("English","90"); people.setScroe(scroe); System.out.println("-----------BeanUtils Test-----------"); //BeanUtils Test System.out.println(BeanUtils.describe(people)); System.out.println("name:"+BeanUtils.getProperty(people,"name")); System.out.println("scroe:"+BeanUtils.getProperty(people,"scroe")); String[] hl=BeanUtils.getArrayProperty(people, "hobit"); for (String s:hl){ System.out.println(s); } //clone Bean People cp= (People) BeanUtils.cloneBean(people); System.out.println("clone Bean:"+cp.toString()); //Map HashMap ph=new HashMap (); ph.put("id",2); ph.put("name","tan"); ph.put("pay",3000); ph.put("email","474481263@qq.com"); ph.put("age",22); ph.put("hobit",h); People people1=new People(); //Map to obj BeanUtils.populate(people1,ph); System.out.println(people1.toString()); //PropertyUtils Test System.out.println("-----------PropertyUtils Test-----------"); PropertyUtils.setProperty(people,"email","474481263@qq.com"); System.out.println(PropertyUtils.describe(people)); System.out.println("name:"+PropertyUtils.getProperty(people,"name")); //describe Map pd=PropertyUtils.describe(people); System.out.println("map size:"+pd.size()); }}
结果:
-----------BeanUtils Test-----------{id=1, scroe={Math=80, English=90}, email=null, age=20, name=tanweijie, class=class People, pay=3000.0, hobit=football}name:tanweijiescroe:{Math=80, English=90}footballbasketballclone Bean:People:{id:1;name:tanweijie;age:20;email:null;pay:3000.0;hobit:[football, basketball];scroe:{Math=80, English=90}}People:{id:2;name:tan;age:22;email:474481263@qq.com;pay:3000.0;hobit:[football, basketball];scroe:{Math=80, English=90}}-----------PropertyUtils Test-----------{id=1, scroe={Math=80, English=90}, email=474481263@qq.com, age=20, name=tanweijie, class=class People, pay=3000.0, hobit=[football, basketball]}name:tanweijiemap size:8