實(shí)體bean。
package com.ztf;

public class Entity {
private Integer id;
private String name;
public void sayHello(){
System.out.println("sayHello()---> 無(wú)參");
}
public void sayHello(String s){
System.out.println("sayHello()---> 有1個(gè)參數(shù)" );
}
public void sayHello(Integer a,Integer b){
System.out.println("sayHello()---> 有2個(gè)參數(shù)");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
package com.ztf;
import java.util.Map;

import org.apache.commons.beanutils.MethodUtils;
import org.apache.commons.beanutils.PropertyUtils;

public class TestPropertyUtils {
public static void main(String[] args) throws Exception{
Entity entity = new Entity();
entity.setId(1) ;
entity.setName("斷點(diǎn)");
// 通過(guò)PropertyUtils的getProperty方法獲取指定屬性的值
Integer id = (Integer)PropertyUtils.getProperty(entity, "id");
String name = (String)PropertyUtils.getProperty(entity, "name");
System.out.println("id = " + id + " name = " + name);
// 調(diào)用PropertyUtils的setProperty方法設(shè)置entity的指定屬性
PropertyUtils.setProperty(entity, "name", "每天進(jìn)步一點(diǎn)");
System.out.println("name = " + entity.getName());
// 通過(guò)PropertyUtils的describe方法把entity的所有屬性與屬性值封裝進(jìn)Map中
Map map = PropertyUtils.describe(entity);
System.out.println("id = " + map.get("id") + " name = " + map.get("name"));
}
}
輸出:
id = 1 name = 斷點(diǎn)
name = 每天進(jìn)步一點(diǎn)
id = 1 name = 每天進(jìn)步一點(diǎn)
其它例子:
import org.apache.commons.beanutils.PropertyUtils;
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
for(BaseGrpMemberVO member : MemberVO){
Map m = new HashMap();
for(Map.Entry<String, String> entry: fieldMap.entrySet()){
String key = entry.getKey();
fieldList.add(key); //記錄字段名
String[] keyArray = key.split("\\.");
if(keyArray.length == 2){//成員信息
Object o =PropertyUtils.getProperty(member, keyArray[1]);
m.put(key, o==null?"":o.toString());
}else if(keyArray.length == 3){//險(xiǎn)別信息
List<BaseGrpCvrgVO> cvrgVoList = mgr.getRelCvrgById(member.getCPkId());
String cvrgNo = keyArray[0];
for(BaseGrpCvrgVO cvrgVo : cvrgVoList){
if(cvrgNo.equals(cvrgVo.getCCvrgNo())){
Object o =PropertyUtils.getProperty(cvrgVo, keyArray[2]);
m.put(key, o==null?"":o.toString());
}
}
}
}
data.add(m);
}





























































輸出:
id = 1 name = 斷點(diǎn)
name = 每天進(jìn)步一點(diǎn)
id = 1 name = 每天進(jìn)步一點(diǎn)
其它例子:
import org.apache.commons.beanutils.PropertyUtils;
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
for(BaseGrpMemberVO member : MemberVO){
Map m = new HashMap();
for(Map.Entry<String, String> entry: fieldMap.entrySet()){
String key = entry.getKey();
fieldList.add(key); //記錄字段名
String[] keyArray = key.split("\\.");
if(keyArray.length == 2){//成員信息
Object o =PropertyUtils.getProperty(member, keyArray[1]);
m.put(key, o==null?"":o.toString());
}else if(keyArray.length == 3){//險(xiǎn)別信息
List<BaseGrpCvrgVO> cvrgVoList = mgr.getRelCvrgById(member.getCPkId());
String cvrgNo = keyArray[0];
for(BaseGrpCvrgVO cvrgVo : cvrgVoList){
if(cvrgNo.equals(cvrgVo.getCCvrgNo())){
Object o =PropertyUtils.getProperty(cvrgVo, keyArray[2]);
m.put(key, o==null?"":o.toString());
}
}
}
}
data.add(m);
}