java map转bean_Map与javaBean的互转

本文介绍了如何在Java中将Map转换为JavaBean以及JavaBean转换为Map的多种方法,包括使用Introspector获取Bean信息、通过反射调用getter/setter方法,以及利用BeanMap进行快速转换。同时,提供了相应的代码实现。
摘要由CSDN通过智能技术生成

1.map转javaBean

/**

* map转换成javaBean

* @param map

* @return

*/

public static Object transMap2Bean(Map map,Object obj){

try{

//1.获取bean信息

BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());

PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();

for (PropertyDescriptor prop: properties) {

String key = prop.getName();

if(map.containsKey(key) && map.get(key) != null){

Object value = map.get(key);

Method setMethod = prop.getWriteMethod();

setMethod.invoke(obj,value);

}

}

}catch(Exception e){

e.printStackTrace();

}

return obj;

}

2.javaBean转map

第一种办法:

/**

* javaBean转换成map

* @param obj

* @return

*/

public static Map transBean2Map(Object obj){

Map map = new HashMap();

try{

//1.获取bean信息

BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());

PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();

if(properties != null && properties.length>0){

for (PropertyDescriptor prop :properties) {

//2.得到属性名

String name = prop.getName();

//3.过滤class属性

if(!"class".equals(name)){

//4.得到属性的get方法

Method getter = prop.getReadMethod();

//5.获取属性值

Object value = getter.invoke(obj);

//6.放入map中

map.put(name,value);

}

}

}

}catch (Exception e){

e.printStackTrace();

}

return map;

}

第二种办法:

public static Map transBean2Map2(Object obj){

Map map = new HashMap();

try{

Field[] fields = obj.getClass().getDeclaredFields();

if (fields != null && fields.length > 0){

for (Field field : fields) {

int a = field.getModifiers();

System.out.println(a);

//当属性的修饰符为private时,需要先setAccessible(true)

if (!field.isAccessible()){

field.setAccessible(true);

}

Object value = field.get(obj);

map.put(field.getName(),value);

}

}

}catch(Exception e){

e.printStackTrace();

}

return map;

}

3.利用BeanMap转map的方式

速度快,提供map的接口操作对象

Object get(Object key)

Object put(Object key, Object value)

void putAll(Map t)

Set entrySet()

Collection values()

boolean containsKey(Object key)

……

/**

* 将map装换为javabean对象

* @param map

* @param bean

* @return

*/

public static T mapToBean(Map map,T bean) {

BeanMap beanMap = BeanMap.create(bean);

beanMap.putAll(map);

return bean;

}

/**

* bean转Map

* @param

* @return

*/

public static Map bean2Map(T bean){

Map map = new HashMap();

try{

if (bean != null){

BeanMap beanMap = BeanMap.create(bean);

map.putAll(beanMap);

}

}catch (Exception e){

e.printStackTrace();

}

return map;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java中将Map换为实体类的示例代码: ```java public class EntityMapUtils { /** * 将一个 Map对象化为JavaBean * * @param map 包含属性值的map * @param beanClass 要化成的类 * @return 化出来的JavaBean对象 */ public static <T> T mapToEntity(Map<String, Object> map, Class<T> beanClass) throws Exception { // 创建 JavaBean 对象 T obj = beanClass.newInstance(); // 获取类中所有的属性集合 Field[] fields = obj.getClass().getDeclaredFields(); // 遍历属性集合,将属性值从map中赋值给JavaBean对象 for (Field field : fields) { // 获取属性名 String fieldName = field.getName(); // 获取属性值 Object fieldValue = map.get(fieldName); // 如果属性值不为空,则将属性值赋值给JavaBean对象 if (fieldValue != null) { field.setAccessible(true); field.set(obj, fieldValue); } } return obj; } /** * 将实体类换为Map * * @param obj 要化的实体类 * @return 化出来的Map对象 */ public static Map<String, Object> entityToMap(Object obj) throws Exception { // 创建Map对象 Map<String, Object> map = new HashMap<>(); // 获取类中所有的属性集合 Field[] fields = obj.getClass().getDeclaredFields(); // 遍历属性集合,将属性值从JavaBean对象中取出并存入Map对象中 for (Field field : fields) { // 获取属性名 String fieldName = field.getName(); // 获取属性值 field.setAccessible(true); Object fieldValue = field.get(obj); // 如果属性值不为空,则将属性值存入Map对象中 if (fieldValue != null) { map.put(fieldName, fieldValue); } } return map; } } ``` 使用示例: ```java // 定义一个实体类 public class User { private String name; private int age; public User() { } public User(String name, int age) { this.name = name; this.age = age; } // 省略getter和setter方法 } // 将Map换为实体类 Map<String, Object> map = new HashMap<>(); map.put("name", "张三"); map.put("age", 20); User user = EntityMapUtils.mapToEntity(map, User.class); // 将实体类换为Map User user = new User("张三", 20); Map<String, Object> map = EntityMapUtils.entityToMap(user); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值