MyBatisPlus3.x中使用代码生成器(全注释)
作者:BADAO_LIUMANG_QIZHI 时间:2022-04-06 19:34:38
场景
MyBaitsPlus3.x与2.x是不一样的。这里使用3.0.1版本。
官方文档
https://mp.baomidou.com/guide/generator.html
这里在IDEA上的SpringBoot项目中进行代码生成测试。
实现
添加依赖
添加 代码生成器 依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.2.0</version>
</dependency>
注意:MyBatis-Plus 从 3.0.3 之后移除了代码生成器与模板引擎的默认依赖,需要手动添加相关依赖
<!-- 模板引擎velocity start-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
<!-- 模板引擎velocity end-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.0.1</version>
</dependency>
模板引擎
MyBatis-Plus 支持 Velocity(默认)、Freemarker、Beetl,用户可以选择自己熟悉的模板引擎。
注意!如果您选择了非默认引擎,需要在 AutoGenerator 中 设置模板引擎。
AutoGenerator generator = new AutoGenerator();
// set freemarker engine
generator.setTemplateEngine(new FreemarkerTemplateEngine());
// set beetl engine
generator.setTemplateEngine(new BeetlTemplateEngine());
// set custom engine (reference class is your custom engine class)
generator.setTemplateEngine(new CustomTemplateEngine());
编写代码
在test下新建单元测试类Generatortest.java
全局配置
//全局配置
GlobalConfig config = new GlobalConfig();
//设置是否支持AR模式
config.setActiveRecord(true)
//设置生成代码的作者
.setAuthor("badaoliumangqizhi")
//设置输出代码的位置
.setOutputDir("f:output")
//.setEnableCache(false)// XML 二级缓存
//.setBaseResultMap(true)// XML ResultMap
//.setBaseColumnList(true)// XML columList
//.setKotlin(true) 是否生成 kotlin 代码
//设置是否覆盖原来的代码
.setFileOverride(true);
数据源配置
//数据库连接url
String dbUrl = "jdbc:sqlserver://;DatabaseName=";
//数据源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
//数据库类型 枚举
dataSourceConfig.setDbType(DbType.SQL_SERVER)
//设置url
.setUrl(dbUrl)
//设置用户名
.setUsername("")
//设置密码
.setPassword("")
//设置数据库驱动
.setDriverName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
// 自定义数据库表字段类型转换【可选】
.setTypeConvert(new MySqlTypeConvert() {
@Override
public DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
System.out.println("转换类型:" + fieldType);
//tinyint转换成Boolean
if ( fieldType.toLowerCase().contains( "tinyint" ) ) {
return DbColumnType.BOOLEAN;
}
//将数据库中datetime转换成date
if ( fieldType.toLowerCase().contains( "datetime" ) ) {
return DbColumnType.DATE;
}
return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);
}
});
策略配置
//策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig
//全局大写命名是否开启
.setCapitalMode(true)
//【实体】是否为lombok模型
.setEntityLombokModel(true)
//表名生成策略 下划线转驼峰
.setNaming(NamingStrategy.underline_to_camel)
//自动填充设置
//.setTableFillList(tableFillList)
//修改替换成你需要的表名,多个表名传数组
.setInclude("wms_receive_order");
集成注入配置
//注入全局设置
new AutoGenerator().setGlobalConfig(config)
//注入数据源配置
.setDataSource(dataSourceConfig)
//注入策略配置
.setStrategy(strategyConfig)
//设置包名信息
.setPackageInfo(
new PackageConfig()
//提取公共父级包名
.setParent("com.badao.bus.sys")
//设置controller信息
.setController("controller")
//设置实体类信息
.setEntity("entity")
)
//设置自定义模板
.setTemplate(
new TemplateConfig()
//.setXml(null)//指定自定义模板路径, 位置:/resources/templates/entity2.java.ftl(或者是.vm)
//注意不要带上.ftl(或者是.vm), 会根据使用的模板引擎自动识别
// 自定义模板配置,模板可以参考源码 /mybatis-plus/src/main/resources/template 使用 copy
// 至您项目 src/main/resources/template 目录下,模板名称也可自定义如下配置:
// .setController("...");
// .setEntity("...");
// .setMapper("...");
// .setXml("...");
// .setService("...");
.setServiceImpl("templates/serviceImpl.java")
)
//开始执行代码生成
.execute();
}
完整生成器代码
package com.ws.test.generator;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import org.junit.Test;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import java.util.ArrayList;
import java.util.List;
/**
* Created by badado on 2019/4/25.
*/
public class Generatortest {
@Test
public void generateTest(){
//全局配置
GlobalConfig config = new GlobalConfig();
//设置是否支持AR模式
config.setActiveRecord(true)
//设置生成代码的作者
.setAuthor("badaoliumangqizhi")
//设置输出代码的位置
.setOutputDir("f:output")
//.setEnableCache(false)// XML 二级缓存
//.setBaseResultMap(true)// XML ResultMap
//.setBaseColumnList(true)// XML columList
//.setKotlin(true) 是否生成 kotlin 代码
//设置是否覆盖原来的代码
.setFileOverride(true);
//******************************数据源配置***************************************
//数据库连接url
String dbUrl = "jdbc:sqlserver://;DatabaseName=";
//数据源配置
DataSourceConfig dataSourceConfig = new DataSourceConfig();
//数据库类型 枚举
dataSourceConfig.setDbType(DbType.SQL_SERVER)
//设置url
.setUrl(dbUrl)
//设置用户名
.setUsername("")
//设置密码
.setPassword("")
//设置数据库驱动
.setDriverName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
// 自定义数据库表字段类型转换【可选】
.setTypeConvert(new MySqlTypeConvert() {
@Override
public DbColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
System.out.println("转换类型:" + fieldType);
//tinyint转换成Boolean
if ( fieldType.toLowerCase().contains( "tinyint" ) ) {
return DbColumnType.BOOLEAN;
}
//将数据库中datetime转换成date
if ( fieldType.toLowerCase().contains( "datetime" ) ) {
return DbColumnType.DATE;
}
return (DbColumnType) super.processTypeConvert(globalConfig, fieldType);
}
});
//******************************策略配置******************************************************
// 自定义需要填充的字段 数据库中的字段
List<TableFill> tableFillList = new ArrayList<>();
tableFillList.add(new TableFill("gmt_modified", FieldFill.INSERT_UPDATE));
tableFillList.add(new TableFill("modifier_id", FieldFill.INSERT_UPDATE));
tableFillList.add(new TableFill("creator_id", FieldFill.INSERT));
tableFillList.add(new TableFill("gmt_creat", FieldFill.INSERT));
tableFillList.add(new TableFill("available_flag", FieldFill.INSERT));
tableFillList.add(new TableFill("deleted_flag", FieldFill.INSERT));
tableFillList.add(new TableFill("sync_flag", FieldFill.INSERT));
//策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig
//全局大写命名是否开启
.setCapitalMode(true)
//【实体】是否为lombok模型
.setEntityLombokModel(true)
//表名生成策略 下划线转驼峰
.setNaming(NamingStrategy.underline_to_camel)
//自动填充设置
.setTableFillList(tableFillList)
//修改替换成你需要的表名,多个表名传数组
.setInclude("wms_receive_order");
//集成注入设置
//注入全局设置
new AutoGenerator().setGlobalConfig(config)
//注入数据源配置
.setDataSource(dataSourceConfig)
//注入策略配置
.setStrategy(strategyConfig)
//设置包名信息
.setPackageInfo(
new PackageConfig()
//提取公共父级包名
.setParent("com.badao.bus.sys")
//设置controller信息
.setController("controller")
//设置实体类信息
.setEntity("entity")
)
//设置自定义模板
.setTemplate(
new TemplateConfig()
//.setXml(null)//指定自定义模板路径, 位置:/resources/templates/entity2.java.ftl(或者是.vm)
//注意不要带上.ftl(或者是.vm), 会根据使用的模板引擎自动识别
// 自定义模板配置,模板可以参考源码 /mybatis-plus/src/main/resources/template 使用 copy
// 至您项目 src/main/resources/template 目录下,模板名称也可自定义如下配置:
// .setController("...");
// .setEntity("...");
// .setMapper("...");
// .setXml("...");
// .setService("...");
.setServiceImpl("templates/serviceImpl.java")
)
//开始执行代码生成
.execute();
}
}
效果
运行测试
生成成功后会自动弹出生成代码的目录
来源:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/89518466
标签:MyBatisPlus,代码生成器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#结束进程及子进程
2021-06-22 15:18:10
Spring Data Jpa 复杂查询方式总结(多表关联及自定义分页)
2023-11-21 10:01:27
![](https://img.aspxhome.com/file/2023/2/59722_0s.png)
Mybatis中where标签与if标签结合使用详细说明
2021-07-27 08:15:53
Spring自动装配之方法、构造器位置的自动注入操作
2021-11-30 23:28:40
unity 实现摄像机绕某点旋转一周
2021-06-11 16:48:57
Java 详解Collection集合之ArrayList和HashSet
2021-09-10 06:27:02
![](https://img.aspxhome.com/file/2023/6/113516_0s.png)
详解SpringBoot注册Windows服务和启动报错的原因
2022-12-28 17:10:09
![](https://img.aspxhome.com/file/2023/1/63431_0s.jpg)
Android 微信摇一摇功能实现详细介绍
2023-06-21 21:00:09
c#中WebService的介绍及调用方式小结
2021-05-29 00:10:17
![](https://img.aspxhome.com/file/2023/5/113835_0s.jpg)
Android巧用XListView实现万能下拉刷新控件
2023-07-25 00:33:03
基于Java代码操作Redis过程详解
2022-03-28 06:53:58
![](https://img.aspxhome.com/file/2023/9/113259_0s.jpg)
Spring Boot中的那些条件判断的实现方法
2023-04-26 15:02:07
Effective Java (异常处理)
2022-09-24 12:17:29
C#中Lambda表达式的三种写法
2022-03-27 02:50:27
Spring Security基于JWT实现SSO单点登录详解
2022-02-28 10:56:55
![](https://img.aspxhome.com/file/2023/5/71585_0s.jpg)
深入解读Android的Volley库的功能结构
2023-02-28 00:57:12
![](https://img.aspxhome.com/file/2023/5/116005_0s.jpg)
Unity封装延时调用定时器
2022-10-14 05:43:53
Springcloud-nacos实现配置和注册中心的方法
2023-06-15 13:46:42
Android 简单实现倒计时功能
2023-06-27 11:35:40
![](https://img.aspxhome.com/file/2023/5/91035_0s.jpg)
C#设计模式之适配器模式与装饰器模式的实现
2021-10-30 02:54:32
![](https://img.aspxhome.com/file/2023/8/108718_0s.png)