粗略的整理了BeanUtil类~~~~~
海川520 2011-01-13 04:35:44 http://topic.csdn.net/u/20110113/10/801168dd-a390-40cb-b381-53021e3d4e4e.html?11736 字符串处理类此链接。
public static void testDemo(Object obj) throws Exception{
//对象复制,属于是深复制吧~~~~~~~~~~~~~~~~~~~~~~
//UserBean ub = (UserBean) BeanUtils.cloneBean(obj);
//ub.setUName("李四");
//System.out.println(obj + " ~~ " + ub + " ~~ " + ub.getUName() + " ~~ " + ((UserBean)obj).getUName());
//复制2个Bean中相同属性的值~~~~~~~~~~~~~~~~~~~~~~~~~~
//PersonBean pba = new PersonBean();
//BeanUtils.copyProperties(pba, obj);
//System.out.println(pba.getId());
//给Bean中某个属性重新赋值,第一个字符大写(小写不行,可能是忘转换了)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//BeanUtils.copyProperty(obj, "UName", "麻匪");
//System.out.println(((UserBean)obj).getId() + " ~~ " + ((UserBean)obj).getUName());
//封装的属性值转换为map(value为null,全变成字符串放key中了)~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//Map map = BeanUtils.describe(obj);
//showIteratorView(map);
//把指定属性的值放到数组中~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//String[] str = BeanUtils.getArrayProperty(obj, "id");
//System.out.println(Arrays.asList(str));
//返回Bean中集合的指定下标的值,2种形式~~~~~~~~~~~~~~~~~~~~~~~~~~
//System.out.println(BeanUtils.getIndexedProperty(obj, "UHobby[1]"));
//System.out.println(BeanUtils.getIndexedProperty(obj, "UHobby", 2));
//返回Bean中map指定key的值,2种形式~~~~~~~~~~~~~~~~~~~~~~~~~~
//System.out.println(BeanUtils.getMappedProperty(obj, "map" , "2008"));
//获得指定属性的值~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//System.out.println(BeanUtils.getProperty(obj, "UName"));
//未发现不同的地方,获得指定属性的值(返回指定的名称(可能是嵌套的)属性)
//System.out.println(BeanUtils.getNestedProperty(obj, "map"));
//未发现不同的地方,获得指定属性的值
//System.out.println(BeanUtils.getSimpleProperty(obj, "UName"));
//将Map转换为Bean。key对应的属性名.value为值~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//UserBean ubb = new UserBean();
//BeanUtils.populate(ubb, getMapParamsValue());
//System.out.println(ubb.getId() + " ~~ " + ubb.getUName() + " ~~ " + ubb.getUHobby());
//设置Bean中指定的属性值~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//BeanUtils.setProperty(obj, "UName", "大家好");
//System.out.println(((UserBean)obj).getId() + " ~~ " + ((UserBean)obj).getUName());
//未解~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//BeanUtils.createCache();
//BeanUtils.getCacheFast(new HashMap());
//BeanUtils.setCacheFast(new HashMap(), true);
//还有PropertyUtils类。方法大致相同。不同地方希望大家补上~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}
//迭代显示
public static void showIteratorView(Map map){
Iterator setKey = map.entrySet().iterator();
while(setKey.hasNext()){
Object beanObj = setKey.next();
System.out.println(beanObj +" ~~ "+ map.get(beanObj));
}
}
//返回map
public static Map getMapParamsValue(){
Map map = new HashMap();
map.put("id", "54120");
map.put("UName", "麻匪");
map.put("UHobby", Arrays.asList("篮球", "电影", "足球"));
return map;
}
public static void main(String[] args) throws Exception {
Map map =new HashMap();
map.put("2007", 76);
map.put("2008", 90);
UserBean ub = new UserBean("1001", "张三", Arrays.asList("篮球", "电影", "足球"), map);
Test.testDemo(ub);
}