管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2022-10-29 16aff7930d4a2b8e1034f8f3d6caafb5f422b363
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.lf.server.helper;
 
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.support.AopUtils;
 
import java.lang.reflect.Type;
 
/**
 * 类帮助类
 * @author WWW
 */
public class ClassHelper {
    private final static Log log = LogFactory.getLog(ClassHelper.class);
 
    /**
     * 根据类名创建实例
     *
     * @param className 类名
     * @return 实体
     */
    public static Object createInstance(String className) {
        try {
            Class clazz = Class.forName(className);
            Object obj = clazz.newInstance();
 
            return obj;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }
 
    /**
     * 获取Bean
     *
     * @param className 类名
     * @return Bean
     */
    public static Object getBean(String className) {
        try {
            Object obj = SpringContextHelper.getBean(className);
 
            return obj;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }
 
    /**
     * 获取类名
     *
     * @param baseMapper 父Mapper
     * @return 类名
     */
    public static String getClassName(BaseMapper baseMapper) {
        Type[] genericInterfaces = AopUtils.getTargetClass(baseMapper).getGenericInterfaces();
 
        return genericInterfaces[0].getTypeName();
    }
 
    /**
     * 根据Mapper类名获取实体类的Class
     *
     * @param className Mapper类名
     * @return 实体类的Class
     */
    public static Class getEntityClass(String className) {
        try {
            className = className.replace(".mapper", ".entity").replace("Mapper", "Entity");
            Class clazz = Class.forName(className);
 
            return clazz;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }
 
 
    /**
     * 根据BaseMapper创建实体类
     *
     * @param baseMapper 父Mapper
     * @return 实体类
     */
    public static Object createEntityByMapper(BaseMapper baseMapper) {
        String className = getClassName(baseMapper);
        className = className.replace(".mapper", ".entity").replace("Mapper", "Entity");
 
        return createInstance(className);
    }
}