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