Skip to content

Commit 98cc47b

Browse files
committed
fix controller.java.vm & sql.vm
1 parent 3d085cb commit 98cc47b

File tree

6 files changed

+46
-165
lines changed

6 files changed

+46
-165
lines changed

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

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import cn.devcxl.generator.domain.EntityInfo;
66
import cn.devcxl.generator.enums.FieldType;
77
import cn.devcxl.generator.utils.VelocityUtils;
8+
import cn.hutool.core.io.FileUtil;
89
import org.apache.velocity.Template;
910
import org.apache.velocity.VelocityContext;
1011
import org.apache.velocity.app.Velocity;
@@ -29,13 +30,13 @@ public static void main(String[] args) {
2930

3031
List<FieldInfo> fieldInfos = new ArrayList<>();
3132

32-
FieldInfo idField = new FieldInfo("id", "用户ID", FieldType.INTEGER, "(11)", true, true, true, false, false, false, true, "EQ", "input", "");
33-
FieldInfo usernameField = new FieldInfo("userName", "用户名", FieldType.VARCHAR, "(30)", false, false, true, true, true, true, true, "LIKE", "input", "");
34-
FieldInfo passwordField = new FieldInfo("password", "密码", FieldType.VARCHAR, "(64)", false, false, true, false, true, false, false, "", "input", "");
35-
FieldInfo emailField = new FieldInfo("email", "邮箱", FieldType.VARCHAR, "(30)", false, false, false, true, true, true, true, "LIKE", "input", "");
36-
FieldInfo ageField = new FieldInfo("age", "年龄", FieldType.INTEGER, "(3)", false, false, false, true, true, true, true, "EQ", "input", "");
37-
FieldInfo genderField = new FieldInfo("gender", "性别", FieldType.TINYINT, "(1)", false, false, false, true, true, true, true, "EQ", "select", "genderDict");
38-
FieldInfo birthdayField = new FieldInfo("birthday", "生日", FieldType.DATE, "", false, false, false, true, true, true, true, "BETWEEN", "datetime", "");
33+
FieldInfo idField = new FieldInfo("id", "用户ID", FieldType.INTEGER, "(11)", "",true, true, true, false, false, false, true, "EQ", "input", "");
34+
FieldInfo usernameField = new FieldInfo("userName", "用户名", FieldType.VARCHAR, "(30)", "", false, false, true, true, true, true, true, "LIKE", "input", "");
35+
FieldInfo passwordField = new FieldInfo("password", "密码", FieldType.VARCHAR, "(64)","", false, false, true, false, true, false, false, "", "input", "");
36+
FieldInfo emailField = new FieldInfo("email", "邮箱", FieldType.VARCHAR, "(30)", "",false, false, false, true, true, true, true, "LIKE", "input", "");
37+
FieldInfo ageField = new FieldInfo("age", "年龄", FieldType.INTEGER, "(3)", "",false, false, false, true, true, true, true, "EQ", "input", "");
38+
FieldInfo genderField = new FieldInfo("gender", "性别", FieldType.TINYINT, "(1)", "",false, false, false, true, true, true, true, "EQ", "select", "genderDict");
39+
FieldInfo birthdayField = new FieldInfo("birthday", "生日", FieldType.DATE, "", "",false, false, false, true, true, true, true, "BETWEEN", "datetime", "");
3940

4041
fieldInfos.add(idField);
4142
fieldInfos.add(usernameField);
@@ -46,17 +47,22 @@ public static void main(String[] args) {
4647
fieldInfos.add(birthdayField);
4748

4849
// 创建表信息
49-
EntityInfo entityInfo = new EntityInfo(configuration, "用户表", "User", "管理用户信息", fieldInfos);
50+
EntityInfo entityInfo = new EntityInfo(configuration, "用户测试", "User", "管理用户信息", fieldInfos);
5051

5152
VelocityInitializer.initVelocity();
5253
VelocityContext context = VelocityUtils.prepareContext(configuration, entityInfo);
5354

5455

55-
String template = "templates/java/serviceImpl.java.vm" ;
56-
StringWriter sw = new StringWriter();
57-
Template tpl = Velocity.getTemplate(template, Velocity.ENCODING_DEFAULT);
58-
tpl.merge(context, sw);
59-
System.out.printf("%s\n%s", template, sw.toString());
56+
List<String> templateList = VelocityUtils.getTemplateList();
57+
58+
for (String s : templateList) {
59+
StringWriter sw = new StringWriter();
60+
Template tpl = Velocity.getTemplate(s, Velocity.ENCODING_DEFAULT);
61+
tpl.merge(context, sw);
62+
String fileName = VelocityUtils.getFileName(s,configuration,entityInfo);
63+
System.out.println(fileName);
64+
FileUtil.writeUtf8String(sw.toString(),"/home/devcxl/IdeaProjects/code-gen-template/src/"+fileName);
65+
}
6066
}
6167

6268
}

src/main/java/cn/devcxl/generator/constant/GeneratorConstant.java

Lines changed: 2 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -7,126 +7,7 @@
77
*/
88
public class GeneratorConstant {
99

10-
private static final String PROJECT_PATH = "main/java";
11-
/**
12-
* 数据库字符串类型
13-
*/
14-
public static final String[] COLUMNTYPE_STR = {"char", "varchar", "nvarchar", "varchar2", "tinytext", "text", "mediumtext", "longtext"};
10+
public static final String JAVA_CODE_PATH = "main/java/";
11+
public static final String RESOURCES_PATH = "main/resources";
1512

16-
/**
17-
* 数据库时间类型
18-
*/
19-
public static final String[] COLUMNTYPE_TIME = {"datetime", "time", "date", "timestamp"};
20-
21-
/**
22-
* 数据库布尔类型
23-
*/
24-
public static final String[] COLUMNTYPE_BOOL = {"bit"};
25-
26-
/**
27-
* 数据库数字类型
28-
*/
29-
public static final String[] COLUMNTYPE_NUMBER = {"tinyint", "smallint", "mediumint", "int", "number", "integer", "bigint", "float", "float", "double", "decimal"};
30-
31-
/**
32-
* 页面不需要编辑字段
33-
*/
34-
public static final String[] COLUMNNAME_NOT_EDIT = {"id", "create_by", "create_time", "del_flag"};
35-
36-
/**
37-
* 页面不需要显示的列表字段
38-
*/
39-
public static final String[] COLUMNNAME_NOT_LIST = {"id", "create_by", "create_time", "del_flag", "update_by", "update_time"};
40-
41-
/**
42-
* 页面不需要查询字段
43-
*/
44-
public static final String[] COLUMNNAME_NOT_QUERY = {"id", "create_by", "create_time", "del_flag", "update_by", "update_time", "remark"};
45-
46-
/**
47-
* Entity基类字段
48-
*/
49-
public static final String[] BASE_ENTITY = {"createBy", "createTime", "updateBy", "updateTime", "remark"};
50-
51-
/**
52-
* Tree基类字段
53-
*/
54-
public static final String[] TREE_ENTITY = {"parentName", "parentId"};
55-
56-
/**
57-
* 文本框
58-
*/
59-
public static final String HTML_INPUT = "input";
60-
61-
/**
62-
* 文本域
63-
*/
64-
public static final String HTML_TEXTAREA = "textarea";
65-
66-
/**
67-
* 下拉框
68-
*/
69-
public static final String HTML_SELECT = "select";
70-
71-
/**
72-
* 单选框
73-
*/
74-
public static final String HTML_RADIO = "radio";
75-
76-
/**
77-
* 复选框
78-
*/
79-
public static final String HTML_CHECKBOX = "checkbox";
80-
81-
/**
82-
* 日期控件
83-
*/
84-
public static final String HTML_DATETIME = "datetime";
85-
86-
/**
87-
* 上传控件
88-
*/
89-
public static final String HTML_UPLOAD = "upload";
90-
91-
/**
92-
* 字符串类型
93-
*/
94-
public static final String TYPE_STRING = "String";
95-
96-
/**
97-
* 整型
98-
*/
99-
public static final String TYPE_INTEGER = "Integer";
100-
101-
/**
102-
* 长整型
103-
*/
104-
public static final String TYPE_LONG = "Long";
105-
106-
/**
107-
* 浮点型
108-
*/
109-
public static final String TYPE_DOUBLE = "Double";
110-
111-
/**
112-
* 高精度计算类型
113-
*/
114-
public static final String TYPE_BIGDECIMAL = "BigDecimal";
115-
116-
/**
117-
* 时间类型
118-
*/
119-
public static final String TYPE_DATE = "Date";
120-
121-
public static final String TYPE_BOOLEAN = "Boolean";
122-
123-
/**
124-
* 模糊查询
125-
*/
126-
public static final String QUERY_LIKE = "LIKE";
127-
128-
/**
129-
* 需要
130-
*/
131-
public static final String REQUIRE = "1";
13213
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,15 @@ public class FieldInfo {
102102
private String dictType;
103103

104104

105-
public FieldInfo(String name, String comment, FieldType fieldType, String customSql, boolean isPk, boolean isIncrement, boolean isRequired, boolean isInsert, boolean isEdit, boolean isList, boolean isQuery, String queryType, String htmlType, String dictType) {
105+
public FieldInfo(String name, String comment, FieldType fieldType, String customSql,String defaultValue, boolean isPk, boolean isIncrement, boolean isRequired, boolean isInsert, boolean isEdit, boolean isList, boolean isQuery, String queryType, String htmlType, String dictType) {
106106
this.name = name;
107107
this.javaField = GeneratorUtils.toLowerCaseCamelCase(this.name);
108108
this.sqlField = GeneratorUtils.toSnakeCase(this.name);
109109

110110
this.comment = comment;
111111
this.fieldType = fieldType;
112112
this.customSql = customSql;
113+
this.defaultValue = defaultValue;
113114

114115
this.isPrimaryKey = isPk;
115116
this.isIncrement = isIncrement;

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

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

3+
import cn.devcxl.generator.constant.GeneratorConstant;
34
import cn.devcxl.generator.domain.Configuration;
45
import cn.devcxl.generator.domain.FieldInfo;
56
import cn.devcxl.generator.domain.EntityInfo;
@@ -15,10 +16,7 @@
1516
* @author devcxl
1617
*/
1718
public class VelocityUtils {
18-
/**
19-
* 项目空间路径
20-
*/
21-
private static final String PROJECT_PATH = "main/java";
19+
2220

2321
/**
2422
* 设置模板变量信息
@@ -59,22 +57,14 @@ public static VelocityContext prepareContext(Configuration configuration, Entity
5957
*/
6058
public static List<String> getTemplateList() {
6159
List<String> templates = new ArrayList<String>();
62-
// 实体类
6360
templates.add("templates/java/domain.java.vm");
64-
// // 展示类
65-
// templates.add("templates/java/projection.java.vm");
66-
// // 查询条件
67-
// templates.add("templates/java/query.java.vm");
68-
// templates.add("templates/java/service.java.vm");
69-
// templates.add("templates/java/serviceImpl.java.vm");
70-
// templates.add("templates/java/controller.java.vm");
71-
////
72-
// templates.add("templates/java/mapper.java.vm");
73-
// templates.add("templates/xml/mapper.xml.vm");
74-
75-
// templates.add("templates/html/list.html.vm");
76-
// templates.add("templates/html/add.html.vm");
77-
// templates.add("templates/html/edit.html.vm");
61+
templates.add("templates/java/projection.java.vm");
62+
templates.add("templates/java/query.java.vm");
63+
templates.add("templates/java/service.java.vm");
64+
templates.add("templates/java/serviceImpl.java.vm");
65+
templates.add("templates/java/controller.java.vm");
66+
templates.add("templates/java/mapper.java.vm");
67+
templates.add("templates/xml/mapper.xml.vm");
7868
templates.add("templates/sql/sql.vm");
7969
return templates;
8070
}
@@ -86,12 +76,15 @@ public static String getFileName(String template, Configuration configuration, E
8676
String fileName = "";
8777
String packageName = configuration.getPackageName();
8878
String className = entityInfo.getClassName();
89-
String businessName = entityInfo.getComment();
90-
String javaPath = PROJECT_PATH + "/" + packageName.replace(".", "/");
79+
String tableName = entityInfo.getTableName();
80+
String javaPath = GeneratorConstant.JAVA_CODE_PATH + packageName.replace(".", "/");
9181
if (template.contains("domain.java.vm")) {
9282
fileName = String.format("%s/domain/%s.java", javaPath, className);
93-
}
94-
if (template.contains("mapper.java.vm")) {
83+
} else if (template.contains("query.java.vm")) {
84+
fileName = String.format("%s/domain/param/Query%s.java", javaPath, className);
85+
} else if (template.contains("projection.java.vm")) {
86+
fileName = String.format("%s/domain/projection/%sProjection.java", javaPath, className);
87+
} else if (template.contains("mapper.java.vm")) {
9588
fileName = String.format("%s/mapper/%sMapper.java", javaPath, className);
9689
} else if (template.contains("service.java.vm")) {
9790
fileName = String.format("%s/service/I%sService.java", javaPath, className);
@@ -100,9 +93,9 @@ public static String getFileName(String template, Configuration configuration, E
10093
} else if (template.contains("controller.java.vm")) {
10194
fileName = String.format("%s/controller/%sController.java", javaPath, className);
10295
} else if (template.contains("mapper.xml.vm")) {
103-
fileName = String.format("%s/mapper/xml/%sMapper.xml", javaPath, className);
96+
fileName = String.format("%s/mapper/%sMapper.xml", GeneratorConstant.RESOURCES_PATH, className);
10497
} else if (template.contains("sql.vm")) {
105-
fileName = businessName + "Menu.sql";
98+
fileName = String.format("%s/sql/%s.sql", GeneratorConstant.RESOURCES_PATH, tableName);
10699
}
107100
return fileName;
108101
}

src/main/resources/templates/java/controller.java.vm

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import ${packageName}.service.I${ClassName}Service;
99
import java.time.LocalDateTime;
1010
import java.util.Arrays;
1111
import java.util.List;
12-
12+
import java.util.stream.Collectors;
1313
import ${primaryKeyField.fieldType.javaType};
1414

1515
/**
@@ -53,7 +53,7 @@ public class ${ClassName}Controller {
5353
* 修改${ClassName}
5454
*/
5555
@PutMapping("/update")
56-
public Result update(@RequestBody ${ClassName} ${className}) {
56+
public CommonResp<?> update(@RequestBody ${ClassName} ${className}) {
5757
${className}ServiceImpl.update(${className});
5858
return CommonResp.ok();
5959
}
@@ -72,7 +72,8 @@ public class ${ClassName}Controller {
7272
*/
7373
@DeleteMapping("/batch")
7474
public CommonResp<?> deleteIds(String ids){
75-
return ${className}ServiceImpl.batchDelete(List.of(ids.split(",")));
75+
List<${primaryKeyField.fieldType.javaShortType}> values = Arrays.stream(ids.split(",")).map(${primaryKeyField.fieldType.javaShortType}::valueOf).collect(Collectors.toList());
76+
return ${className}ServiceImpl.batchDelete(values);
7677
}
7778

7879

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
CREATE TABLE `${tableName}` (
22
#foreach($field in $fields)
3-
${field.sqlField} ${field.fieldType.sqlType}${field.customSql}#if($field.defaultValue != '') default $field.defaultValue#end#if($field.required) not null#else null#end#if($field.increment) auto_increment#end#if($foreach.hasNext),#end
3+
${field.sqlField} ${field.fieldType.sqlType}${field.customSql}#if($field.defaultValue != '') default $field.defaultValue#end#if($field.required) not null#else null#end#if($field.increment) auto_increment#end#if($field.primaryKey) primary key#end#if($foreach.hasNext),#end
44
#end
5-
PRIMARY KEY (`id`)
65
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

0 commit comments

Comments
 (0)