Map与JavaBean相互转换的工具类
作者:梁云亮 时间:2021-09-22 20:59:02
下面就来分享工具类的内容:
使用范围:JavaBean
类对象的属性不能是数组、List
、Set
、Map
public class MapBeanUtil {
/**
* JavaBean转Map
* @param obj
* @return
*/
public static Map<String, Object> bean2Map(Object obj) {
Map<String, Object> map = new LinkedHashMap<>();
Class<?> clazz = obj.getClass();
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
String fieldName = field.getName();
Object value = null;
try {
value = field.get(obj);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (value == null){
value = "";
}
map.put(fieldName, value);
}
return map;
}
/**
* Map转JavaBean
* @param clazz
* @param map
* @param <T>
* @return
*/
public static <T> T map2Bean(final Class<T> clazz, final Map<String, ? extends Object> map) {
if (map == null) {
return null;
}
T res = null;
try {
res = clazz.getDeclaredConstructor().newInstance();
//获取到所有属性,不包括继承的属性
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
//获取字段的修饰符
int mod = field.getModifiers();
if (Modifier.isStatic(mod) || Modifier.isFinal(mod)) {
continue;
}
//设置对象的访问权限
field.setAccessible(true);
//根据属性名称去map获取value
if(map.containsKey(field.getName())) {
//给对象赋值
field.set(res, map.get(field.getName()));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return res;
}
public static void main(String[] args) throws Exception {
HashMap<String, Object> map = new HashMap<>();
map.put("id", 1001);
map.put("username", "zhangsan");
map.put("password", "123456");
map.put("nickname", "张三");
map.put("email", "369950806@qq.com");
map.put("gender", true);
map.put("birth", LocalDate.now());
map.put("avatar", "/aa/bb/ab.jpg");
map.put("role", "VIP");
map.put("status", (byte) 1);
map.put("salt", "ldfkasjghweoiq324");
map.put("createTime", LocalDateTime.now());
map.put("updateTime", LocalDateTime.now());
User user = map2Bean(User.class, map);
System.out.println(user);
Map<String, Object> res = bean2Map(user);
System.out.println(map);
}
}
User类的代码:
public class User {
private Integer id;
private String username;
private String password;
private String nickname;
private String email;
private Boolean gender;
private LocalDate birth;
private String avatar;
private String role;
private Byte status;
private String salt;
private LocalDateTime createTime;
private LocalDateTime updateTime;
}
来源:https://blog.51cto.com/lianghecai/5015227
标签:Map,JavaBean,转换,工具类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java 中泛型 T 和 ? 的区别详解
2022-07-08 00:40:28
![](https://img.aspxhome.com/file/2023/8/64748_0s.png)
Java中静态代码块、构造代码块、构造函数和普通代码块的区别
2023-11-25 10:09:06
![](https://img.aspxhome.com/file/2023/8/60158_0s.png)
Java中的Object类详细介绍
2023-11-23 23:18:46
Flutter自定义圆盘取色器
2023-07-05 23:55:43
![](https://img.aspxhome.com/file/2023/0/96010_0s.gif)
java控制台打印本月的日历
2023-10-15 22:58:12
![](https://img.aspxhome.com/file/2023/1/58621_0s.jpg)
Spring Boot 整合JPA 数据模型关联使用操作(一对一、一对多、多对多)
2022-11-18 23:14:29
Springboot实现图片上传功能的示例代码
2022-08-05 22:46:44
![](https://img.aspxhome.com/file/2023/2/62722_0s.png)
详解如何在SpringBoot中自定义参数解析器
2023-07-24 16:06:51
Java 序列化和反序列化实例详解
2023-11-23 04:43:44
Java中对象与C++中对象的放置安排的对比
2022-05-31 15:07:18
线程局部变量的实现 ThreadLocal使用及场景介绍
2023-11-10 03:19:26
![](https://img.aspxhome.com/file/2023/1/59031_0s.jpg)
java实现文件下载的两种方式
2023-11-11 06:37:14
使用HTTPclient保持长连接
2023-10-17 12:29:34
![](https://img.aspxhome.com/file/2023/9/64109_0s.png)
基于request获取访问者真实IP代码示例
2023-02-15 02:45:13
Linux下动静态库的打包与使用指南(C/C++)
2023-06-21 20:22:11
![](https://img.aspxhome.com/file/2023/8/68778_0s.png)
flutter中使用流式布局示例详解
2023-08-24 23:49:06
![](https://img.aspxhome.com/file/2023/5/86765_0s.jpg)
Spring使用xml方式整合第三方框架流程详解
2022-07-05 10:50:03
JAVA NIO实现简单聊天室功能
2023-05-01 10:32:49
Java7之forkjoin简介_动力节点Java学院整理
2023-08-31 14:08:28
![](https://img.aspxhome.com/file/2023/1/58341_0s.png)
解决IDEA克隆代码后在右下角没有git分支的问题
2021-11-23 09:43:19
![](https://img.aspxhome.com/file/2023/2/63112_0s.jpg)