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

古城微笑少年丶 2023-06-11 07:28 29阅读 0赞

1、创建entity(User.java)

  1. package com.jeff.entity;
  2. public class User {
  3. private String userName;
  4. private String password;
  5. private Integer age;
  6. public User() {
  7. super();
  8. }
  9. public User(String userName, String password, Integer age) {
  10. super();
  11. this.userName = userName;
  12. this.password = password;
  13. this.age = age;
  14. }
  15. public String getUserName() {
  16. return userName;
  17. }
  18. public void setUserName(String userName) {
  19. this.userName = userName;
  20. }
  21. public String getPassword() {
  22. return password;
  23. }
  24. public void setPassword(String password) {
  25. this.password = password;
  26. }
  27. public Integer getAge() {
  28. return age;
  29. }
  30. public void setAge(Integer age) {
  31. this.age = age;
  32. }
  33. @Override
  34. public String toString() {
  35. return "User [userName=" + userName + ", password=" + password + ", age=" + age + "]";
  36. }
  37. }

2、创建utils(EntityUtils.java)

  1. package com.jeff.utils;
  2. import java.lang.reflect.Field;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. public class EntityUtils {
  6. /** * * @description: 实体类转Map * @author: Jeff * @date: 2019年10月29日 * @param object * @return */
  7. public static Map<String, Object> entityToMap(Object object) {
  8. Map<String, Object> map = new HashMap<>();
  9. for (Field field : object.getClass().getDeclaredFields()) {
  10. try {
  11. boolean flag = field.isAccessible();
  12. field.setAccessible(true);
  13. Object o = field.get(object);
  14. map.put(field.getName(), o);
  15. field.setAccessible(flag);
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. return map;
  21. }
  22. /** * * @description: Map转实体类 * @author: Jeff * @date: 2019年10月29日 * @param <T> * @param map 需要初始化的数据,key字段必须与实体类的成员名字一样,否则赋值为空 * @param entity 需要转化成的实体类 * @return */
  23. public static <T> T mapToEntity(Map<String, Object> map, Class<T> entity) {
  24. T t = null;
  25. try {
  26. t = entity.newInstance();
  27. for (Field field : entity.getDeclaredFields()) {
  28. if (map.containsKey(field.getName())) {
  29. boolean flag = field.isAccessible();
  30. field.setAccessible(true);
  31. Object object = map.get(field.getName());
  32. if (object != null && field.getType().isAssignableFrom(object.getClass())) {
  33. field.set(t, object);
  34. }
  35. field.setAccessible(flag);
  36. }
  37. }
  38. return t;
  39. } catch (InstantiationException e) {
  40. e.printStackTrace();
  41. } catch (IllegalAccessException e) {
  42. e.printStackTrace();
  43. }
  44. return t;
  45. }
  46. }

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

  1. package com.jeff;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import com.jeff.entity.User;
  5. import com.jeff.utils.EntityUtils;
  6. public class TestCopyObj3 {
  7. public static void main(String[] args) throws Exception {
  8. Map<String, Object> map = new HashMap<String, Object>();
  9. map.put("userName", "Jeff");
  10. map.put("password", "123456");
  11. map.put("age", 18);
  12. System.out.println("Map对象:" + map);
  13. User user = EntityUtils.mapToEntity(map, User.class);
  14. System.out.println("User对象:" + user);
  15. }
  16. }

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

  1. package com.jeff;
  2. import java.util.Map;
  3. import com.jeff.entity.User;
  4. import com.jeff.utils.EntityUtils;
  5. public class TestCopyObj4 {
  6. public static void main(String[] args) throws Exception {
  7. User user = new User("Jeff", "123456", 18);
  8. System.out.println("User对象:" + user);
  9. Map<String, Object> map = EntityUtils.entityToMap(user);
  10. System.out.println("Map对象:" + map);
  11. }
  12. }

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

发表评论

表情:
评论列表 (有 0 条评论,29人围观)

还没有评论,来说两句吧...

相关阅读