此工具类主要方法有
Map
对象转化为JavaBean
、JavaBean
对象转化为Map
、List<Map>
对象转化为List<JavaBean>
、List<JavaBean>
对象转化为List<Map>
等。
一、maven依赖
<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils-core -->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils-core</artifactId>
<version>1.8.3</version>
</dependency>
二、所需的测试类
public class User implements Serializable {
private String id;
private String name;
//省略get、set
}
三、工具类
1、Map
对象转化为JavaBean
/**
* 将 Map对象转化为JavaBean
* @author 601278
* @param T 要转化的类型
* @param map
* @return Object对象
*/
public static <T> T convertMap2Bean(Map map, Class T) throws Exception {
if(map==null || map.size()==0){
return null;
}
BeanInfo beanInfo = Introspector.getBeanInfo(T);
T bean = (T)T.newInstance();
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0, n = propertyDescriptors.length; i <n ; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
//String upperPropertyName = propertyName.toUpperCase();
if (map.containsKey(propertyName)) {
Object value = map.get(propertyName);
//这个方法不会报参数类型不匹配的错误。
BeanUtils.copyProperty(bean, propertyName, value);
//用这个方法对int等类型会报参数类型不匹配错误,需要我们手动判断类型进行转换,比较麻烦。
//descriptor.getWriteMethod().invoke(bean, value);
//用这个方法对时间等类型会报参数类型不匹配错误,也需要我们手动判断类型进行转换,比较麻烦。
//BeanUtils.setProperty(bean, propertyName, value);
}
}
return bean;
}
测试:
public static void main(String[] args) throws Exception {
Map map = new HashMap();
map.put("id","1");
map.put("name","zs");
User o = convertMap2Bean(map, User.class);
System.out.println(o.toString());
}
//结果:User{id='1', name='zs'}
2、JavaBean
对象转化为Map
/**
* 将 JavaBean对象转化为 Map
* @author 601278
* @param bean 要转化的类型
* @return Map对象
*/
public static Map convertBean2Map(Object bean) throws Exception {
Class type = bean.getClass();
Map returnMap = new HashMap();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors = beanInfo
.getPropertyDescriptors();
for (int i = 0, n = propertyDescriptors.length; i <n ; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean, new Object[0]);
if (result != null) {
returnMap.put(propertyName, result);
} else {
returnMap.put(propertyName, "");
}
}
}
return returnMap;
}
测试:
public static void main(String[] args) throws Exception {
User user = new User();
user.setId("1");
user.setName("zs");
Map map = convertBean2Map(user);
System.out.println(map.size());
}
结果
3、List<Map>
对象转化为List<JavaBean>
/**
* 将 List<Map>对象转化为List<JavaBean>
* @author 601278
* @param T
* @param listMap
* @return Object对象
*/
public static <T> List<T> convertListMap2ListBean(List<Map<String,Object>> listMap, Class T) throws Exception {
List<T> beanList = new ArrayList<T>();
for(int i=0, n=listMap.size(); i<n; i++){
Map<String,Object> map = listMap.get(i);
T bean = convertMap2Bean(map,T);
beanList.add(bean);
}
return beanList;
}
测试:
public static void main(String[] args) throws Exception {
Map<String,Object> map = new HashMap<>();
map.put("id","1");
map.put("name","zs");
List<Map<String,Object>> list = new ArrayList<>();
list.add(map);
List<Object> objects = convertListMap2ListBean(list, User.class);
System.out.println(objects.toString());
}
结果
4、Map
键值对转换成对象(参数object
形式)
/**Map对象中的值为 name=aaa,value=bbb
调用方法
addProperty(map,user);
*将自动将map中的值赋给user类
*此方法结合Spring框架的jdbcTemplete将非
*常有用
* @param map 存储着名称和值集合
* @param obj 要封装的对象
* @return封装好的对象
*/
public static Object addProperty(Map map,Object obj){
//遍历所有名称
Iterator it = map.keySet().iterator();
while(it.hasNext()){
//取得名称
String name = it.next().toString();
//取得值
String value = map.get(name).toString();
try{
//取得值的类形
Class type = PropertyUtils.getPropertyType(obj, name);
if(type!=null){
//设置参数
PropertyUtils.setProperty(obj, name,ConvertUtils.convert(value, type));
}
}catch(Exception ex){
ex.printStackTrace();
}
}
return obj;
}
测试:
public static void main(String[] args) throws Exception {
Map map = new HashMap();
map.put("id","1");
map.put("name","zs");
//User user = new User();
Object o = addProperty(map, new User());
System.out.println(o.toString());
}
结果
5、List<JavaBean>
对象转化为List<Map>
/**
* 将 List<JavaBean>对象转化为List<Map>
* @author 601278
* @param beanList
* @return Object对象
*/
public static List<Map<String,Object>> convertListBean2ListMap(List<Object> beanList) throws Exception {
List<Map<String,Object>> mapList = new ArrayList<Map<String,Object>>();
for(int i=0, n=beanList.size(); i<n; i++){
Object bean = beanList.get(i);
Map map = convertBean2Map(bean);
mapList.add(map);
}
return mapList;
}
测试:
public static void main(String[] args) throws Exception {
User user = new User();
user.setId("1");
user.setName("zs");
List<Object> list = new ArrayList<>();
list.add(user);
//maps的size为1,泛型Map里的size为2
List<Map<String, Object>> maps = convertListBean2ListMap(list);
System.out.println(maps.toString());
}
结果
**6、List<Map>
对象转化为List<JavaBean>
/**
*将 List<Map>对象转化为List<JavaBean>
* @param list 值为map
* @param obj
* @return
*/
public static List<Object> parse(List<Map<String,Object>> list, Class obj){
//生成集合
ArrayList ary = new ArrayList();
//遍历集合中的所有数据
for(int i = 0; i<list.size(); i++){
try {
////生成对象实历 将MAP中的所有参数封装到对象中
Object o = addProperty( (Map)list.get(i),obj.newInstance() );
//把对象加入到集合中
ary.add(o);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
//返回封装好的集合
return ary;
}
测试:
public static void main(String[] args) throws Exception {
List<Map<String,Object>> list = new ArrayList();
Map map = new HashMap();
map.put("id","1");
map.put("name","zs");
list.add(map);
List<Object> parse = parse(list, User.class);
System.out.println(parse.toString());
}
结果
网友评论