admin
2021-06-30 92cc47680855fd0ad62c90de013ea79530cf5c21
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package org.yeshi.utils.generater.mybatis;
 
import java.beans.Transient;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
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;
 
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
import org.yeshi.utils.generater.entity.MybatisColumnData;
 
public class MyBatisMapperUtil {
    private static String basePath = "D:/mybatis";
 
    static {
        // 创建工作目录
        if (!new File(basePath).exists())
            new File(basePath).mkdirs();
    }
 
    // 获取dao所在的包名
    private static String getDaoPackageName(Class<?> bean) {
        String name = bean.getName();
        String[] ns = name.split("\\.");
        if (ns.length > 2) {
            String pks = "";
            for (int i = 0; i < ns.length - 2; i++) {
                pks += ns[i] + ".";
            }
            return pks + "dao";
        }
        return "";
    }
 
    private static String getMapperPackageName(Class<?> bean) {
        String name = bean.getName();
        String[] ns = name.split("\\.");
        if (ns.length > 2) {
            String pks = "";
            for (int i = 0; i < ns.length - 2; i++) {
                pks += ns[i] + ".";
            }
            return pks + "mapper";
        }
        return "";
    }
 
    private static List<MybatisColumnData> createMapperDaoQuery(int importPosition, StringBuffer buffer, Class<?> clz) {
        List<MybatisColumnData> queryColumnData = new ArrayList<>();
 
        buffer.append("public static class DaoQuery{");
        buffer.append("\n\t");
        Field[] fields = clz.getDeclaredFields();
        Set<String> imports = new HashSet<>();
        for (Field fd : fields) {
            String property = fd.getName();
            Annotation[] as = fd.getAnnotations();
            String columnName = fd.getName();
            for (Annotation a : as) {
                if (a instanceof Transient || a instanceof org.springframework.data.annotation.Transient) {
                    property = null;
                    break;
                }
                if (a instanceof Column) {
                    columnName = ((Column) a).name();
                }
            }
 
            String type = null;
            if (property != null) {
                //加
                String genericType = fd.getGenericType().getTypeName();
                if (genericType.indexOf(".") > -1) {
                    imports.add(genericType);
                    type = genericType.split("\\.")[genericType.split("\\.").length - 1];
                } else {
                    type = genericType;
                }
                if (type.equalsIgnoreCase("Date")) {
                    String tempProperty = "min" + property.substring(0, 1).toUpperCase() + property.substring(1);
                    buffer.append(String.format("\tpublic %s %s;", type, tempProperty));
                    queryColumnData.add(new MybatisColumnData(columnName, tempProperty, type));
                    buffer.append("\n\t");
                    tempProperty = "max" + property.substring(0, 1).toUpperCase() + property.substring(1);
                    buffer.append(String.format("\tpublic %s %s;", type, tempProperty));
                    queryColumnData.add(new MybatisColumnData(columnName, tempProperty, type));
                    buffer.append("\n\t");
                } else {
                    buffer.append(String.format("\tpublic %s %s;", type, property));
                    queryColumnData.add(new MybatisColumnData(columnName, property, type));
                    buffer.append("\n\t");
                }
            }
        }
        buffer.append("\tpublic long start;");
        buffer.append("\n\t");
        buffer.append("\tpublic int count;");
        buffer.append("\n\t");
        buffer.append("\tpublic List<String> sortList;");
        buffer.append("\n\t");
        buffer.append("}");
        buffer.append("\n");
 
        imports.add("org.apache.ibatis.annotations.Param");
        for (String im : imports) {
            buffer.insert(importPosition, "\n" + String.format("import %s;", im));
        }
        return queryColumnData;
    }
 
    public static void createMapper(Class<?> clz) {
        // 生成mapper java文件
        String pks = getDaoPackageName(clz);
        StringBuffer buffer = new StringBuffer("package " + pks + ";");
        int importPosition = buffer.length();
        buffer.append("\n\n");
        buffer.append("import " + clz.getName() + ";");
        buffer.append("\n\n");
        buffer.append(String.format("public interface %sMapper extends BaseMapper<%s> {", clz.getSimpleName(), clz.getSimpleName()));
        buffer.append("\n\n\t");
 
        //TODO 确定ID类型
        buffer.append(String.format("%s selectByPrimaryKeyForUpdate(@Param(\"id\") Long id);",clz.getSimpleName()));
        buffer.append("\n\n\t");
 
        buffer.append(String.format("List<%s> list(@Param(\"query\") DaoQuery query);", clz.getSimpleName()));
        buffer.append("\n\n\t");
        buffer.append("long count(@Param(\"query\") DaoQuery query);");
        buffer.append("\n\n\t");
 
        List<MybatisColumnData> queryColumnData = createMapperDaoQuery(importPosition, buffer, clz);
 
 
        buffer.append("}");
        String daoName = String.format("%sMapper.java", clz.getSimpleName());
        String daoPath = basePath + "/dao/" + daoName;
        try {
            if (!new File(basePath + "/dao/").exists())
                new File(basePath + "/dao/").mkdirs();
            if (!new File(daoPath).exists())
                new File(daoPath).createNewFile();
            FileOutputStream fos = new FileOutputStream(new File(daoPath));
            PrintWriter pw = new PrintWriter(fos);
            pw.write(buffer.toString().toCharArray());
            pw.flush();
            pw.close();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        /**
         * 生成Mapper xml文件
         */
        // 获取需要映射的列
        List<AttributeColumnMap> keysList = new ArrayList<>();
        Field[] fields = clz.getDeclaredFields();
        for (Field fd : fields) {
            Annotation[] as = fd.getAnnotations();
            for (Annotation a : as) {
                if (a instanceof Column) {
                    Column c = (Column) a;
                    keysList.add(new AttributeColumnMap(fd.getName(), c.name(), fd.getType().getName()));
                }
            }
        }
        String tableName = "";
        Annotation[] as = clz.getAnnotations();
        for (Annotation a : as) {
            if (a instanceof Table) {
                Table t = (Table) a;
                tableName = t.value();
            }
        }
 
        try {
            String mapperName = String.format("%sMapper.xml", clz.getSimpleName());
            String mapperPath = basePath + "/mapper/" + mapperName;
 
            org.dom4j.Document document = DocumentHelper.createDocument();
            document.addDocType("mapper", "-//mybatis.org//DTD Mapper 3.0//EN",
                    "http://mybatis.org/dtd/mybatis-3-mapper.dtd");
            org.dom4j.Element root = document.addElement("mapper");
            root.addAttribute("namespace", pks + "." + mapperName.replace(".xml", ""));
 
            org.dom4j.Element resultMap = root.addElement("resultMap");
            resultMap.addAttribute("id", "BaseResultMap");
            resultMap.addAttribute("type", clz.getName());
 
            AttributeColumnMap idKeys = getAttributeColumnMapByAttribute("id", keysList);
            if (idKeys != null) {
                org.dom4j.Element id = resultMap.addElement("id");
                id.addAttribute("column", idKeys.column);
                id.addAttribute("property", idKeys.attribute);
                id.addAttribute("jdbcType", ColumnParseUtil.getJDBCType(idKeys.type));
            }
 
            for (AttributeColumnMap key : keysList) {
                if (key.attribute.equalsIgnoreCase(idKeys.attribute) || ColumnParseUtil.getJDBCType(key.type) == null)
                    continue;
                org.dom4j.Element result = resultMap.addElement("result");
                result.addAttribute("column", key.column);
                result.addAttribute("property", key.attribute);
                result.addAttribute("jdbcType", ColumnParseUtil.getJDBCType(key.type));
            }
 
            // 属性值中包含实体
            for (AttributeColumnMap key : keysList) {
                if (ColumnParseUtil.getJDBCType(key.type) == null) {
                    Class<?> propertyClass = Class.forName(key.type);
                    String propertyMapper = getDaoPackageName(propertyClass) + "." + propertyClass.getSimpleName()
                            + "Mapper";
                    org.dom4j.Element association = resultMap.addElement("association");
                    association.addAttribute("property", key.attribute);
                    association.addAttribute("column", key.column);
                    association.addAttribute("resultMap", propertyMapper + ".BaseResultMap");
                    key.attribute = key.attribute + ".id";
                    key.type = "java.lang.Long";
                }
            }
 
            org.dom4j.Element sql = root.addElement("sql");
            sql.addAttribute("id", "Base_Column_List");
            sql.setText(getColumns(keysList));
 
            org.dom4j.Element select = root.addElement("select");
            select.addAttribute("id", "selectByPrimaryKey");
            select.addAttribute("resultMap", "BaseResultMap");
            select.addAttribute("parameterType", "java.lang.Long");
            select.addText("select");
            org.dom4j.Element include = select.addElement("include");
            include.addAttribute("refid", "Base_Column_List");
            select.addText(String.format(" from %s where %s = #{%s,jdbcType=BIGINT}", tableName, idKeys.column,
                    idKeys.attribute));
 
 
            select = root.addElement("select");
            select.addAttribute("id", "selectByPrimaryKeyForUpdate");
            select.addAttribute("resultMap", "BaseResultMap");
            select.addAttribute("parameterType", "java.lang.Long");
            select.addText("select");
            include = select.addElement("include");
            include.addAttribute("refid", "Base_Column_List");
            select.addText(String.format(" from %s where %s = #{%s,jdbcType=BIGINT} for update", tableName, idKeys.column,
                    idKeys.attribute));
 
            //添加sql
            sql = root.addElement("sql");
            sql.addAttribute("id", "listWhereSQL");
 
 
            for (MybatisColumnData columnData : queryColumnData) {
                org.dom4j.Element ife = sql.addElement("if");
                ife.addAttribute("test", String.format("query.%s!=null", columnData.getProperty()));
                if (columnData.getType().equalsIgnoreCase("Date")) {
                    if (columnData.getProperty().startsWith("min"))
                        ife.addText(String.format("AND %s >= #{query.%s}", columnData.getColumn(), columnData.getProperty()));
                    else
                        ife.addText(String.format("AND   #{query.%s} > %s", columnData.getProperty(), columnData.getColumn()));
                } else {
                    ife.addText(String.format("AND %s = #{query.%s}", columnData.getColumn(), columnData.getProperty()));
                }
            }
            //批量查询
            select = root.addElement("select");
            select.addAttribute("id", "list");
            select.addAttribute("resultMap", "BaseResultMap");
            select.addText("select");
            include = select.addElement("include");
            include.addAttribute("refid", "Base_Column_List");
            select.addText(String.format(" from %s where 1=1", tableName));
 
            include = select.addElement("include");
            include.addAttribute("refid", "listWhereSQL");
 
            org.dom4j.Element ife = select.addElement("if");
            ife.addAttribute("test", "query.sortList!=null");
            org.dom4j.Element foreach = ife.addElement("foreach");
            foreach.addAttribute("collection", "query.sortList");
            foreach.addAttribute("item", "item");
            foreach.addAttribute("open", " order by ");
            foreach.addAttribute("separator", ",");
            foreach.addText(" #{item}");
            select.addText("limit #{query.start},#{query.count}");
 
            //批量计数
            select = root.addElement("select");
            select.addAttribute("id", "count");
            select.addAttribute("resultType", "java.lang.Long");
            select.addText(String.format(" select count(*) from %s where 1=1", tableName));
            include = select.addElement("include");
            include.addAttribute("refid", "listWhereSQL");
 
 
            org.dom4j.Element delete = root.addElement("delete");
            delete.addAttribute("id", "deleteByPrimaryKey");
            delete.addAttribute("parameterType", "java.lang.Long");
            delete.setText(String.format("delete from %s where %s = #{%s,jdbcType=BIGINT}", tableName, idKeys.column,
                    idKeys.attribute));
 
            org.dom4j.Element insert = root.addElement("insert");
            insert.addAttribute("id", "insert");
            insert.addAttribute("parameterType", clz.getName());
            insert.addAttribute("useGeneratedKeys", "true");
            insert.addAttribute("keyProperty", "id");
            StringBuffer text = new StringBuffer();
            text.append(String.format("insert into %s (", tableName));
            text.append(getColumns(keysList));
            text.append(")");
            text.append(" values (");
            for (AttributeColumnMap acm : keysList)
                text.append(getKeyPair(acm)).append(",");
            text.deleteCharAt(text.length() - 1);
            text.append(")");
            insert.setText(text.toString());
 
            org.dom4j.Element insertSelective = root.addElement("insert");
            insertSelective.addAttribute("id", "insertSelective");
            insertSelective.addAttribute("parameterType", clz.getName());
            insertSelective.addAttribute("useGeneratedKeys", "true");
            insertSelective.addAttribute("keyProperty", "id");
            insertSelective.addText("insert into " + tableName);
 
            org.dom4j.Element trim = insertSelective.addElement("trim");
            trim.addAttribute("prefix", "(");
            trim.addAttribute("suffix", ")");
            trim.addAttribute("suffixOverrides", ",");
            for (AttributeColumnMap acm : keysList) {
                org.dom4j.Element iff = trim.addElement("if");
                iff.addAttribute("test",
                        (acm.attribute.indexOf(".") > -1 ? acm.attribute.split("\\.")[0] : acm.attribute) + " != null");
                iff.setText(acm.column + ",");
            }
 
            insertSelective.addText("values");
 
            trim = insertSelective.addElement("trim");
            trim.addAttribute("prefix", "(");
            trim.addAttribute("suffix", ")");
            trim.addAttribute("suffixOverrides", ",");
            for (AttributeColumnMap acm : keysList) {
                org.dom4j.Element iff = trim.addElement("if");
                iff.addAttribute("test",
                        (acm.attribute.indexOf(".") > -1 ? acm.attribute.split("\\.")[0] : acm.attribute) + " != null");
                iff.setText(getKeyPair(acm) + ",");
            }
 
            // update
 
            org.dom4j.Element update = root.addElement("update");
            update.addAttribute("id", "updateByPrimaryKey");
            update.addAttribute("parameterType", clz.getName());
            text = new StringBuffer(String.format("update %s set ", tableName));
            for (AttributeColumnMap acm : keysList) {
                if (acm.attribute.equalsIgnoreCase(idKeys.attribute))
                    continue;
                text.append(String.format("%s = #{%s,jdbcType=%s}", acm.column, acm.attribute,
                        ColumnParseUtil.getJDBCType(acm.type))).append(",");
            }
            text.deleteCharAt(text.length() - 1);
 
            text.append(String.format(" where %s = #{%s,jdbcType=%s}", idKeys.column, idKeys.attribute,
                    ColumnParseUtil.getJDBCType(idKeys.type)));
 
            update.setText(text.toString());
 
            // updateSelective
            org.dom4j.Element updateSelective = root.addElement("update");
            updateSelective.addAttribute("id", "updateByPrimaryKeySelective");
            updateSelective.addAttribute("parameterType", clz.getName());
            updateSelective.addText("update " + tableName);
            org.dom4j.Element set = updateSelective.addElement("set");
 
            for (AttributeColumnMap acm : keysList) {
                if (acm.attribute.equalsIgnoreCase(idKeys.attribute))
                    continue;
                org.dom4j.Element iff = set.addElement("if");
                iff.addAttribute("test",
                        (acm.attribute.indexOf(".") > -1 ? acm.attribute.split("\\.")[0] : acm.attribute) + " != null");
                iff.setText(acm.column + "=" + getKeyPair(acm) + ",");
            }
 
            updateSelective.addText(String.format(" where %s = #{%s,jdbcType=%s}", idKeys.column, idKeys.attribute,
                    ColumnParseUtil.getJDBCType(idKeys.type)));
 
            // 创建mapper文件
 
            if (!new File(basePath + "/mapper/").exists())
                new File(basePath + "/mapper/").mkdirs();
            if (!new File(mapperPath).exists())
                new File(mapperPath).createNewFile();
 
            XMLWriter writer = new XMLWriter(new FileOutputStream(new File(mapperPath)),
                    OutputFormat.createPrettyPrint());
            writer.setEscapeText(false);// 字符是否转义,默认true
            writer.write(document);
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    private static AttributeColumnMap getAttributeColumnMapByAttribute(String attributeName,
                                                                       List<AttributeColumnMap> list) {
        for (AttributeColumnMap acm : list)
            if (acm.attribute.equalsIgnoreCase(attributeName))
                return acm;
        return null;
    }
 
    private static String getColumns(List<AttributeColumnMap> list) {
        String columns = "";
        for (AttributeColumnMap map : list)
            columns += map.column + ",";
        return columns.length() > 0 ? columns.substring(0, columns.length() - 1) : columns;
    }
 
    private static String getKeyPair(AttributeColumnMap map) {
        return String.format("#{%s,jdbcType=%s}", map.attribute, ColumnParseUtil.getJDBCType(map.type));
    }
 
}
 
class AttributeColumnMap {
    String attribute;
    String column;
    String type;
 
    public AttributeColumnMap(String attribute, String column, String type) {
        this.attribute = attribute;
        this.column = column;
        this.type = type;
    }
 
    public AttributeColumnMap() {
 
    }
}