月球大数据地理空间分析展示平台-【后端】-月球后台服务
1
13693261870
2024-11-17 796b44ea813a1133beae4f3a67f1c0263510c0c7
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
package com.moon.server.helper;
 
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.moon.server.mapper.all.BasicMapper;
import com.moon.server.mapper.all.GeomBaseMapper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.support.AopUtils;
 
import java.lang.reflect.Type;
 
@SuppressWarnings("ALL")
public class ClassHelper {
    private final static Log log = LogFactory.getLog(ClassHelper.class);
 
    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;
        }
    }
 
    public static Object getBean(String className) {
        try {
            Object obj = SpringContextHelper.getBean(className);
 
            return obj;
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return null;
        }
    }
 
    public static BasicMapper getBasicMapper(String name) {
        if (StringHelper.isEmpty(name)) {
            return null;
        }
 
        Object obj = getBean(name.trim() + "Mapper");
        if (!(obj instanceof BasicMapper)) {
            return null;
        }
 
        return (BasicMapper) obj;
    }
 
    public static GeomBaseMapper getGeoBaseMapper(String name) {
        if (StringHelper.isEmpty(name)) {
            return null;
        }
 
        Object obj = getBean(name.trim() + "Mapper");
        if (!(obj instanceof GeomBaseMapper)) {
            return null;
        }
 
        return (GeomBaseMapper) obj;
    }
 
    public static String getClassName(BaseMapper baseMapper) {
        Type[] genericInterfaces = AopUtils.getTargetClass(baseMapper).getGenericInterfaces();
 
        return genericInterfaces[0].getTypeName();
    }
 
    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;
        }
    }
 
    public static Object createEntityByMapper(BaseMapper baseMapper) {
        String className = getClassName(baseMapper);
        className = className.replace(".mapper", ".entity").replace("Mapper", "Entity");
 
        return createInstance(className);
    }
}