JavaBean和Map转换封装类的方法
作者:jingxian 时间:2023-04-18 06:50:52
实例如下:
package com.ljq.util;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Map工具类
*
* @author jqlin
*/
public class MapUtils {
/**
* 从map集合中获取属性值
*
* @param <E>
* @param map
* map集合
* @param key
* 键对
* @param defaultValue
* 默认值
* @return
* @author jiqinlin
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public final static <E> E get(Map map, Object key, E defaultValue) {
Object o = map.get(key);
if (o == null)
return defaultValue;
return (E) o;
}
/**
* Map集合对象转化成 JavaBean集合对象
*
* @param javaBean JavaBean实例对象
* @param mapList Map数据集对象
* @return
* @author jqlin
*/
@SuppressWarnings({ "rawtypes" })
public static <T> List<T> map2Java(T javaBean, List<Map> mapList) {
if(mapList == null || mapList.isEmpty()){
return null;
}
List<T> objectList = new ArrayList<T>();
T object = null;
for(Map map : mapList){
if(map != null){
object = map2Java(javaBean, map);
objectList.add(object);
}
}
return objectList;
}
/**
* Map对象转化成 JavaBean对象
*
* @param javaBean JavaBean实例对象
* @param map Map对象
* @return
* @author jqlin
*/
@SuppressWarnings({ "rawtypes","unchecked", "hiding" })
public static <T> T map2Java(T javaBean, Map map) {
try {
// 获取javaBean属性
BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());
// 创建 JavaBean 对象
Object obj = javaBean.getClass().newInstance();
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
if (propertyDescriptors != null && propertyDescriptors.length > 0) {
String propertyName = null; // javaBean属性名
Object propertyValue = null; // javaBean属性值
for (PropertyDescriptor pd : propertyDescriptors) {
propertyName = pd.getName();
if (map.containsKey(propertyName)) {
propertyValue = map.get(propertyName);
pd.getWriteMethod().invoke(obj, new Object[] { propertyValue });
}
}
return (T) obj;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* JavaBean对象转化成Map对象
*
* @param javaBean
* @return
* @author jqlin
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map java2Map(Object javaBean) {
Map map = new HashMap();
try {
// 获取javaBean属性
BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
if (propertyDescriptors != null && propertyDescriptors.length > 0) {
String propertyName = null; // javaBean属性名
Object propertyValue = null; // javaBean属性值
for (PropertyDescriptor pd : propertyDescriptors) {
propertyName = pd.getName();
if (!propertyName.equals("class")) {
Method readMethod = pd.getReadMethod();
propertyValue = readMethod.invoke(javaBean, new Object[0]);
map.put(propertyName, propertyValue);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}
标签:java,bean,map
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot2.0如何通过fastdfs实现文件分布式上传
2022-03-20 16:49:24
![](https://img.aspxhome.com/file/2023/7/71917_0s.png)
Mybatis中一条SQL使用两个foreach的问题及解决
2022-02-04 06:05:41
![](https://img.aspxhome.com/file/2023/6/79316_0s.jpg)
C#为控件添加自定义事件及自定义触发
2021-11-24 14:29:16
![](https://img.aspxhome.com/file/2023/7/100137_0s.png)
Android定时器Timer的停止和重启实现代码
2022-10-03 23:25:43
C#编程和Visual Studio使用技巧(上)
2021-10-01 00:54:27
![](https://img.aspxhome.com/file/2023/8/104248_0s.jpg)
Java 回调函数深入理解
2023-11-01 17:32:04
Android短信验证码自动填充功能
2023-11-25 00:04:20
![](https://img.aspxhome.com/file/2023/0/138020_0s.png)
详解Java中String类的各种用法
2022-03-10 12:48:35
![](https://img.aspxhome.com/file/2023/0/91730_0s.png)
Android中实现地址栏输入网址能浏览该地址网页源码并操作访问网络
2022-08-21 16:11:42
![](https://img.aspxhome.com/file/2023/0/106860_0s.gif)
Android进阶事件分发机制解决事件冲突
2021-08-22 06:12:34
![](https://img.aspxhome.com/file/2023/7/123367_0s.png)
基于Spring Security前后端分离的权限控制系统问题
2022-10-16 21:32:39
![](https://img.aspxhome.com/file/2023/4/66254_0s.png)
java使用JNA(Java Native Access)调用dll的方法
2022-02-06 09:27:34
Java多线程的其他知识_动力节点Java学院整理
2023-09-06 06:01:58
![](https://img.aspxhome.com/file/2023/2/68032_0s.png)
浅谈Action+Service +Dao 功能
2023-01-04 13:44:20
![](https://img.aspxhome.com/file/2023/5/113405_0s.png)
Unity实现UI光晕效果(发光效果)
2022-07-08 16:37:41
![](https://img.aspxhome.com/file/2023/7/104217_0s.jpg)
java报错:找不到或无法加载主类的解决方法简单粗暴
2023-03-29 17:20:58
![](https://img.aspxhome.com/file/2023/4/93074_0s.png)
C#中使用IFormattable实现自定义格式化字符串输出示例
2023-05-31 23:34:04
![](https://img.aspxhome.com/file/2023/6/104226_0s.png)
Java源码解析之Iterable接口
2023-04-17 08:06:57
实例讲解Java并发编程之闭锁
2023-10-25 14:25:07
Java8新特性之JavaFX 8_动力节点Java学院整理
2023-03-26 02:31:28
![](https://img.aspxhome.com/file/2023/7/65367_0s.jpg)