yijiusmile
2021-03-06 551fe577e2e6b8cfc84b17fbc38b95f41c07ad00
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package cc.mrbird.febs.server.hr.util;
 
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.lang3.StringUtils;
 
import com.baomidou.mybatisplus.annotation.TableName;
 
/**
 * 实体的工具类
 * 
 * @author zhangshuaipeng
 *
 */
public class ModelUtil {
    public static Map<String, Field[]> modelFields = new HashMap<String, Field[]>();
 
    public static Map<String, String> modelJson = new HashMap<String, String>();
 
    /**
     * 判断实体不为空
     * 
     * @param obj
     * @return
     */
    public static Boolean isNotNull(Object obj) {
        if (obj != null) {
            return true;
        } else {
            return false;
        }
    }
 
    /**
     * 获取实体对应的数据表名
     * 
     * @param clazz
     *            要获取表名的实体类
     * @param equalEntity
     *            为true时返回实体的名称,否则返回注解的表名
     * @return
     */
    public static String getTableName(Class<?> clazz, boolean equalEntity) {
        String tableName = clazz.getSimpleName();
 
        if (!equalEntity) {
            TableName annotation = (TableName)clazz.getAnnotation(TableName.class);
            if (!StringUtils.isEmpty(annotation.value())) {
                tableName = annotation.value();
            }
        }
        return tableName;
    }
 
    /**
     * 获取指定字段映射的数据表的列名
     * 
     * @param f
     *            指定的字段
     * @param isAnnotation
     *            为true表示取字段使用@Column进行注解的名称,当没有注解时取字段名
     * @return
     */
//    public static String getColumnName(Field f, boolean isAnnotation) {
//        String columnName = f.getName();
//        if (isAnnotation) {
//            Annotation[] annotations = f.getAnnotations();
//            for (int i = 0; i < annotations.length; i++) {
//                if (annotations[i] instanceof Column) {
//                    // 数据列
//                    Column column = (Column) annotations[i];
//                    if (StringUtils.isNotEmpty(column.name())) {
//                        columnName = column.name();
//                    }
//                } else if (annotations[i] instanceof JoinColumn) {
//                    // 关联列
//                    JoinColumn joinColumn = (JoinColumn) annotations[i];
//                    if (StringUtils.isNotEmpty(joinColumn.name())) {
//                        columnName = joinColumn.name();
//                    }
//                }
//
//            }
//        }
 
//        return columnName;
//    }
 
    /**
     * 得到类的属性集合
     * 
     * @param c
     * @param itself
     *            是否是自身的字段
     * @return
     */
    public static Field[] getClassFields(Class<?> c, boolean itself) {
        if (itself) {
            if (modelFields.get(c.getName()) != null) {
                return modelFields.get(c.getName());
            } else {
                Field[] fields = c.getDeclaredFields();
                modelFields.put(c.getName(), fields);
                return fields;
            }
        } else {
            if (modelFields.get(c.getName()) != null) {
                return modelFields.get(c.getName());
            } else {
                List<Field> fields = new ArrayList<Field>();
                getAllDeclaredFields(c, fields);
                Field[] fies = new Field[fields.size()];
                fields.toArray(fies);
                modelFields.put(c.getName(), fies);
                return fies;
            }
        }
    }
 
    /**
     * 从c类中取得全部字段,包括父类
     * 
     * @param c
     * @param fields
     */
    public static void getAllDeclaredFields(Class<?> c, List<Field> fields) {
        Field[] fies = c.getDeclaredFields();
        Collections.addAll(fields, fies);
        Class<?> parent = c.getSuperclass();
        if (parent != Object.class) {
            getAllDeclaredFields(parent, fields);
        } else {
            return;
        }
    }
 
    /**
     * 得到类的主键字段
     * 
     * @param clazz
     * @return
     */
//    public static String getClassPkName(Class<?> clazz) {
//        Field[] fields = getClassFields(clazz, false);
//        String pkName = "";
//        for (Field f : fields) {
//            FieldInfo fieldInfo = f.getAnnotation(FieldInfo.class);
//            if (isNotNull(fieldInfo)) {
//                if ("ID".equals(fieldInfo.type())) {
//                    pkName = f.getName();
//                    break;
//                }
//            }
//        }
//        return pkName;
//    }
 
}