MapUtil.java 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package easydo.technology.utils;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.annotation.TableField;
  6. import easydo.technology.annotation.NotTableField;
  7. import java.lang.reflect.Field;
  8. import java.util.HashMap;
  9. import java.util.List;
  10. import java.util.Map;
  11. /**
  12. * map工具类
  13. *
  14. *
  15. * @author whz 2020-12-25
  16. */
  17. public class MapUtil {
  18. /**
  19. * map转List
  20. */
  21. public static <T> List<T> mapToList(Map<String, Object> map, Class<T> clazz, String key) {
  22. List<T> t = null;
  23. JSONObject jsonObject = mapToJson(map);
  24. JSONArray array = jsonObject.getJSONArray(key);
  25. t = array.toJavaList(clazz);
  26. return t;
  27. }
  28. /**
  29. * map转Json
  30. */
  31. public static JSONObject mapToJson(Map<String, Object> map) {
  32. String data = JSON.toJSONString(map);
  33. return JSON.parseObject(data);
  34. }
  35. /**
  36. * map中取key对应的value
  37. *
  38. * @param map
  39. * @param key
  40. * @return
  41. */
  42. public static String mapToString(Map<String, Object> map, String key) {
  43. JSONObject jsonObject = mapToJson(map);
  44. return jsonObject.getString(key);
  45. }
  46. /**
  47. * map中取类对象
  48. *
  49. * @param map
  50. * @param clazz
  51. * @param key
  52. * @param <T>
  53. * @return
  54. */
  55. public static <T> T mapToObject(Map<String, Object> map, Class<T> clazz, String key) {
  56. T t = null;
  57. JSONObject jsonObject = mapToJson(map);
  58. JSONObject object = jsonObject.getJSONObject(key);
  59. t = object.toJavaObject(clazz);
  60. return t;
  61. }
  62. public static <T> T mapToObject(Map<String, Object> map, Class<T> clazz) {
  63. T t = null;
  64. JSONObject jsonObject = mapToJson(map);
  65. t = jsonObject.toJavaObject(clazz);
  66. return t;
  67. }
  68. /**
  69. * 对象转化为Map
  70. *
  71. * @param obj
  72. * @return
  73. * @throws Exception
  74. */
  75. public static Map<String, Object> objectToMap(Object obj) throws Exception {
  76. if (obj == null) {
  77. return new HashMap<>();
  78. }
  79. Map<String, Object> map = new HashMap<>();
  80. Field[] declaredFields = obj.getClass().getDeclaredFields();
  81. for (Field field : declaredFields) {
  82. if ("folders".equals(field.getName()) || "serialVersionUID".equals(field.getName())) {
  83. continue;
  84. }
  85. if (field.isAnnotationPresent(NotTableField.class)) {
  86. continue;
  87. }
  88. if (field.isAnnotationPresent(TableField.class)) {
  89. if (!field.getAnnotation(TableField.class).exist()) {
  90. continue;
  91. }
  92. }
  93. field.setAccessible(true);
  94. if (StringUtil.isEmpty(field.get(obj))) {
  95. continue;
  96. }
  97. map.put(field.getName(), field.get(obj));
  98. }
  99. return map;
  100. }
  101. }