admin
2021-06-29 9577602ea5616f34688f57ce9fd4d601ee3502d8
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
package org.yeshi.utils.generater.mybatis;
 
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.List;
 
import org.dom4j.DocumentHelper;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
 
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 "";
    }
 
    public static void createMapper(Class<?> clz) {
        // 生成mapper java文件
        String pks = getDaoPackageName(clz);
        StringBuffer buffer = new StringBuffer("package " + pks + ";");
        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");
        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));
 
 
 
 
            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() {
 
    }
}