mybatis generator默认模板
模板文件说明
- entity.java.*:java语言实体类模板文件
- entity.kt.*:kotlin语言实体类模板文件
-
- mapper.java.*:mapper接口类模板文件
- mapper.xml.*:mapper xml文件模板文件
-
- controller.*:controller模板文件
- service.java.*:service接口模板文件
- serviceImpl.java.*:service实现类模板文件
-
- 后缀说明:
- btl:beetl模板
- ftl:freemarker模板
- vm:velocity模板
默认模板生成的实体类:@Getter、@Setter注解,未使用@Data注解
- @Getter
- @Setter
- @TableName("t_person")
- @ApiModel(value = "Person对象", description = "")
- public class Person extends Model
{ -
- private static final long serialVersionUID = 1L;
-
- @TableId(value = "id", type = IdType.AUTO)
- private Integer id;
-
- private String name;
-
- private Integer age;
-
-
- @Override
- public Serializable pkVal() {
- return this.id;
- }
-
- }
自定义实体类模板,使用@Data注解
复制entity.java.vm到resources/entity目录下,引入@Data注解,替换掉@Getter、@Setter
- package ${package.Entity};
-
- #foreach($pkg in ${table.importPackages})
- import ${pkg};
- #end
- #if(${swagger})
- import io.swagger.annotations.ApiModel;
- import io.swagger.annotations.ApiModelProperty;
- #end
- #if(${entityLombokModel})
- import lombok.Data;
- import lombok.EqualsAndHashCode;
- #if(${chainModel})
- import lombok.experimental.Accessors;
- #end
- #end
-
- /**
- *
- * $!{table.comment}
- *
- *
- * @author ${author}
- * @since ${date}
- */
- #if(${entityLombokModel})
- @Data
- @EqualsAndHashCode(callSuper = true)
- #if(${chainModel})
- @Accessors(chain = true)
- #end
- #end
- #if(${table.convert})
- @TableName("${schemaName}${table.name}")
- #end
- #if(${swagger})
- @ApiModel(value = "${entity}对象", description = "$!{table.comment}")
- #end
- #if(${superEntityClass})
- public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end {
- #elseif(${activeRecord})
- public class ${entity} extends Model<${entity}> {
- #elseif(${entitySerialVersionUID})
- public class ${entity} implements Serializable {
- #else
- public class ${entity} {
- #end
- #if(${entitySerialVersionUID})
-
- private static final long serialVersionUID = 1L;
- #end
- ## ---------- BEGIN 字段循环遍历 ----------
- #foreach($field in ${table.fields})
-
- #if(${field.keyFlag})
- #set($keyPropertyName=${field.propertyName})
- #end
- #if("$!field.comment" != "")
- #if(${swagger})
- @ApiModelProperty("${field.comment}")
- #else
- /**
- * ${field.comment}
- */
- #end
- #end
- #if(${field.keyFlag})
- ## 主键
- #if(${field.keyIdentityFlag})
- @TableId(value = "${field.annotationColumnName}", type = IdType.AUTO)
- #elseif(!$null.isNull(${idType}) && "$!idType" != "")
- @TableId(value = "${field.annotationColumnName}", type = IdType.${idType})
- #elseif(${field.convert})
- @TableId("${field.annotationColumnName}")
- #end
- ## 普通字段
- #elseif(${field.fill})
- ## ----- 存在字段填充设置 -----
- #if(${field.convert})
- @TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill})
- #else
- @TableField(fill = FieldFill.${field.fill})
- #end
- #elseif(${field.convert})
- @TableField("${field.annotationColumnName}")
- #end
- ## 乐观锁注解
- #if(${field.versionField})
- @Version
- #end
- ## 逻辑删除注解
- #if(${field.logicDeleteField})
- @TableLogic
- #end
- private ${field.propertyType} ${field.propertyName};
- #end
- ## ---------- END 字段循环遍历 ----------
-
- #if(!${entityLombokModel})
- #foreach($field in ${table.fields})
- #if(${field.propertyType.equals("boolean")})
- #set($getprefix="is")
- #else
- #set($getprefix="get")
- #end
-
- public ${field.propertyType} ${getprefix}${field.capitalName}() {
- return ${field.propertyName};
- }
-
- #if(${chainModel})
- public ${entity} set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
- #else
- public void set${field.capitalName}(${field.propertyType} ${field.propertyName}) {
- #end
- this.${field.propertyName} = ${field.propertyName};
- #if(${chainModel})
- return this;
- #end
- }
- #end
- ## --foreach end---
- #end
- ## --end of #if(!${entityLombokModel})--
-
- #if(${entityColumnConstant})
- #foreach($field in ${table.fields})
- public static final String ${field.name.toUpperCase()} = "${field.name}";
-
- #end
- #end
- #if(${activeRecord})
- @Override
- public Serializable pkVal() {
- #if(${keyPropertyName})
- return this.${keyPropertyName};
- #else
- return null;
- #end
- }
-
- #end
- #if(!${entityLombokModel})
- @Override
- public String toString() {
- return "${entity}{" +
- #foreach($field in ${table.fields})
- #if($!{foreach.index}==0)
- "${field.propertyName}=" + ${field.propertyName} +
- #else
- ", ${field.propertyName}=" + ${field.propertyName} +
- #end
- #end
- "}";
- }
- #end
- }
CodeGenerator:代码生成器使用自定义模板
- public class CodeGenerator {
-
- public static void main(String[] args){
- FastAutoGenerator.create("jdbc:mysql://localhost:3366/lihu?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8",
- "root","123456")
- .globalConfig(builder -> builder
- .author("lihu").enableSwagger()
- .outputDir("src"+ File.separator +"main" + File.separator + "java")
- .build())
- .strategyConfig(builder -> builder
- .enableCapitalMode().addInclude("t_person").addTablePrefix("t_")
- .entityBuilder().idType(IdType.ASSIGN_UUID)
- .enableLombok().enableActiveRecord().fileOverride()
- .columnNaming(NamingStrategy.underline_to_camel)
- .versionColumnName("version").versionPropertyName("version")
- .mapperBuilder().formatMapperFileName("%sMapper").formatXmlFileName("%sMapper")
- .enableBaseResultMap().enableBaseColumnList().fileOverride()
- .serviceBuilder().formatServiceFileName("%sService").formatServiceImplFileName("%sServiceImpl")
- .controllerBuilder().enableRestStyle().formatFileName("%sController")
- .build())
- .packageConfig(builder -> builder
- .parent("com.example.demo")
- .entity("pojo").mapper("dao")
- .service("service").serviceImpl("service.impl").controller("controller")
- .pathInfo(Collections.singletonMap(OutputFile.xml, "src/main/resources/mappers/"))
- //设置mapper文件的自定义路径
- .build())
- .templateConfig(builder -> builder.entity("/entity/entity.java.vm"))
- //使用自定义的实体类模板生成类
- .execute();
-
- }
- }
Person:自定义的模板生成的实体类
- @Data
- @EqualsAndHashCode(callSuper = true)
- @TableName("t_person")
- @ApiModel(value = "Person对象", description = "")
- public class Person extends Model
{ -
- private static final long serialVersionUID = 1L;
-
- @TableId(value = "id", type = IdType.AUTO)
- private Integer id;
-
- private String name;
-
- private Integer age;
-
-
- @Override
- public Serializable pkVal() {
- return this.id;
- }
-
- }