Java实体类转Map、Map转实体类

4 篇文章 0 订阅

1、创建entity(User.java)

package com.jeff.entity;

public class User {

	private String userName;
	private String password;
	private Integer age;

	public User() {
		super();
	}

	public User(String userName, String password, Integer age) {
		super();
		this.userName = userName;
		this.password = password;
		this.age = age;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	@Override
	public String toString() {
		return "User [userName=" + userName + ", password=" + password + ", age=" + age + "]";
	}

}

2、创建utils(EntityUtils.java)

package com.jeff.utils;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class EntityUtils {
	/**
	 * 
	 * @description: 实体类转Map
	 * @author: Jeff
	 * @date: 2019年10月29日
	 * @param object
	 * @return
	 */
	public static Map<String, Object> entityToMap(Object object) {
		Map<String, Object> map = new HashMap<>();
		for (Field field : object.getClass().getDeclaredFields()) {
			try {
				boolean flag = field.isAccessible();
				field.setAccessible(true);
				Object o = field.get(object);
				map.put(field.getName(), o);
				field.setAccessible(flag);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return map;
	}

	/**
	 * 
	 * @description: Map转实体类
	 * @author: Jeff
	 * @date: 2019年10月29日
	 * @param <T>
	 * @param map    需要初始化的数据,key字段必须与实体类的成员名字一样,否则赋值为空
	 * @param entity 需要转化成的实体类
	 * @return
	 */
	public static <T> T mapToEntity(Map<String, Object> map, Class<T> entity) {
		T t = null;
		try {
			t = entity.newInstance();
			for (Field field : entity.getDeclaredFields()) {
				if (map.containsKey(field.getName())) {
					boolean flag = field.isAccessible();
					field.setAccessible(true);
					Object object = map.get(field.getName());
					if (object != null && field.getType().isAssignableFrom(object.getClass())) {
						field.set(t, object);
					}
					field.setAccessible(flag);
				}
			}
			return t;
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return t;
	}

}

3、创建Map转实体类(TestCopyObj3.java)

package com.jeff;

import java.util.HashMap;
import java.util.Map;

import com.jeff.entity.User;
import com.jeff.utils.EntityUtils;

public class TestCopyObj3 {

	public static void main(String[] args) throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("userName", "Jeff");
		map.put("password", "123456");
		map.put("age", 18);
		System.out.println("Map对象:" + map);
		User user = EntityUtils.mapToEntity(map, User.class);
		System.out.println("User对象:" + user);
	}

}

4、控制台输出结果
在这里插入图片描述
5、创建实体类转Map(TestCopyObj4.java)

package com.jeff;

import java.util.Map;

import com.jeff.entity.User;
import com.jeff.utils.EntityUtils;

public class TestCopyObj4 {

	public static void main(String[] args) throws Exception {
		User user = new User("Jeff", "123456", 18);
		System.out.println("User对象:" + user);
		Map<String, Object> map = EntityUtils.entityToMap(user);
		System.out.println("Map对象:" + map);
	}

}

6、控制台输出结果
在这里插入图片描述

  • 8
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值