MyBatisPlus代码生成器添加自定义模板

前言

MyBatisPlus:AutoGenerator 是 MyBatis-Plus 的代码生成器,通过 AutoGenerator 可以快速生成 Entity、Mapper、Mapper XML、Service、Controller
等各个模块的代码,极大的提升了开发效率。

但是,自带的模板还是有点差强人意,这里就需要自定义模板来实现。

需要了解自定义代码生成器

参考:MyBatisPlus代码生成器自定义FreemarkerEngine

这里以新建一个 Dto 举例。

配置

配置模板信息

创建枚举类CodeGeneratorEnumVal(详见附录)

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
/**
* 常用配置信息<br>
* 自定义模板需要添加 .ftl<br>
*
* @author maxzhao
* @date 2021-08-26 23:01
*/
@Getter
public enum CodeGeneratorEnumVal {
/**
* controller<br>
* * 非自定义,模板路径不需要 .ftl
*/
CONTROLLER("templates/api/z.controller.java", "api", "%sController"),
/**
* dto
*/
DTO("templates/api/dto/z.dto.java.ftl", "api.dto", "%sDto"),
/*********/
/**
* 模板
*/
private final String template;
/**
* 模板
*/
private final String packageName;
/**
* 模板
*/
private final String className;
}

配置 AutoGenerator

1
2
3
4
5
6
7
8
9
public class CodeGeneratorFactory {
public static byte[] generate(CodeGeneratorConfig config) {
CodeGeneratorDbConfig dbConfig = config.getDbConfig();
//region 注入配置
mpg.setCfg(configInjection(config));
/*************/
return null;
}
}

configInjection方法

这里配置的是

  1. 自定义模板的路径( map cfg)
  2. 输出文件的路径 (PathInfo)
  3. 通用模板参数
    • 包路径 (packageInfo)
    • 基础的 Entity、Mapper、Mapper XML、Service、Controller生成路径
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
public class CodeGeneratorFactory {
private static InjectionConfig configInjection(CodeGeneratorConfig config) {
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
String controllerPackage = this.getConfig().getPackageInfo().get("Controller");
this.getConfig().getPackageInfo().put("Dto", controllerPackage + ".dto");

/**************************/
String xmlPath = this.getConfig().getPathInfo().get("xml_path");
/*修改 xml 生成路径*/
this.getConfig().getPathInfo().put("xml_path", xmlPath.replace(CodeGeneratorVal.JAVA_SOURCE_PATH, CodeGeneratorVal.XML_SOURCE_PATH));
Map<String, Object> map = new HashMap<>(4);
/*自定义cfg引用*/
this.setMap(map);
}
};
List<FileOutConfig> focList = new ArrayList<>();
/*dto 模板*/
focList.add(convertFileOutConfig(config, CodeGeneratorEnumVal.DTO));
/**************************/
cfg.setFileOutConfigList(focList);
return cfg;
}
/**
* 加载文件输出配置
*
* @param config 配置
* @param enumVal 模板
* @return mybatis plus 文件输出配置
*/
private static FileOutConfig convertFileOutConfig(CodeGeneratorConfig config, CodeGeneratorEnumVal enumVal) {
return new FileOutConfig(enumVal.getTemplate()) {
@Override
public String outputFile(TableInfo tableInfo) {
/*指定模板生,自定义生成文件到哪个地方*/
return config.getModuleOutPutDir() + File.separator +
enumVal.getPackageName().replace(".", File.separator) +
File.separator +
String.format(enumVal.getClassName(), tableInfo.getEntityName()) + StringPool.DOT_JAVA;
}
};
}
}

模板文件z.dto.java.ftl

resources/templates/api/dto/z.dto.java.ftl 下创建文件,dto模板文件与 entity文件是比较相似的,所以直接从 entity.java.ftl拷贝一下,然后修改自己的自定义模板逻辑。

主要修改点

1
2
3
4
5
package ${package.Dto};
/**去掉 @Table @TableId @TableField 等Mybatis 标签***/
public class ${table.dtoName} implements Serializable {
/*****/
}

附录

CodeGeneratorEnumVal

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
package com.gt.code.generator.config;

import lombok.Getter;

/**
* 常用配置信息<br>
* 自定义模板需要添加 .ftl<br>
*
* @author maxzhao
* @date 2021-08-26 23:01
*/
@Getter
public enum CodeGeneratorEnumVal {
/**
* controller<br>
* * 非自定义,模板路径不需要 .ftl
*/
CONTROLLER("templates/api/z.controller.java", "api", "%sDto"),
/**
* dto
*/
DTO("templates/api/dto/z.dto.java.ftl", "api.dto", "%sDto"),

/**
* params
*/
PARAMS("templates/api/params/z.params.java.ftl", "api.params", "%sParams"),

/**
* vo
*/
VO("templates/api/vo/z.vo.java.ftl", "api.vo", "%sVo"),


/**
* entity<br>
* * 非自定义,模板路径不需要 .ftl
*/
ENTITY("templates/model/entity/z.entity.java", "model.entity", "%"),
/**
* service<br>
* 非自定义,模板路径不需要 .ftl
*/
SERVICE("templates/service/z.service.java", "service", "%"),
/**
* service impl<br>
* * 非自定义,模板路径不需要 .ftl
*/
SERVICE_IMPL("templates/service/impl/z.serviceImpl.java", "service.impl", "%"),
/**
* mapper<br>
* * 非自定义,模板路径不需要 .ftl
*/
MAPPER("templates/mapper/z.mapper.java", "mapper", "%Mapper"),
/**
* mapper xml<br>
* * 非自定义,模板路径不需要 .ftl
*/
TEMPLATE_XML("templates/mapper/z.mapper.xml", "mapper", "%Mapper"),

/**
* convert
*/
CONVERT("templates/util/convert/z.convert.java.ftl", "util.convert", "%sConvert"),

/**
* 异常
*/
EXCEPTION("templates/exception/z.exception.java.ftl", "exception", "%sException"),

/**
* 异常类型
*/
EXCEPTION_ENUM("templates/exception/z.exceptionEnum.java.ftl", "exception", "%sExceptionEnum"),

/**
* 异常类型
*/
EXCEPTION_HANDLER("templates/exception/handler/z.exceptionHandler.java.ftl", "exception.handler", "%sExceptionHandler");

/**
* 模板
*/
private final String template;
/**
* 模板
*/
private final String packageName;
/**
* 模板
*/
private final String className;

CodeGeneratorEnumVal(String template, String packageName, String className) {
this.template = template;
this.packageName = packageName;
this.className = className;
}

@Override
public String toString() {
return super.toString();
}
}

CodeGeneratorFactory

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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package com.gt.code.generator.factory;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.OracleTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.gt.code.generator.config.CodeGeneratorConfig;
import com.gt.code.generator.config.CodeGeneratorDbConfig;
import com.gt.code.generator.config.CodeGeneratorEnumVal;
import com.gt.code.generator.config.CodeGeneratorVal;
import com.gt.code.generator.util.template.CustomFreemarkerTemplateEngine;
import org.apache.commons.io.IOUtils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.*;
import java.util.zip.ZipOutputStream;

/**
* 生成代码工厂<br>
*
* @author maxzhao
* @date 2021-08-27
*/
public class CodeGeneratorFactory {
/**
* 自动填充
*/
private static final List<TableFill> TABLE_FILL_LIST = Arrays.asList(
new TableFill("createTime", FieldFill.INSERT),
new TableFill("updateTime", FieldFill.UPDATE),
new TableFill("delStatus", FieldFill.INSERT_UPDATE));

private CodeGeneratorFactory() {
}

public static byte[] generate(CodeGeneratorConfig config) {
AutoGenerator mpg = new AutoGenerator();
CodeGeneratorDbConfig dbConfig = config.getDbConfig();
/*输出路径*/
String outPutDir = config.getFullOutPutDir();
//region 注入配置
mpg.setCfg(configInjection(config));
//endregion
//region 数据源配置
DataSourceConfig dataSource = dbConfig.generatorDataSourceConfig();
if (dataSource.getDbType() == DbType.ORACLE) {
dataSource.setTypeConvert(new OracleTypeConvert() {
@Override
public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
if (fieldType.toLowerCase(Locale.ROOT).matches("")) {

}
return super.processTypeConvert(globalConfig, fieldType);
}
});
}
mpg.setDataSource(dataSource);
//endregion
//region 表策略配置
StrategyConfig strategy = new StrategyConfig();
/*驼峰*/
strategy.setNaming(NamingStrategy.underline_to_camel);
/*驼峰*/
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
/*使用 lombok*/
strategy.setEntityLombokModel(true);
/*默认使用 restfull风格*/
strategy.setRestControllerStyle(true);
/*标*/
strategy.setInclude(config.getTablesNames());
/*驼峰转连字符串*/
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(config.getTablePrefix());
/*字段添加 @TableField*/
strategy.setEntityTableFieldAnnotationEnable(true);
strategy.setTableFillList(TABLE_FILL_LIST);
mpg.setStrategy(strategy);
//endregion
//region 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName(config.getModuleName());
pc.setParent(config.getParentPackage());
/*设置 controller 文件 包名*/
pc.setController(CodeGeneratorEnumVal.CONTROLLER.getPackageName());
/*设置 xml 文件 包名*/
pc.setXml("mapper");
/*设置 entity 文件 包名*/
pc.setEntity("model.entity");
pc.setXml("mapper");
mpg.setPackageInfo(pc);
//endregion
//region 模板配置
TemplateConfig templateConfig = new TemplateConfig()
.setEntity(CodeGeneratorEnumVal.ENTITY.getTemplate())
.setController(CodeGeneratorEnumVal.CONTROLLER.getTemplate())
.setService(CodeGeneratorEnumVal.SERVICE.getTemplate())
.setServiceImpl(CodeGeneratorEnumVal.SERVICE_IMPL.getTemplate())
.setMapper(CodeGeneratorEnumVal.MAPPER.getTemplate())
.setXml(CodeGeneratorEnumVal.TEMPLATE_XML.getTemplate());
mpg.setTemplate(templateConfig);
//endregion
//region 全局配置
/*默认使用雪花算法*/
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(outPutDir);
gc.setAuthor(config.getAuthor());
gc.setOpen(false);
/*默认不覆盖,如果文件存在,将不会再生成,配置true就是覆盖*/
gc.setFileOverride(true);
/*配置 swagger2*/
gc.setSwagger2(config.getSwagger2());
/*开启 ActiveRecord 模式*/
gc.setActiveRecord(config.getActiveRecord());
mpg.setGlobalConfig(gc);

//endregion
//region 模板引擎
// 选择 freemarker 引擎需要指定如下加,注意 pom 依赖必须有!
CustomFreemarkerTemplateEngine templateEngine = new CustomFreemarkerTemplateEngine(config);
ByteArrayOutputStream outputStream = null;
ZipOutputStream zip = null;
if (config.getUseZip()) {
/*下载zip文件 字节输出流*/
outputStream = new ByteArrayOutputStream();
zip = new ZipOutputStream(outputStream);
templateEngine.setZipOutputStream(zip);
/*下载zip ,会覆盖文件*/
gc.setFileOverride(true);
}
mpg.setTemplateEngine(templateEngine);
//endregion
mpg.execute();
if (config.getUseZip()) {
/*获取结果*/
//等同于自己写判断然后关闭
IOUtils.closeQuietly(zip);
return outputStream != null ? outputStream.toByteArray() : null;
}
return null;
}

/**
* 添加自动填充
*
* @param tableFill 新的自动填充
*/
public static void addTableFill(TableFill tableFill) {
TABLE_FILL_LIST.add(tableFill);
}

/**
* 配置自定义参数/属性
*
* @param config 配置信息
*/
private static InjectionConfig configInjection(CodeGeneratorConfig config) {
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
String controllerPackage = this.getConfig().getPackageInfo().get("Controller");
this.getConfig().getPackageInfo().put("Dto", controllerPackage + ".dto");
this.getConfig().getPackageInfo().put("Params", controllerPackage + ".params");
this.getConfig().getPackageInfo().put("Vo", controllerPackage + ".vo");
this.getConfig().getPackageInfo().put("Convert", controllerPackage.substring(0, controllerPackage.lastIndexOf(CodeGeneratorEnumVal.CONTROLLER.getPackageName())) + CodeGeneratorEnumVal.CONVERT.getPackageName());
String xmlPath = this.getConfig().getPathInfo().get("xml_path");
/*修改 xml 生成路径*/
this.getConfig().getPathInfo().put("xml_path", xmlPath.replace(CodeGeneratorVal.JAVA_SOURCE_PATH, CodeGeneratorVal.XML_SOURCE_PATH));
Map<String, Object> map = new HashMap<>(4);
/*自定义cfg引用*/
this.setMap(map);
}
};
List<FileOutConfig> focList = new ArrayList<>();
/*dto 模板*/
focList.add(convertFileOutConfig(config, CodeGeneratorEnumVal.DTO));
/*params 模板*/
focList.add(convertFileOutConfig(config, CodeGeneratorEnumVal.PARAMS));
/*vo 模板*/
focList.add(convertFileOutConfig(config, CodeGeneratorEnumVal.VO));
/*判断是否使用 mapstruct convert*/
if ((config.getUseConvert() != null && config.getUseConvert())
/*增删查改时使用*/
|| (config.getActiveCrud() != null && config.getActiveCrud())) {
/*convert 模板*/
focList.add(convertFileOutConfig(config, CodeGeneratorEnumVal.CONVERT));
}
/*使用增删查改*/
if (config.getActiveCrud() != null && config.getActiveCrud()) {
/*exception 模板*/
focList.add(convertFileOutConfig(config, CodeGeneratorEnumVal.EXCEPTION));
/*exception enum 模板*/
focList.add(convertFileOutConfig(config, CodeGeneratorEnumVal.EXCEPTION_ENUM));
/*exception handler 模板*/
focList.add(convertFileOutConfig(config, CodeGeneratorEnumVal.EXCEPTION_HANDLER));
}
cfg.setFileOutConfigList(focList);
return cfg;
}

/**
* 加载文件输出配置
*
* @param config 配置
* @param enumVal 模板
* @return mybatis plus 文件输出配置
*/
private static FileOutConfig convertFileOutConfig(CodeGeneratorConfig config, CodeGeneratorEnumVal enumVal) {
return new FileOutConfig(enumVal.getTemplate()) {
@Override
public String outputFile(TableInfo tableInfo) {
/*指定模板生,自定义生成文件到哪个地方*/
return config.getModuleOutPutDir() + File.separator +
enumVal.getPackageName().replace(".", File.separator) +
File.separator +
String.format(enumVal.getClassName(), tableInfo.getEntityName()) + StringPool.DOT_JAVA;
}
};
}

}

CodeGeneratorConfig

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
package com.gt.code.generator.config;

import io.swagger.annotations.ApiModelProperty;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.springframework.util.StringUtils;

import java.io.File;
import java.util.Optional;

/**
* 代码生成器基础配置<br>
* Bean 注入 <br>
* 静态注入 <br>
*
* @author maxzhao
* @date 2021-08-26 22:16
*/
@Data
@Accessors
public class CodeGeneratorConfig {
/**
* 输出路径,默认当前项目路径
*/
private String outPutDir;
/**
* 输出 Java 文件的起始路径<br>
* 只读
*/
@Setter(AccessLevel.NONE)
private String fullOutPutDir;
/**
* 输出具体模块代码的文件的起始路径<br>
* {@link #fullOutPutDir} + {@link #parentPackage} + {@link #moduleName} <br>
* 只读
*/
@Setter(AccessLevel.NONE)
private String moduleOutPutDir;

/**
* 输出路径,默认当前项目路径
*
* @param outPutDir 输出路径
*/
public void setOutPutDir(String outPutDir) {
this.outPutDir = StringUtils.hasText(outPutDir)
? outPutDir
: System.getProperty("user.dir");
}

/**
* 输出 Java 文件的起始路径
*
* @return 输出全路径 包含 src.main.java
*/
public String getFullOutPutDir() {
if (this.fullOutPutDir != null) {
return this.fullOutPutDir;
}
/*获取当前的项目路径*/
this.fullOutPutDir = this.outPutDir;
fullOutPutDir += File.separator + CodeGeneratorVal.JAVA_SOURCE_PATH;
fullOutPutDir = fullOutPutDir.replace("\\", File.separator)
.replace("/", File.separator)
.replace(File.separator + File.separator, File.separator);
return fullOutPutDir;
}

/**
* 获取输出模块的路径
*
* @return 输出全路径 包含 src.main.java
*/
public String getModuleOutPutDir() {
if (this.moduleOutPutDir != null) {
return this.moduleOutPutDir;
}
/*获取java 文件的输出路径*/
this.moduleOutPutDir = getFullOutPutDir();
this.moduleOutPutDir = (this.moduleOutPutDir + File.separator +
this.parentPackage + File.separator + this.moduleName)
.replace(".", File.separator)
.replace(File.separator + File.separator, File.separator);
return this.moduleOutPutDir;
}

/**
* 基础包名(默认 gt.maxzhao.boot)<br>
* default {@link CodeGeneratorVal#DEFAULT_PARENT_PACKAGE}
*/
private String parentPackage;

/**
* 基础包名(默认 gt.maxzhao.boot)
*
* @param parentPackage 基础包名
*/
public void setParentPackage(String parentPackage) {
this.parentPackage = StringUtils.hasText(parentPackage)
? parentPackage
: CodeGeneratorVal.DEFAULT_PARENT_PACKAGE;
}

/**
* 模块名(默认为null,格式为 xx.xxx)
*/
private String moduleName;
/**
* 作者
*/
private String author;
/**
* 表前缀
*/
private String[] tablePrefix;
/**
* 表名数组
*/
private String[] tablesNames;
/**
* 使用 mapstruct 的 convert,true 使用 ;false 不使用<br>
* default false
*/
private Boolean useConvert = false;

/**
* 使用 mapstruct 的 convert,true 使用 ;false 不使用
*
* @param useConvert true 使用 ;false 不使用
*/
public void setUseConvert(Boolean useConvert) {
this.useConvert = Optional.ofNullable(useConvert).orElse(false);
}

/**
* 开启 ActiveRecord 模式<br>
* default false
*/
private Boolean activeRecord = false;

/**
* 开启 ActiveRecord 模式<br>
* default false
*
* @param activeRecord true 使用 ;false 不使用
*/
public void setActiveRecord(Boolean activeRecord) {
this.activeRecord = Optional.ofNullable(activeRecord).orElse(false);
}

/**
* 使用 swagger2,true 使用 ;false 不使用<br>
* default true
*/
private Boolean swagger2 = true;

/**
* 使用 swagger2,true 使用 ;false 不使用<br>
* default true
*
* @param swagger2 true 使用 ;false 不使用
*/
public void setSwagger2(Boolean swagger2) {
this.swagger2 = Optional.ofNullable(swagger2).orElse(true);
}

/**
* 数据库配置
*/
private CodeGeneratorDbConfig dbConfig = null;
/**
* 使用zip文件下载<br>
* http 可以直接下载 zip 文件
*/
private Boolean useZip = false;
/**
* 自动生成crud<br>
* 默认使用
*/
private Boolean activeCrud = true;
/**
* 自动生成 exception<br>
* 默认不使用
*/
private Boolean activeException = true;
}

z.dto.java.ftl

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
package ${package.Dto};

<#list table.importPackages as pkg>
<#if pkg?contains("com.baomidou.mybatisplus.annotation")>
<#else>
import ${pkg};
</#if>
</#list>
<#if swagger2>
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
</#if>
<#if entityLombokModel>
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
</#if>
import org.springframework.format.annotation.DateTimeFormat;

/**
* <p>${table.comment!'TODO 待补充注解'}</p>
* <p>接口、服务等接收参数</p>
*
* @author ${author}
* @date ${date}
*/
<#if entityLombokModel>
@Data
<#if superEntityClass??>
@EqualsAndHashCode(callSuper = true)
<#else>
@EqualsAndHashCode(callSuper = false)
</#if>
@Accessors(chain = true)
</#if>
<#if swagger2>
@ApiModel(value="${table.dtoName}对象", description="${table.comment!'TODO 待补充注解'}")
</#if>
public class ${table.dtoName} implements Serializable {

<#if entitySerialVersionUID>
private static final long serialVersionUID = 1L;
</#if>
<#-- ---------- BEGIN 字段循环遍历 ---------->
<#list table.fields as field>
<#if field.keyFlag>
<#assign keyPropertyName="${field.propertyName}"/>
</#if>
/**
* ${field.comment!'TODO 待补充注解'}
*/
<#if field.comment!?length gt 0>
<#if swagger2>
@ApiModelProperty(value = "${field.comment!'TODO 待补充注解'}")
</#if>
</#if>
<#-- DateTimeFormat -->
<#if "LocalDateTime" == field.propertyType>
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
<#elseif "LocalDate" == field.propertyType>
@DateTimeFormat(pattern = "yyyy-MM-dd")
</#if>
private ${field.propertyType} ${field.propertyName};
</#list>
<#------------ END 字段循环遍历 ---------->

<#if !entityLombokModel>
<#list table.fields as field>
<#if field.propertyType == "boolean">
<#assign getprefix="is"/>
<#else>
<#assign getprefix="get"/>
</#if>
public ${field.propertyType} ${getprefix}${field.capitalName}() {
return ${field.propertyName};
}

<#if entityBuilderModel>
public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
<#else>
public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
</#if>
this.${field.propertyName} = ${field.propertyName};
<#if entityBuilderModel>
return this;
</#if>
}
</#list>
</#if>

<#if entityColumnConstant>
<#list table.fields as field>
public static final String ${field.name?upper_case} = "${field.name}";

</#list>
</#if>
}

本文地址: https://github.com/maxzhao-it/blog/post/49280/