spring-boot 整合mybatis-plus反向代码生成
一、引入maven依赖包
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
二、创建反向生成类
public class MyTest {
@Test
public void testGenerator() {
AutoGenerator autoGenerator = new AutoGenerator();
//全局配置
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setAuthor("ttb")
.setOutputDir("E:\\self_project\\ttb\\src\\main\\java")//设置输出路径
.setFileOverride(true)//设置文件覆盖
.setIdType(IdType.AUTO)//设置主键生成策略
.setServiceName("%sService")//service接口的名称
.setBaseResultMap(true)//基本结果集合
.setBaseColumnList(true)//设置基本的列
.setControllerName("%sController");
//配置数据源
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setDriverName("com.mysql.jdbc.Driver").setUrl("jdbc:mysql://localhost:3306/base?serverTimezone=UTC")
.setUsername("root").setPassword("123456");
//策略配置
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setCapitalMode(true)//设置全局大写命名
.setNaming(NamingStrategy.underline_to_camel)//数据库表映射到实体的命名策略
.setInclude();
//包名配置
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent("com.ttb").setMapper("mapper")
.setService("service").setController("controller")
.setEntity("bean").setXml("mapper");
autoGenerator.setGlobalConfig(globalConfig).setDataSource(dataSourceConfig)
.setStrategy(strategyConfig).setPackageInfo(packageConfig);
autoGenerator.execute();
}
}
三、运行结果
18:12:02.668 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================准备生成文件...==========================
18:12:04.779 [main] WARN org.apache.velocity.deprecation - configuration key 'file.resource.loader.unicode' has been deprecated in favor of 'resource.loader.file.unicode'
18:12:04.781 [main] WARN org.apache.velocity.deprecation - configuration key 'file.resource.loader.class' has been deprecated in favor of 'resource.loader.file.class'
18:12:04.783 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [F:\A_xtb\mytest\hejiayun\hjiayunMytest\src\main\java\com\xtb\hjyun\bean]
18:12:04.784 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [F:\A_xtb\mytest\hejiayun\hjiayunMytest\src\main\java\com\xtb\hjyun\controller]
18:12:04.784 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [F:\A_xtb\mytest\hejiayun\hjiayunMytest\src\main\java\com\xtb\hjyun\mapper]
18:12:04.785 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [F:\A_xtb\mytest\hejiayun\hjiayunMytest\src\main\java\com\xtb\hjyun\service]
18:12:04.786 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 创建目录: [F:\A_xtb\mytest\hejiayun\hjiayunMytest\src\main\java\com\xtb\hjyun\service\impl]
...........................................................
..........................................................
18:12:10.363 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/serviceImpl.java.vm; 文件:F:\A_xtb\mytest\hejiayun\hjiayunMytest\src\main\java\com\xtb\hjyun\service\impl\ZhRentTransferServiceImpl.java
18:12:10.363 [main] DEBUG org.apache.velocity.loader - ResourceManager: found /templates/controller.java.vm with loader org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
18:12:10.365 [main] DEBUG com.baomidou.mybatisplus.generator.engine.AbstractTemplateEngine - 模板:/templates/controller.java.vm; 文件:F:\A_xtb\mytest\hejiayun\hjiayunMytest\src\main\java\com\xtb\hjyun\controller\ZhRentTransferController.java
18:12:10.378 [main] DEBUG com.baomidou.mybatisplus.generator.AutoGenerator - ==========================文件生成完成!!!==========================
图示
出现上述结果就说明运行成功,可以去项目里查看了。