duhuizhe
2024-04-17 c389984b5d3e5523e1b22de1fc045dc415261330
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.yqzx.common.util;
 
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.beanutils.BeanUtils;
 
import java.util.Map;
 
/**
 * @Description 类转换
 * @Author wh
 * @Date 2023/12/9 16:45
 */
 @Slf4j
 public class BeanUtil {
 
     public static <T> Map<String, Object> beanToMap(T bean, Map<String, Object> map) {
         if (bean == null) {
             return map;
         }
         try {
             Map<String, String> maps = BeanUtils.describe(bean);
             for (Map.Entry<String, String> mp: maps.entrySet()) {
                 map.put(mp.getKey(), mp.getValue());
             }
         } catch (Exception e) {
             log.error(e.getMessage());
         }
         return map;
     }
 
     public static <T> T mapToBean(Map<String, Object> map, Class<T> clazz){
         T bean = null;
         try {
             bean = clazz.newInstance();
             BeanUtils.populate(bean, map);
         } catch (Exception e) {
             log.error(e.getMessage());
         }
         return bean;
     }
 
     public static Map objToMap(Object obj) {
         if (obj == null) {
             return new BeanMap();
         }
         return new BeanMap(obj);
     }
 }