admin
2022-04-07 211840b64fa1132d76d6dff6c779e9ba2c0c450f
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package org.yeshi.utils.generater.vo.dao;
 
import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.Transient;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.generater.annotation.admin.DaoQueryCondition;
import org.yeshi.utils.generater.entity.*;
import org.yeshi.utils.generater.mybatis.Column;
import org.yeshi.utils.generater.mybatis.ColumnParseUtil;
import org.yeshi.utils.generater.mybatis.ColumnUtil;
import org.yeshi.utils.generater.mybatis.Table;
import org.yeshi.utils.generater.vo.ExceptionVO;
import org.yeshi.utils.generater.vo.xmlconfig.dao.DaoData;
import org.yeshi.utils.generater.vo.xmlconfig.dao.DaoQuery;
 
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
 
public class MyBatisDBDaoVO extends BaseData {
 
    private ClassInfo mapper;
    private ClassInfo baseMapper;
    //主键
    private MybatisColumnData identify;
    //属性
    private List<MybatisColumnData> columnList;
    private List<DaoQueryColumnData> queryList;
    private String table;
 
 
    public static class Builder {
 
        private ClassInfo baseMapper;
        private Class entity;
        private ClassInfo mapper;
        private DaoData daoData;
 
        public Builder setBaseMapper(ClassInfo baseMapper) {
            this.baseMapper = baseMapper;
            return this;
        }
 
        public Builder setEntity(Class entity) {
            this.entity = entity;
            return this;
        }
 
        public Builder setMapper(ClassInfo mapper) {
            this.mapper = mapper;
            return this;
        }
 
        public Builder setDaoData(DaoData daoData) {
            this.daoData = daoData;
            return this;
        }
 
        //获取table
 
        private String getTableName(Class clz) throws Exception {
            Annotation[] as = clz.getAnnotations();
            for (Annotation a : as) {
                if (a instanceof Table) {
                    Table t = (Table) a;
                    return t.value();
                }
            }
            throw new Exception("未获取到表名称");
        }
 
        public MyBatisDBDaoVO build() throws Exception {
 
            if (entity == null) {
                throw new Exception("entity不能为空");
            }
 
            if (baseMapper == null) {
                throw new Exception("baseMapper不能为空");
            }
 
            if (mapper == null) {
                throw new Exception("mapper不能为空");
            }
 
            //获取table名称
            String table = getTableName(entity);
 
            Field[] fields = entity.getDeclaredFields();
            MybatisColumnData identity = null;
            List<MybatisColumnData> columlist = new ArrayList<>();
            List<DaoQueryColumnData> daoQueryColumnList = new ArrayList<>();
            for (Field fd : fields) {
                //private/public 非final,static属性才会参与解析
                if (fd.getModifiers() != 1 && fd.getModifiers() != 2) {
                    continue;
                }
                boolean valid = true;
 
                Annotation[] as = fd.getAnnotations();
                for (Annotation a : as) {
                    //不参与解析
                    if (a instanceof Transient || a instanceof java.beans.Transient) {
                        valid = false;
                        break;
                    }
                }
 
                if (!valid) {
                    continue;
                }
 
                String property = fd.getName();
                String column = null;
                for (Annotation a : as) {
                    //不参与解析
                    if (a instanceof Column) {
                        column = ((Column) a).name();
                        break;
                    }
                }
 
                //驼峰写法,首字母小写
                if (column == null) {
                    column = ColumnUtil.getColumnFromProperty(fd.getName());
                }
 
                MybatisColumnData columData = new MybatisColumnData(column, property, fd.getType().getSimpleName(), ColumnParseUtil.getJDBCType(fd.getType().getName()));
 
 
                for (Annotation a : as) {
                    //主键
                    if (a instanceof Id) {
                        identity = columData;
                        identity.setType(fd.getType().getName());
                        valid = false;
                        break;
                    }
                }
 
                if (!valid) {
                    continue;
                }
 
                columlist.add(columData);
 
 
                if (daoData != null) {
                    //查询条件从外部传入
                    if (daoData.getQueryList() != null)
                        for (DaoQuery query : daoData.getQueryList()) {
                            if (query.getName().equalsIgnoreCase(columData.getProperty())) {
                                if (fd.getType().getSimpleName().equalsIgnoreCase("Date")) {
                                    daoQueryColumnList.add(new DaoQueryColumnData("max" + StringUtil.firstCharToUpper(columData.getProperty()), columData, fd.getType().getSimpleName(), DaoQueryCondition.QueryType.lt));
                                    daoQueryColumnList.add(new DaoQueryColumnData("min" + StringUtil.firstCharToUpper(columData.getProperty()), columData, fd.getType().getSimpleName(), DaoQueryCondition.QueryType.gte));
                                } else {
                                    daoQueryColumnList.add(new DaoQueryColumnData(columData.getProperty(), columData, fd.getType().getSimpleName(), query.getQueryType()));
                                }
                            } else if (query.getName().equalsIgnoreCase(columData.getColumn())) {
                                daoQueryColumnList.add(new DaoQueryColumnData(columData.getColumn(), columData, fd.getType().getSimpleName(), query.getQueryType()));
                            }
                        }
 
                } else {
                    //获取注解查询条件
                    for (Annotation a : as) {
                        if (a instanceof DaoQueryCondition) {
                            if (fd.getType().getSimpleName().equalsIgnoreCase("Date")) {
                                daoQueryColumnList.add(new DaoQueryColumnData("max" + StringUtil.firstCharToUpper(columData.getProperty()), columData, fd.getType().getSimpleName(), DaoQueryCondition.QueryType.lt));
                                daoQueryColumnList.add(new DaoQueryColumnData("min" + StringUtil.firstCharToUpper(columData.getProperty()), columData, fd.getType().getSimpleName(), DaoQueryCondition.QueryType.gte));
                            } else {
                                daoQueryColumnList.add(new DaoQueryColumnData(columData.getProperty(), columData, fd.getType().getSimpleName(), ((DaoQueryCondition) a).queryType()));
                            }
                            break;
                        }
                    }
                }
            }
 
            if (identity == null)
                throw new Exception("尚未指定主键");
 
 
            MyBatisDBDaoVO vo = new MyBatisDBDaoVO();
            vo.setBaseMapper(baseMapper);
            vo.setEntity(new ClassInfo(entity.getSimpleName(), entity.getName()));
            vo.setIdentify(identity);
            vo.setTable(table);
            vo.setQueryList(daoQueryColumnList);
            vo.setPackageName(mapper.getClazz().replace("." + mapper.getName(), ""));
            vo.setMapper(mapper);
            vo.setColumnList(columlist);
            return vo;
        }
    }
 
    public ClassInfo getMapper() {
        return mapper;
    }
 
    public void setMapper(ClassInfo mapper) {
        this.mapper = mapper;
    }
 
    public ClassInfo getBaseMapper() {
        return baseMapper;
    }
 
    public void setBaseMapper(ClassInfo baseMapper) {
        this.baseMapper = baseMapper;
    }
 
 
    public MybatisColumnData getIdentify() {
        return identify;
    }
 
    public void setIdentify(MybatisColumnData identify) {
        this.identify = identify;
    }
 
    public List<MybatisColumnData> getColumnList() {
        return columnList;
    }
 
    public void setColumnList(List<MybatisColumnData> columnList) {
        this.columnList = columnList;
    }
 
    public List<DaoQueryColumnData> getQueryList() {
        return queryList;
    }
 
    public void setQueryList(List<DaoQueryColumnData> queryList) {
        this.queryList = queryList;
    }
 
    public String getTable() {
        return table;
    }
 
    public void setTable(String table) {
        this.table = table;
    }
 
 
}