BeanUtils1.9.1簡単な使用
4051 ワード
BeanUtilsの簡単な使用
簡単なPeopleクラス:
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 List<String> hobit;
private Map<String,String> 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("[email protected]");
people.setPay(3000);
List<String> h=new ArrayList<String>();
h.add("football");
h.add("basketball");
people.setHobit(h);
Map<String,String> scroe=new HashMap<String, String>();
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<String,Object> ph=new HashMap<String, Object>();
ph.put("id",2);
ph.put("name","tan");
ph.put("pay",3000);
ph.put("email","[email protected]");
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","[email protected]");
System.out.println(PropertyUtils.describe(people));
System.out.println("name:"+PropertyUtils.getProperty(people,"name"));
//describe
Map<String, Object> 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:tanweijie
scroe:{Math=80, English=90}
football
basketball
clone 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:[email protected];pay:3000.0;hobit:[football, basketball];scroe:{Math=80, English=90}}
-----------PropertyUtils Test-----------
{id=1, scroe={Math=80, English=90}, [email protected], age=20, name=tanweijie, class=class People, pay=3000.0, hobit=[football, basketball]}
name:tanweijie
map size:8