提交 | 用户 | age
|
826b66
|
1 |
package com.hx.auto.manage.xml.scan.util; |
C |
2 |
|
|
3 |
import com.gitee.sunchenbin.mybatis.actable.annotation.Column; |
|
4 |
import com.gitee.sunchenbin.mybatis.actable.annotation.Table; |
|
5 |
import com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant; |
|
6 |
import com.hx.auto.GeneratorMapperUtil; |
|
7 |
import com.hx.auto.GeneratorReadXmlUtil; |
|
8 |
import com.hx.auto.common.JdbcType; |
|
9 |
import com.hx.auto.common.ReadEntityData; |
|
10 |
import com.hx.auto.util.CommonTool; |
|
11 |
import com.hx.auto.util.GeneratorClassParentUtil; |
|
12 |
import freemarker.template.Configuration; |
|
13 |
import freemarker.template.DefaultObjectWrapper; |
|
14 |
import freemarker.template.Template; |
|
15 |
|
|
16 |
import java.io.*; |
|
17 |
import java.lang.reflect.Field; |
|
18 |
import java.util.HashMap; |
|
19 |
|
|
20 |
/** |
|
21 |
* 自动生成mapper.xml |
|
22 |
* |
|
23 |
* @author chenjiahe 2019年09月08日23:57:47 |
|
24 |
* |
|
25 |
*/ |
|
26 |
public class CreateMapperUtil { |
|
27 |
|
|
28 |
private static String templateDir = "/ftl";//获取模板路径 |
|
29 |
private static String templateName = "Mapper.tpl";//action模板名称 |
|
30 |
|
|
31 |
/**生成mapper.xml |
|
32 |
* @param cl 实体类 |
|
33 |
* @param configUtil 生成配置信息 |
|
34 |
* @throws Exception |
|
35 |
*/ |
|
36 |
public static void generatorMapper(Class<?> cl, ConfigUtil configUtil) throws Exception { |
|
37 |
try { |
|
38 |
|
|
39 |
// 反射start |
|
40 |
// 类名 |
|
41 |
String entityName = cl.getSimpleName(); |
|
42 |
|
|
43 |
//生成文件路径 |
|
44 |
String targetFile = configUtil.getXmlUrl().replace(".", "/")+"/"; |
|
45 |
//生成文件名称 |
|
46 |
targetFile += entityName + "Mapper.xml"; |
|
47 |
//补全路径 |
|
48 |
targetFile = "./"+configUtil.getPackFileName().replace(".", "/")+"/"+targetFile; |
|
49 |
//获取是否已经生成过文件,拿取自定义的内容 |
|
50 |
String xmlData = GeneratorReadXmlUtil.readMapperXml(targetFile); |
|
51 |
|
|
52 |
//如果不存在就直接跳过 |
|
53 |
if("false".equals(xmlData)){ |
|
54 |
return; |
|
55 |
} |
|
56 |
|
|
57 |
// 类名首字母小写 |
|
58 |
String initial = entityName.substring(0, 1).toLowerCase(); |
|
59 |
String entityNameSmall = initial + entityName.substring(1, entityName.length()); |
|
60 |
|
|
61 |
//获取实体类包名 |
|
62 |
String[] strs = cl.getName().split("\\."); |
|
63 |
String packageName = ""; |
|
64 |
//去掉类名 |
|
65 |
for(int i=0;i<strs.length-1;i++) { |
|
66 |
packageName += "."+strs[i]; |
|
67 |
} |
|
68 |
packageName = packageName.replaceFirst(".", ""); |
|
69 |
|
|
70 |
//获取模板 |
|
71 |
Configuration configuration = new Configuration(Configuration.getVersion()); |
|
72 |
configuration.setClassForTemplateLoading(GeneratorMapperUtil.class, templateDir); |
|
73 |
// 获取或创建模板 |
|
74 |
Template template = configuration.getTemplate(templateName); |
|
75 |
|
|
76 |
//实体类所有的字段 |
|
77 |
//用来存储数据 |
|
78 |
ReadEntityData readEntityData = new ReadEntityData(); |
|
79 |
//表名称 |
|
80 |
Table table = cl.getAnnotation(Table.class); |
|
81 |
readEntityData.setTableName(table.name()); |
|
82 |
// 取得本类的全部属性 |
|
83 |
Field[] fields = cl.getDeclaredFields(); |
|
84 |
fields = GeneratorClassParentUtil.getPatentFields(fields,cl); |
|
85 |
for (Field field:fields) { |
|
86 |
// 判断方法中是否有指定注解类型的注解 |
|
87 |
boolean hasAnnotation = field.isAnnotationPresent(Column.class); |
|
88 |
if (hasAnnotation) { |
|
89 |
// 根据注解类型返回方法的指定类型注解 |
|
90 |
Column column = field.getAnnotation(Column.class); |
|
91 |
|
|
92 |
//判断是不是 |
|
93 |
boolean isBol = false; |
|
94 |
Integer isBlob = 0; |
|
95 |
if(column.type().equals(MySqlTypeConstant.TEXT)||column.type().equals(MySqlTypeConstant.LONGTEXT) |
|
96 |
||column.type().equals(MySqlTypeConstant.LONGBLOB)){ |
|
97 |
isBol = true; |
|
98 |
isBlob = 1; |
|
99 |
} |
|
100 |
|
|
101 |
//类型 |
|
102 |
//String type = column.type().toUpperCase(); |
|
103 |
String type = JdbcType.jdbcTypeData(field.getType().getTypeName().toUpperCase(),isBol); |
|
104 |
//主键 |
|
105 |
if (column.isKey()) { |
|
106 |
readEntityData.setEntityIdName(field.getName()); |
|
107 |
readEntityData.setEntityIdData("#{"+field.getName()+"}"); |
|
108 |
if(!CommonTool.checkNotNull(column.name())) { |
|
109 |
readEntityData.setTableIdName(field.getName()); |
|
110 |
}else { |
|
111 |
readEntityData.setTableIdName(column.name()); |
|
112 |
} |
|
113 |
readEntityData.setKeyType(type); |
|
114 |
continue; |
|
115 |
} |
|
116 |
//存储数据 |
|
117 |
if(!CommonTool.checkNotNull(column.name())) { |
|
118 |
readEntityData.fielData(field.getName(),field.getName(),type,isBlob,"#{"+field.getName()+"}"); |
|
119 |
}else { |
|
120 |
readEntityData.fielData(field.getName(),column.name(),type,isBlob,"#{"+field.getName()+"}"); |
|
121 |
} |
|
122 |
} |
|
123 |
} |
|
124 |
// 创建数据模型 |
|
125 |
HashMap<String, Object> root = new HashMap<String, Object>(); |
|
126 |
|
|
127 |
root.put("packageEntityName",packageName); |
|
128 |
//实体类的类名 |
|
129 |
root.put("entityName", entityName); |
|
130 |
//实体类的类名(首字母小写) |
|
131 |
root.put("entityNameSmall", entityNameSmall); |
|
132 |
//表字段 数据 |
|
133 |
root.put("fieldData",readEntityData); |
|
134 |
|
|
135 |
root.put("sqlSentence","${sqlSentence}"); |
|
136 |
|
|
137 |
root.put("DAOPackageName",configUtil.getDaoUrl()); |
|
138 |
//实体类的包名 |
|
139 |
root.put("entityPackageName",packageName); |
|
140 |
|
|
141 |
//获取是否已经生成过文件,拿取自定义的内容 |
|
142 |
root.put("customData",xmlData); |
|
143 |
|
|
144 |
System.out.println("mapperUrl:"+targetFile); |
|
145 |
// 将模板和数据模型合并 输出到Console |
|
146 |
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(targetFile), "UTF-8")); |
|
147 |
template.process(root, out); |
|
148 |
out.flush(); |
|
149 |
out.close(); |
|
150 |
} catch (IOException e) { |
|
151 |
e.printStackTrace(); |
|
152 |
} |
|
153 |
} |
|
154 |
} |