引入依赖
<!-- mysql 依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
<scope>runtime</scope>
</dependency>
<!-- mybatis-plus 依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<!-- mybatis-plus代码生成器 依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
<!--freemarker 依赖-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
生成器代码
package com.code.generator;
/*
* @ClassName CodeGenerator
* @description: 代码生成器
* @author: 何翔
* @Date 2022/7/4 18:50
*/
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import java.util.*;
public class CodeGenerator {
// TODO 项目名称
private static final String PROJECT_NAME = "code";
// TODO 微服务项目模块名(可选,和单体二选一)
private static final String CLOUD_NAME = "code-example";
// TODO 路径包名(生成的文件放在绝地路径下,为下面路径代码铺垫)
private static final String PACKAGE_DIR = "/com/code/product/";
// TODO 生成包名
private static final String PACKAGE_EXE = "com.code.example";
// TODO 数据库地址
private static final String URL = "jdbc:mysql://localhost:3306/db_code?useUnicode=true&characterEncoding =UTF-8&serverTimezone = Asia/Shanghai";
// TODO 数据库用户名
private static final String USERNAME = "root";
// TODO 数据库密码
private static final String PASSWORD = "root";
// TODO 作者
private static final String AUTHOR = "何翔";
// TODO 去除的表前缀(可选)
private static final String FieldPrefix = "ex_";
// TODO 模板路径(可选)
private static final String TEMPLATE_PATH = "/templates/entity.java";
/**
* 项目路径
*/
private static final String PARENT_DIR = System.getProperty("user.dir");
/**
* xml路径
*/
//private static final String XML_PATH = PARENT_DIR + "/"+PROJECT_NAME+"-mapper/src/main/resources/mapper"; //单体项目
private static final String XML_PATH = PARENT_DIR + "/"+CLOUD_NAME+"/src/main/resources/mapper";
/**
* entity路径
*/
//private static final String ENTITY_PATH = PARENT_DIR +"/"+PROJECT_NAME+"-entity/src/main/java"+PACKAGE_DIR+"entity";//单体项目
private static final String ENTITY_PATH = PARENT_DIR +"/"+CLOUD_NAME+"/src/main/java"+PACKAGE_DIR+"entity";
/**
* mapper(Dao)路径
*/
//private static final String MAPPER_PATH = PARENT_DIR +"/"+PROJECT_NAME+"-mapper/src/main/java"+PACKAGE_DIR+"mapper";//单体项目
private static final String MAPPER_PATH = PARENT_DIR +"/"+CLOUD_NAME+"/src/main/java"+PACKAGE_DIR+"dao";
/**
* service路径
*/
//private static final String SERVICE_PATH = PARENT_DIR +"/"+PROJECT_NAME+"-service/src/main/java"+PACKAGE_DIR+"service";//单体项目
private static final String SERVICE_PATH = PARENT_DIR +"/"+CLOUD_NAME+"/src/main/java"+PACKAGE_DIR+"service";
/**
* serviceImpl路径
*/
//private static final String SERVICE_IMPL_PATH = PARENT_DIR +"/"+PROJECT_NAME+"-service/src/main/java"+PACKAGE_DIR+"service/impl/";//单体项目
private static final String SERVICE_IMPL_PATH = PARENT_DIR +"/"+CLOUD_NAME+"/src/main/java"+PACKAGE_DIR+"service/impl/";
/**
* controller路径
*/
//private static final String CONTROLLER_PATH = PARENT_DIR +"/"+PROJECT_NAME+"-api/src/main/java"+PACKAGE_DIR+"controller";//单体项目
private static final String CONTROLLER_PATH = PARENT_DIR +"/"+CLOUD_NAME+"/src/main/java"+PACKAGE_DIR+"controller";
public static void main(String[] args) {
FastAutoGenerator.create(URL, USERNAME, PASSWORD)
// 全局配置
.globalConfig(builder -> builder
// 设置作者
.author(AUTHOR)
// 开启swagger注解
.enableSwagger()
.disableOpenDir()
)
// 包配置
.packageConfig(builder -> builder
.parent("")
.xml("mapper")
.entity(PACKAGE_EXE+".entity")
.mapper(PACKAGE_EXE+".dao")
.service(PACKAGE_EXE+".service")
.serviceImpl(PACKAGE_EXE+".service.impl")
.controller(PACKAGE_EXE+".controller")
.pathInfo(getPathInfo())
)
// 策略配置
.strategyConfig((scanner, builder) ->
builder.addInclude(getTables(scanner.apply("请输入表名,多个表之间用英文逗号分隔,所有输入all")))
.addTablePrefix(FieldPrefix)
// entity
.entityBuilder()
.fileOverride()
.enableChainModel()
.fileOverride()
// 开启lombock
.enableLombok()
.enableTableFieldAnnotation()
//.logicDeleteColumnName("deleted")
//.logicDeletePropertyName("deleteFlag")
.idType(IdType.AUTO)
//.addTableFills(new Column("create_time", FieldFill.INSERT))
//.addTableFills(new Property("update_time", FieldFill.INSERT_UPDATE))
// controller
.controllerBuilder()
.fileOverride()
.enableRestStyle()
.formatFileName("%sController")
// service
.serviceBuilder()
.fileOverride()
.superServiceClass(IService.class)
.formatServiceFileName("%sService")
.formatServiceImplFileName("%sServiceImpl")
// mapper
.mapperBuilder()
.fileOverride()
.enableBaseResultMap()
.enableBaseColumnList()
.superClass(BaseMapper.class)
.formatMapperFileName("%sMapper")
.formatXmlFileName("%sMapper")
//.enableMapperAnnotation()
)
// 使用Freemarker引擎模板,默认的是Velocity引擎模板
.templateConfig(builder -> {
// 实体类使用我们自定义模板
builder.entity("/templates/entity.java");
})
.templateEngine(new FreemarkerTemplateEngine())
.execute();
}
/**
* 获取路径信息map
* @author 何翔
*/
private static Map<OutputFile, String> getPathInfo() {
Map<OutputFile, String> pathInfo = new HashMap<>(5);
pathInfo.put(OutputFile.entity, ENTITY_PATH);
pathInfo.put(OutputFile.mapper, MAPPER_PATH);
pathInfo.put(OutputFile.service, SERVICE_PATH);
pathInfo.put(OutputFile.serviceImpl, SERVICE_IMPL_PATH);
pathInfo.put(OutputFile.controller, CONTROLLER_PATH);
pathInfo.put(OutputFile.xml, XML_PATH);
return pathInfo;
}
// 处理 all 情况
protected static List<String> getTables (String tables){
return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
}
}
自定义模板
模板代码(一般放置于当前resources包下的templates目录下)
例如:修改官方默认lombok表达,改为@Data
package ${package.Entity};
<#list table.importPackages as pkg>
import ${pkg};
</#list>
<#if swagger>
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
</#if>
<#if entityLombokModel>
import lombok.Data;
<#if chainModel>
import lombok.experimental.Accessors;
</#if>
</#if>
/**
* <p>
* ${table.comment!}
* </p>
*
* @author ${author}
* @since ${date}
*/
<#if entityLombokModel>
@Data
<#if chainModel>
@Accessors(chain = true)
</#if>
</#if>
<#if table.convert>
@TableName("${schemaName}${table.name}")
</#if>
<#if swagger>
@ApiModel(value = "${entity}对象", description = "${table.comment!}")
</#if>
<#if superEntityClass??>
public class ${entity} extends ${superEntityClass}<#if activeRecord><${entity}></#if> {
<#elseif activeRecord>
public class ${entity} extends Model<${entity}> {
<#elseif entitySerialVersionUID>
public class ${entity} implements Serializable {
<#else>
public class ${entity} {
</#if>
<#if entitySerialVersionUID>
private static final long serialVersionUID = 1L;
</#if>
<#-- ---------- BEGIN 字段循环遍历 ---------->
<#list table.fields as field>
<#if field.keyFlag>
<#assign keyPropertyName="${field.propertyName}"/>
</#if>
<#if field.comment!?length gt 0>
<#if swagger>
@ApiModelProperty("${field.comment}")
<#else>
/**
* ${field.comment}
*/
</#if>
</#if>
<#if field.keyFlag>
<#-- 主键 -->
<#if field.keyIdentityFlag>
@TableId(value = "${field.annotationColumnName}", type = IdType.AUTO)
<#elseif idType??>
@TableId(value = "${field.annotationColumnName}", type = IdType.${idType})
<#elseif field.convert>
@TableId("${field.annotationColumnName}")
</#if>
<#-- 普通字段 -->
<#elseif field.fill??>
<#-- ----- 存在字段填充设置 ----->
<#if field.convert>
@TableField(value = "${field.annotationColumnName}", fill = FieldFill.${field.fill})
<#else>
@TableField(fill = FieldFill.${field.fill})
</#if>
<#elseif field.convert>
@TableField("${field.annotationColumnName}")
</#if>
<#-- 乐观锁注解 -->
<#if field.versionField>
@Version
</#if>
<#-- 逻辑删除注解 -->
<#if field.logicDeleteField>
@TableLogic
</#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 chainModel>
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 chainModel>
return this;
</#if>
}
</#list>
</#if>
<#if entityColumnConstant>
<#list table.fields as field>
public static final String ${field.name?upper_case} = "${field.name}";
</#list>
</#if>
<#if activeRecord>
@Override
public Serializable pkVal() {
<#if keyPropertyName??>
return this.${keyPropertyName};
<#else>
return null;
</#if>
}
</#if>
<#if !entityLombokModel>
@Override
public String toString() {
return "${entity}{" +
<#list table.fields as field>
<#if field_index==0>
"${field.propertyName}=" + ${field.propertyName} +
<#else>
", ${field.propertyName}=" + ${field.propertyName} +
</#if>
</#list>
"}";
}
</#if>
}