MyBatis-Plus在3.5.1版本(包含)之后发布了新版本的代码生成器,旧版本代码生成器在3.5.1版本之后无法继续使用。
直接贴代码,工具类如下:
CodeGeneratorNew.java
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import java.sql.Types;
import java.util.Collections;
/**
* @Author:
* @Time:
* @Description: mybatis-plus 3.5.1版本(包含3.5.1) 使用新的代码生成器
* 官方配置文档地址: https://baomidou.com/reference/new-code-generator-configuration/
*/
public class CodeGeneratorNew {
private final static String AUTHOR = "XXXX"; //作者
private final static String DATABASE = "XXX_XXX"; //数据库名称
private final static String URL = "jdbc:mysql://xxx.xxx.x.xx:3306/"+DATABASE+"?serverTimezone=Asia/Shanghai&useUnicode=true&useSSL=false&characterEncoding=utf8";
private final static String DATABASE_USERNAME = "user";//数据库链接用户名
private final static String DATABASE_PASSWORD = "xxxxxxxx";//数据库链接用户密码
private final static String PROJECT = "com.xxx.xxxx";//父包名
private final static String MODEL_NAME = "xxxx";//模块名称
private final static String[] DATABASES_ARR = new String[]{"sys_xxxx_test"};//表名 支持多个
public static void main(String[] args) {
String projectPath = System.getProperty("user.dir");
String outputDir = projectPath + "/xxxx-xxxx/src/main/java";
FastAutoGenerator.create(URL, DATABASE_USERNAME, DATABASE_PASSWORD)
// 全局配置
.globalConfig(builder -> {
builder.author(AUTHOR) // 设置作者
.disableOpenDir() // 禁止自动打开输出目录
.enableSwagger()
.outputDir(outputDir); // 指定输出目录
})
// 数据库配置
.dataSourceConfig(builder ->
builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
int typeCode = metaInfo.getJdbcType().TYPE_CODE;
if (typeCode == Types.SMALLINT) {
// 自定义类型转换
return DbColumnType.INTEGER;
}
return typeRegistry.getColumnType(metaInfo);
})
)
// 包设置
.packageConfig(builder ->
builder.parent(PROJECT) // 设置父包名
.moduleName(MODEL_NAME) // 设置父包模块名
//.pathInfo(Collections.singletonMap(OutputFile.xml, "D://")) // 设置mapperXml生成路径
)
.strategyConfig(builder ->
builder.addInclude(DATABASES_ARR) // 设置需要生成的表名
.addTablePrefix("xxxx") // 设置过滤表前缀
.entityBuilder()
.enableLombok() // 开启 Lombok 模型
.enableFileOverride() // 开启覆盖已生成文件 仅开启实体类
.controllerBuilder()
.enableHyphenStyle() // 开启驼峰转连字符
.enableRestStyle() // 开启生成@RestController 控制器
)
.templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
.execute();
System.out.println("表:" + ArrayUtils.toString(DATABASES_ARR) + "代码生成成功!");
}
}
上述工具类已经可以快速生成,直接复制代码就能行,需要拓展更详细的配置可以自己参考官方文档进行配置,代码里有官方配置文档链接。
模板用的freemarker,需要在 resource/ templates/ 存在相关的DTO模板ftl,这个自行添加,本文将不提供源码。
附上相关所需依赖:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
550

被折叠的 条评论
为什么被折叠?



