GenUtils.java
6.61 KB
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
package com.huaheng.pc.tool.gen.util;
import java.util.ArrayList;
import java.util.List;
import org.apache.velocity.VelocityContext;
import com.huaheng.common.constant.Constants;
import com.huaheng.common.constant.CommonMap;
import com.huaheng.common.utils.DateUtils;
import com.huaheng.common.utils.StringUtils;
import com.huaheng.framework.config.GenConfig;
import com.huaheng.pc.tool.gen.domain.ColumnInfo;
import com.huaheng.pc.tool.gen.domain.TableInfo;
/**
* 代码生成器 工具类
*
* @author huaheng
*/
public class GenUtils
{
/** 项目空间路径 */
private static final String PROJECT_PATH = "main/java/com/huaheng/pc";
/** mybatis空间路径 */
private static final String MYBATIS_PATH = "main/resources/mybatis";
/** html空间路径 */
private static final String TEMPLATES_PATH = "main/resources/templates";
/**
* 设置列信息
*/
public static List<ColumnInfo> transColums(List<ColumnInfo> columns)
{
// 列信息
List<ColumnInfo> columsList = new ArrayList<>();
for (ColumnInfo column : columns)
{
// 列名转换成Java属性名
String attrName = StringUtils.convertToCamelCase(column.getColumnName());
column.setAttrName(attrName);
column.setAttrname(StringUtils.uncapitalize(attrName));
// 列的数据类型,转换成Java类型
String attrType = CommonMap.javaTypeMap.get(column.getDataType());
column.setAttrType(attrType);
columsList.add(column);
}
return columsList;
}
/**
* 获取模板信息
*
* @return 模板列表
*/
public static VelocityContext getVelocityContext(TableInfo table)
{
// java对象数据传递到模板文件vm
VelocityContext velocityContext = new VelocityContext();
String packageName = GenConfig.getPackageName();
velocityContext.put("tableName", table.getTableName());
velocityContext.put("tableComment", replaceKeyword(table.getTableComment()));
velocityContext.put("primaryKey", table.getPrimaryKey());
velocityContext.put("className", table.getClassName());
velocityContext.put("classname", table.getClassname());
velocityContext.put("moduleName", GenUtils.getModuleName(packageName));
velocityContext.put("columns", table.getColumns());
velocityContext.put("package", packageName + "." + table.getClassname());
velocityContext.put("author", GenConfig.getAuthor());
velocityContext.put("datetime", DateUtils.getDate());
return velocityContext;
}
/**
* 获取模板信息
*
* @return 模板列表
*/
public static List<String> getTemplates()
{
List<String> templates = new ArrayList<String>();
templates.add("templates/vm/java/domain.java.vm");
templates.add("templates/vm/java/Mapper.java.vm");
templates.add("templates/vm/java/Service.java.vm");
templates.add("templates/vm/java/ServiceImpl.java.vm");
templates.add("templates/vm/java/Controller.java.vm");
templates.add("templates/vm/xml/Mapper.xml.vm");
templates.add("templates/vm/html/list.html.vm");
templates.add("templates/vm/html/add.html.vm");
templates.add("templates/vm/html/edit.html.vm");
templates.add("templates/vm/sql/sql.vm");
return templates;
}
/**
* 表名转换成Java类名
*/
public static String tableToJava(String tableName)
{
if (Constants.AUTO_REOMVE_PRE.equals(GenConfig.getAutoRemovePre()))
{
tableName = tableName.substring(tableName.indexOf("_") + 1);
}
if (StringUtils.isNotEmpty(GenConfig.getTablePrefix()))
{
tableName = tableName.replace(GenConfig.getTablePrefix(), "");
}
return StringUtils.convertToCamelCase(tableName);
}
/**
* 获取文件名
*/
public static String getFileName(String template, TableInfo table, String moduleName)
{
// 小写类名
String classname = table.getClassname();
// 大写类名
String className = table.getClassName();
String javaPath = PROJECT_PATH + "/" + moduleName + "/";
String mybatisPath = MYBATIS_PATH + "/" + moduleName + "/" + className;
String htmlPath = TEMPLATES_PATH + "/" + moduleName + "/" + classname;
if (StringUtils.isNotEmpty(classname))
{
javaPath += classname.replace(".", "/") + "/";
}
if (template.contains("domain.java.vm"))
{
return javaPath + "domain" + "/" + className + ".java";
}
if (template.contains("mapper.java.vm"))
{
return javaPath + "mapper" + "/" + className + "mapper.java";
}
if (template.contains("Service.java.vm"))
{
return javaPath + "service" + "/" + "I" + className + "Service.java";
}
if (template.contains("ServiceImpl.java.vm"))
{
return javaPath + "service" + "/" + className + "ServiceImpl.java";
}
if (template.contains("Controller.java.vm"))
{
return javaPath + "controller" + "/" + className + "Controller.java";
}
if (template.contains("mapper.xml.vm"))
{
return mybatisPath + "mapper.xml";
}
if (template.contains("list.html.vm"))
{
return htmlPath + "/" + classname + ".html";
}
if (template.contains("add.html.vm"))
{
return htmlPath + "/" + "add.html";
}
if (template.contains("edit.html.vm"))
{
return htmlPath + "/" + "edit.html";
}
if (template.contains("sql.vm"))
{
return classname + "Menu.sql";
}
return null;
}
/**
* 获取模块名
*
* @param packageName 包名
* @return 模块名
*/
public static String getModuleName(String packageName)
{
int lastIndex = packageName.lastIndexOf(".");
int nameLength = packageName.length();
String moduleName = StringUtils.substring(packageName, lastIndex + 1, nameLength);
return moduleName;
}
public static String replaceKeyword(String keyword)
{
String keyName = keyword.replaceAll("(?:表|信息)", "");
return keyName;
}
public static void main(String[] args)
{
System.out.println(StringUtils.convertToCamelCase("userName"));
System.out.println(replaceKeyword("岗位信息表"));
System.out.println(getModuleName("com.huaheng.pc.system"));
}
}