提交 | 用户 | 时间
|
fd2207
|
1 |
package com.jcdm.generator.util; |
懒 |
2 |
|
|
3 |
import java.util.Arrays; |
|
4 |
import org.apache.commons.lang3.RegExUtils; |
|
5 |
import com.jcdm.common.constant.GenConstants; |
|
6 |
import com.jcdm.common.utils.StringUtils; |
|
7 |
import com.jcdm.generator.config.GenConfig; |
|
8 |
import com.jcdm.generator.domain.GenTable; |
|
9 |
import com.jcdm.generator.domain.GenTableColumn; |
|
10 |
|
|
11 |
/** |
|
12 |
* 代码生成器 工具类 |
|
13 |
* |
|
14 |
* @author jc |
|
15 |
*/ |
|
16 |
public class GenUtils |
|
17 |
{ |
|
18 |
/** |
|
19 |
* 初始化表信息 |
|
20 |
*/ |
|
21 |
public static void initTable(GenTable genTable, String operName) |
|
22 |
{ |
|
23 |
genTable.setClassName(convertClassName(genTable.getTableName())); |
|
24 |
genTable.setPackageName(GenConfig.getPackageName()); |
|
25 |
genTable.setModuleName(getModuleName(GenConfig.getPackageName())); |
|
26 |
genTable.setBusinessName(getBusinessName(genTable.getTableName())); |
|
27 |
genTable.setFunctionName(replaceText(genTable.getTableComment())); |
|
28 |
genTable.setFunctionAuthor(GenConfig.getAuthor()); |
|
29 |
genTable.setCreateBy(operName); |
|
30 |
} |
|
31 |
|
|
32 |
/** |
|
33 |
* 初始化列属性字段 |
|
34 |
*/ |
|
35 |
public static void initColumnField(GenTableColumn column, GenTable table) |
|
36 |
{ |
|
37 |
String dataType = getDbType(column.getColumnType()); |
|
38 |
String columnName = column.getColumnName(); |
|
39 |
column.setTableId(table.getTableId()); |
|
40 |
column.setCreateBy(table.getCreateBy()); |
|
41 |
// 设置java字段名 |
|
42 |
column.setJavaField(StringUtils.toCamelCase(columnName)); |
|
43 |
// 设置默认类型 |
|
44 |
column.setJavaType(GenConstants.TYPE_STRING); |
|
45 |
column.setQueryType(GenConstants.QUERY_EQ); |
|
46 |
|
|
47 |
if (arraysContains(GenConstants.COLUMNTYPE_STR, dataType) || arraysContains(GenConstants.COLUMNTYPE_TEXT, dataType)) |
|
48 |
{ |
|
49 |
// 字符串长度超过500设置为文本域 |
|
50 |
Integer columnLength = getColumnLength(column.getColumnType()); |
|
51 |
String htmlType = columnLength >= 500 || arraysContains(GenConstants.COLUMNTYPE_TEXT, dataType) ? GenConstants.HTML_TEXTAREA : GenConstants.HTML_INPUT; |
|
52 |
column.setHtmlType(htmlType); |
|
53 |
} |
|
54 |
else if (arraysContains(GenConstants.COLUMNTYPE_TIME, dataType)) |
|
55 |
{ |
|
56 |
column.setJavaType(GenConstants.TYPE_DATE); |
|
57 |
column.setHtmlType(GenConstants.HTML_DATETIME); |
|
58 |
} |
|
59 |
else if (arraysContains(GenConstants.COLUMNTYPE_NUMBER, dataType)) |
|
60 |
{ |
|
61 |
column.setHtmlType(GenConstants.HTML_INPUT); |
|
62 |
|
|
63 |
// 如果是浮点型 统一用BigDecimal |
|
64 |
String[] str = StringUtils.split(StringUtils.substringBetween(column.getColumnType(), "(", ")"), ","); |
|
65 |
if (str != null && str.length == 2 && Integer.parseInt(str[1]) > 0) |
|
66 |
{ |
|
67 |
column.setJavaType(GenConstants.TYPE_BIGDECIMAL); |
|
68 |
} |
|
69 |
// 如果是整形 |
|
70 |
else if (str != null && str.length == 1 && Integer.parseInt(str[0]) <= 10) |
|
71 |
{ |
|
72 |
column.setJavaType(GenConstants.TYPE_INTEGER); |
|
73 |
} |
|
74 |
// 长整形 |
|
75 |
else |
|
76 |
{ |
|
77 |
column.setJavaType(GenConstants.TYPE_LONG); |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
// 插入字段(默认所有字段都需要插入) |
|
82 |
column.setIsInsert(GenConstants.REQUIRE); |
|
83 |
|
|
84 |
// 编辑字段 |
|
85 |
if (!arraysContains(GenConstants.COLUMNNAME_NOT_EDIT, columnName) && !column.isPk()) |
|
86 |
{ |
|
87 |
column.setIsEdit(GenConstants.REQUIRE); |
|
88 |
} |
|
89 |
// 列表字段 |
|
90 |
if (!arraysContains(GenConstants.COLUMNNAME_NOT_LIST, columnName) && !column.isPk()) |
|
91 |
{ |
|
92 |
column.setIsList(GenConstants.REQUIRE); |
|
93 |
} |
|
94 |
// 查询字段 |
|
95 |
if (!arraysContains(GenConstants.COLUMNNAME_NOT_QUERY, columnName) && !column.isPk()) |
|
96 |
{ |
|
97 |
column.setIsQuery(GenConstants.REQUIRE); |
|
98 |
} |
|
99 |
|
|
100 |
// 查询字段类型 |
|
101 |
if (StringUtils.endsWithIgnoreCase(columnName, "name")) |
|
102 |
{ |
|
103 |
column.setQueryType(GenConstants.QUERY_LIKE); |
|
104 |
} |
|
105 |
// 状态字段设置单选框 |
|
106 |
if (StringUtils.endsWithIgnoreCase(columnName, "status")) |
|
107 |
{ |
|
108 |
column.setHtmlType(GenConstants.HTML_RADIO); |
|
109 |
} |
|
110 |
// 类型&性别字段设置下拉框 |
|
111 |
else if (StringUtils.endsWithIgnoreCase(columnName, "type") |
|
112 |
|| StringUtils.endsWithIgnoreCase(columnName, "sex")) |
|
113 |
{ |
|
114 |
column.setHtmlType(GenConstants.HTML_SELECT); |
|
115 |
} |
|
116 |
// 图片字段设置图片上传控件 |
|
117 |
else if (StringUtils.endsWithIgnoreCase(columnName, "image")) |
|
118 |
{ |
|
119 |
column.setHtmlType(GenConstants.HTML_IMAGE_UPLOAD); |
|
120 |
} |
|
121 |
// 文件字段设置文件上传控件 |
|
122 |
else if (StringUtils.endsWithIgnoreCase(columnName, "file")) |
|
123 |
{ |
|
124 |
column.setHtmlType(GenConstants.HTML_FILE_UPLOAD); |
|
125 |
} |
|
126 |
// 内容字段设置富文本控件 |
|
127 |
else if (StringUtils.endsWithIgnoreCase(columnName, "content")) |
|
128 |
{ |
|
129 |
column.setHtmlType(GenConstants.HTML_EDITOR); |
|
130 |
} |
|
131 |
} |
|
132 |
|
|
133 |
/** |
|
134 |
* 校验数组是否包含指定值 |
|
135 |
* |
|
136 |
* @param arr 数组 |
|
137 |
* @param targetValue 值 |
|
138 |
* @return 是否包含 |
|
139 |
*/ |
|
140 |
public static boolean arraysContains(String[] arr, String targetValue) |
|
141 |
{ |
|
142 |
return Arrays.asList(arr).contains(targetValue); |
|
143 |
} |
|
144 |
|
|
145 |
/** |
|
146 |
* 获取模块名 |
|
147 |
* |
|
148 |
* @param packageName 包名 |
|
149 |
* @return 模块名 |
|
150 |
*/ |
|
151 |
public static String getModuleName(String packageName) |
|
152 |
{ |
|
153 |
int lastIndex = packageName.lastIndexOf("."); |
|
154 |
int nameLength = packageName.length(); |
|
155 |
return StringUtils.substring(packageName, lastIndex + 1, nameLength); |
|
156 |
} |
|
157 |
|
|
158 |
/** |
|
159 |
* 获取业务名 |
|
160 |
* |
|
161 |
* @param tableName 表名 |
|
162 |
* @return 业务名 |
|
163 |
*/ |
|
164 |
public static String getBusinessName(String tableName) |
|
165 |
{ |
|
166 |
int lastIndex = tableName.lastIndexOf("_"); |
|
167 |
int nameLength = tableName.length(); |
|
168 |
return StringUtils.substring(tableName, lastIndex + 1, nameLength); |
|
169 |
} |
|
170 |
|
|
171 |
/** |
|
172 |
* 表名转换成Java类名 |
|
173 |
* |
|
174 |
* @param tableName 表名称 |
|
175 |
* @return 类名 |
|
176 |
*/ |
|
177 |
public static String convertClassName(String tableName) |
|
178 |
{ |
|
179 |
boolean autoRemovePre = GenConfig.getAutoRemovePre(); |
|
180 |
String tablePrefix = GenConfig.getTablePrefix(); |
|
181 |
if (autoRemovePre && StringUtils.isNotEmpty(tablePrefix)) |
|
182 |
{ |
|
183 |
String[] searchList = StringUtils.split(tablePrefix, ","); |
|
184 |
tableName = replaceFirst(tableName, searchList); |
|
185 |
} |
|
186 |
return StringUtils.convertToCamelCase(tableName); |
|
187 |
} |
|
188 |
|
|
189 |
/** |
|
190 |
* 批量替换前缀 |
|
191 |
* |
|
192 |
* @param replacementm 替换值 |
|
193 |
* @param searchList 替换列表 |
|
194 |
* @return |
|
195 |
*/ |
|
196 |
public static String replaceFirst(String replacementm, String[] searchList) |
|
197 |
{ |
|
198 |
String text = replacementm; |
|
199 |
for (String searchString : searchList) |
|
200 |
{ |
|
201 |
if (replacementm.startsWith(searchString)) |
|
202 |
{ |
|
203 |
text = replacementm.replaceFirst(searchString, ""); |
|
204 |
break; |
|
205 |
} |
|
206 |
} |
|
207 |
return text; |
|
208 |
} |
|
209 |
|
|
210 |
/** |
|
211 |
* 关键字替换 |
|
212 |
* |
|
213 |
* @param text 需要被替换的名字 |
|
214 |
* @return 替换后的名字 |
|
215 |
*/ |
|
216 |
public static String replaceText(String text) |
|
217 |
{ |
|
218 |
return RegExUtils.replaceAll(text, "(?:表|若依)", ""); |
|
219 |
} |
|
220 |
|
|
221 |
/** |
|
222 |
* 获取数据库类型字段 |
|
223 |
* |
|
224 |
* @param columnType 列类型 |
|
225 |
* @return 截取后的列类型 |
|
226 |
*/ |
|
227 |
public static String getDbType(String columnType) |
|
228 |
{ |
|
229 |
if (StringUtils.indexOf(columnType, "(") > 0) |
|
230 |
{ |
|
231 |
return StringUtils.substringBefore(columnType, "("); |
|
232 |
} |
|
233 |
else |
|
234 |
{ |
|
235 |
return columnType; |
|
236 |
} |
|
237 |
} |
|
238 |
|
|
239 |
/** |
|
240 |
* 获取字段长度 |
|
241 |
* |
|
242 |
* @param columnType 列类型 |
|
243 |
* @return 截取后的列类型 |
|
244 |
*/ |
|
245 |
public static Integer getColumnLength(String columnType) |
|
246 |
{ |
|
247 |
if (StringUtils.indexOf(columnType, "(") > 0) |
|
248 |
{ |
|
249 |
String length = StringUtils.substringBetween(columnType, "(", ")"); |
|
250 |
return Integer.valueOf(length); |
|
251 |
} |
|
252 |
else |
|
253 |
{ |
|
254 |
return 0; |
|
255 |
} |
|
256 |
} |
|
257 |
} |