Posted on 2010-05-30 09:51
断点 阅读(544)
评论(0) 编辑 收藏 所属分类:
Apache
实体bean。
package com.ztf;
public class Entity {
private Integer id;
private String name;
public void sayHello(){
System.out.println("sayHello()---> 无参");
}
public void sayHello(String s){
System.out.println("sayHello()---> 有1个参数" );
}
public void sayHello(Integer a,Integer b){
System.out.println("sayHello()---> 有2个参数");
}
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("断点");
// 通过PropertyUtils的getProperty方法获取指定属性的值
Integer id = (Integer)PropertyUtils.getProperty(entity, "id");
String name = (String)PropertyUtils.getProperty(entity, "name");
System.out.println("id = " + id + " name = " + name);
// 调用PropertyUtils的setProperty方法设置entity的指定属性
PropertyUtils.setProperty(entity, "name", "每天进步一点");
System.out.println("name = " + entity.getName());
// 通过PropertyUtils的describe方法把entity的所有属性与属性值封装进Map中
Map map = PropertyUtils.describe(entity);
System.out.println("id = " + map.get("id") + " name = " + map.get("name"));
}
}
输出:
id = 1 name = 断点
name = 每天进步一点
id = 1 name = 每天进步一点
其它例子:
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){//险别信息
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);
}