Skip to content

Commit 1eca386

Browse files
committed
update: 减少FieldInfo所需字段 优化生成
1 parent b562c94 commit 1eca386

19 files changed

+158
-907
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ target/
1111
*.iws
1212
*.iml
1313
*.ipr
14+
.idea
1415

1516
### Eclipse ###
1617
.apt_generated

src/main/java/cn/devcxl/generator/CodeGenerator.java

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,43 @@ public static void main(String[] args) {
2929
Configuration configuration = new Configuration();
3030
configuration.setAuthor("devcxl");
3131
configuration.setPackageName("cn.devcxl.code");
32+
configuration.setPrimaryKeyFieldType(FieldType.BIGINT);
3233
configuration.setTablePrefix("cms_");
3334
configuration.setProjectSrcPath("/home/devcxl/IdeaProjects/code-gen-template/src/");
3435

3536
List<FieldInfo> fieldInfos = new ArrayList<>();
3637

37-
FieldInfo idField = new FieldInfo("id", "用户ID", FieldType.INTEGER, 11, "", true, true, true, false, false, false, true, EQ, "input", "");
38-
FieldInfo usernameField = new FieldInfo("userName", "用户名", FieldType.VARCHAR, 30, "", false, false, true, true, true, true, true, LIKE, "input", "");
39-
FieldInfo passwordField = new FieldInfo("password", "密码", FieldType.VARCHAR, 64, "", false, false, true, false, true, false, false, NONE, "input", "");
40-
FieldInfo emailField = new FieldInfo("email", "邮箱", FieldType.VARCHAR, 30, "", false, false, false, true, true, true, true, LIKE, "input", "");
41-
FieldInfo ageField = new FieldInfo("age", "年龄", FieldType.INTEGER, 3, "", false, false, false, true, true, true, true, EQ, "input", "");
42-
FieldInfo genderField = new FieldInfo("gender", "性别", FieldType.TINYINT, 1, "", false, false, false, true, true, true, true, EQ, "select", "genderDict");
43-
FieldInfo birthdayField = new FieldInfo("birthday", "生日", FieldType.DATE, 0, "", false, false, false, true, true, true, true, BETWEEN, "datetime", "");
44-
FieldInfo createAt = new FieldInfo("createAt", "创建时间", FieldType.TIMESTAMP, 0, "CURRENT_TIMESTAMP", false, false, true, false, false, false, true, BETWEEN, "datetime", "");
45-
FieldInfo updateAt = new FieldInfo("updateAt", "更新时间", FieldType.TIMESTAMP, 0, "CURRENT_TIMESTAMP", false, false, true, false, false, false, false, BETWEEN, "datetime", "");
38+
FieldInfo idField = new FieldInfo("id", "用户ID", FieldType.BIGINT, 11,
39+
true, "", "auto_increment primary key",
40+
true, true, true, EQ);
41+
42+
FieldInfo createAt = new FieldInfo("createTime", "创建时间", FieldType.LOCALDATETIME, 0,
43+
true, "CURRENT_TIMESTAMP", "",
44+
false, false, false, BETWEEN);
45+
46+
FieldInfo updateAt = new FieldInfo("updateTime", "更新时间", FieldType.LOCALDATETIME, 0,
47+
true, "CURRENT_TIMESTAMP", "on update CURRENT_TIMESTAMP",
48+
true, false, false, BETWEEN);
49+
50+
51+
FieldInfo usernameField = new FieldInfo("username", "用户名", FieldType.VARCHAR, 30,
52+
true, "", "", true, true, true, LIKE);
53+
54+
FieldInfo passwordField = new FieldInfo("password", "密码", FieldType.VARCHAR, 64,
55+
true, "", "", true, true, false, NONE);
56+
57+
FieldInfo emailField = new FieldInfo("email", "邮箱", FieldType.VARCHAR, 30,
58+
true, "", "",
59+
true, true, true, LIKE);
60+
61+
FieldInfo ageField = new FieldInfo("age", "年龄", FieldType.INTEGER, 3,
62+
false, "", "", true, true, true, EQ);
63+
FieldInfo genderField = new FieldInfo("gender", "性别", FieldType.TINYINT, 1,
64+
false, "", "", true, true, true, EQ);
65+
66+
FieldInfo birthdayField = new FieldInfo("birthday", "生日", FieldType.LOCALDATETIME, 0,
67+
false, "", "", true, true, true, BETWEEN);
68+
4669

4770
fieldInfos.add(idField);
4871
fieldInfos.add(usernameField);

src/main/java/cn/devcxl/generator/domain/Configuration.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.devcxl.generator.domain;
22

3+
import cn.devcxl.generator.enums.FieldType;
34
import lombok.Getter;
45
import lombok.Setter;
56

@@ -22,6 +23,10 @@ public class Configuration {
2223
*/
2324
private String projectSrcPath;
2425

26+
/**
27+
* 主键类型
28+
*/
29+
private FieldType primaryKeyFieldType;
2530
/**
2631
* 包名
2732
**/

src/main/java/cn/devcxl/generator/domain/EntityInfo.java

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import lombok.Getter;
66
import lombok.Setter;
77

8-
import java.text.MessageFormat;
98
import java.util.List;
109
import java.util.Objects;
1110
import java.util.stream.Collectors;
@@ -75,7 +74,9 @@ public EntityInfo(Configuration configuration, String name, String className, St
7574
* @return
7675
*/
7776
public List<FieldInfo> requiredFields() {
78-
return this.fields.stream().filter(FieldInfo::isRequired).collect(Collectors.toList());
77+
return this.fields.stream().filter(fieldInfo -> {
78+
return fieldInfo.isRequired() && !GeneratorUtils.containsString(GeneratorConstant.INSERT_EXCLUDE_FIELDS, fieldInfo.getName());
79+
}).collect(Collectors.toList());
7980
}
8081

8182
/**
@@ -117,40 +118,4 @@ public List<FieldInfo> queryFields() {
117118
return this.fields.stream().filter(FieldInfo::isQuery).collect(Collectors.toList());
118119
}
119120

120-
121-
/**
122-
* 获取主键字段
123-
*
124-
* @return
125-
*/
126-
public List<FieldInfo> primaryKeyFields() {
127-
return this.fields.stream().filter(FieldInfo::isPrimaryKey).collect(Collectors.toList());
128-
}
129-
130-
/**
131-
* 获取非主键字段
132-
*
133-
* @return
134-
*/
135-
public List<FieldInfo> noPrimaryKeyFields() {
136-
return this.fields.stream().filter(fieldInfo -> !fieldInfo.isPrimaryKey()).collect(Collectors.toList());
137-
}
138-
139-
140-
/**
141-
* 获取主键字段
142-
*
143-
* @return
144-
*/
145-
public FieldInfo primaryKeyField() {
146-
List<FieldInfo> fieldInfos = this.primaryKeyFields();
147-
int size = fieldInfos.size();
148-
if (fieldInfos.size() == 1) {
149-
return fieldInfos.get(0);
150-
} else {
151-
throw new RuntimeException(MessageFormat.format("primaryKeyFields count {0}", size));
152-
}
153-
}
154-
155-
156121
}

src/main/java/cn/devcxl/generator/domain/FieldInfo.java

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,18 @@ public class FieldInfo {
4848
/**
4949
* sql默认值
5050
*/
51-
private String defaultValue="";
51+
private String defaultValue = "";
5252

5353
/**
54-
* 是否主键
54+
* sql扩展
5555
*/
56-
private boolean isPrimaryKey;
56+
private String extra = "";
5757

58-
/**
59-
* 是否自增
60-
*/
61-
private boolean isIncrement;
6258

6359
/**
6460
* 是否必填
6561
*/
66-
private boolean isRequired;
62+
private boolean required;
6763

6864

6965
/**
@@ -91,18 +87,10 @@ public class FieldInfo {
9187
*/
9288
private QueryType queryType;
9389

94-
/**
95-
* 显示类型(input文本框、textarea文本域、select下拉框、checkbox复选框、radio单选框、datetime日期控件、upload上传控件)
96-
*/
97-
private String htmlType;
98-
99-
/**
100-
* 字典类型
101-
*/
102-
private String dictType;
103-
10490

105-
public FieldInfo(String name, String comment, FieldType fieldType, Integer fieldLength, String defaultValue, boolean isPk, boolean isIncrement, boolean isRequired, boolean isInsert, boolean isEdit, boolean isList, boolean isQuery, QueryType queryType, String htmlType, String dictType) {
91+
public FieldInfo(String name, String comment, FieldType fieldType, Integer fieldLength,
92+
boolean required, String defaultValue, String extra,
93+
boolean isInsert, boolean isEdit, boolean isList, QueryType queryType) {
10694
this.name = name;
10795
this.javaField = GeneratorUtils.toLowerCaseCamelCase(this.name);
10896
this.sqlField = GeneratorUtils.toSnakeCase(this.name);
@@ -111,16 +99,13 @@ public FieldInfo(String name, String comment, FieldType fieldType, Integer field
11199
this.fieldType = fieldType;
112100
this.fieldLength = fieldLength;
113101
this.defaultValue = defaultValue;
102+
this.extra = extra;
114103

115-
this.isPrimaryKey = isPk;
116-
this.isIncrement = isIncrement;
117-
this.isRequired = isRequired;
104+
this.required = required;
118105
this.isInsert = isInsert;
119106
this.isEdit = isEdit;
120107
this.isList = isList;
121-
this.isQuery = isQuery;
108+
this.isQuery = queryType != QueryType.NONE;
122109
this.queryType = queryType;
123-
this.htmlType = htmlType;
124-
this.dictType = dictType;
125110
}
126111
}

src/main/java/cn/devcxl/generator/enums/FieldType.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.devcxl.generator.enums;
22

3+
34
/**
45
* 字段类型
56
*
@@ -18,6 +19,7 @@ public enum FieldType {
1819
VARCHAR("java.lang.String", "varchar", "string"),
1920
TEXT("java.lang.String", "text", "string"),
2021
DATE("java.util.Date", "date", "Date"),
22+
LOCALDATETIME("java.time.LocalDateTime", "datetime", "Date"),
2123
TIME("java.sql.Time", "time", "Date"),
2224
TIMESTAMP("java.util.Date", "timestamp", "Date");
2325
/**

src/main/java/cn/devcxl/generator/utils/VelocityUtils.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static VelocityContext prepareContext(Configuration configuration, Entity
2626
VelocityContext velocityContext = new VelocityContext();
2727

2828
velocityContext.put("deletedFlag", configuration.isDeletedFlag());
29+
velocityContext.put("primaryKeyFieldType", configuration.getPrimaryKeyFieldType());
2930

3031
// base
3132
velocityContext.put("entity", entityInfo);
@@ -39,12 +40,6 @@ public static VelocityContext prepareContext(Configuration configuration, Entity
3940
velocityContext.put("importList", getImportList(entityInfo));
4041
velocityContext.put("fields", entityInfo.getFields());
4142

42-
43-
velocityContext.put("primaryKeyField",entityInfo.primaryKeyField());
44-
45-
// other fields
46-
velocityContext.put("primaryKeyFields",entityInfo.primaryKeyFields());
47-
velocityContext.put("noPrimaryKeyFields",entityInfo.noPrimaryKeyFields());
4843
velocityContext.put("showListFields", entityInfo.showListFields());
4944
velocityContext.put("requiredFields", entityInfo.requiredFields());
5045
velocityContext.put("noDefaultValueFields", entityInfo.noDefaultValueFields());
@@ -62,6 +57,7 @@ public static VelocityContext prepareContext(Configuration configuration, Entity
6257
public static List<String> getTemplateList() {
6358
List<String> templates = new ArrayList<String>();
6459
templates.add("templates/java/domain.java.vm");
60+
templates.add("templates/java/create.java.vm");
6561
templates.add("templates/java/projection.java.vm");
6662
templates.add("templates/java/query.java.vm");
6763
templates.add("templates/java/service.java.vm");
@@ -84,6 +80,8 @@ public static String getFileName(String template, Configuration configuration, E
8480
String javaPath = GeneratorConstant.JAVA_CODE_PATH + packageName.replace(".", "/");
8581
if (template.contains("domain.java.vm")) {
8682
fileName = String.format("%s/domain/%s.java", javaPath, className);
83+
} else if (template.contains("create.java.vm")) {
84+
fileName = String.format("%s/domain/param/Create%s.java", javaPath, className);
8785
} else if (template.contains("query.java.vm")) {
8886
fileName = String.format("%s/domain/param/Query%s.java", javaPath, className);
8987
} else if (template.contains("projection.java.vm")) {
@@ -99,7 +97,7 @@ public static String getFileName(String template, Configuration configuration, E
9997
} else if (template.contains("mapper.xml.vm")) {
10098
fileName = String.format("%s/mapper/%sMapper.xml", GeneratorConstant.RESOURCES_PATH, className);
10199
} else if (template.contains("sql.vm")) {
102-
fileName = String.format("%s/sql/%s.sql", GeneratorConstant.RESOURCES_PATH, tableName);
100+
fileName = String.format("%s/schemas/%s.sql", GeneratorConstant.RESOURCES_PATH, tableName);
103101
}
104102
return fileName;
105103
}

0 commit comments

Comments
 (0)