spring-boot 整合mybatis反向代碼生成
一、引入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 - ==========================文件生成完成!!!==========================
圖示
出現上述結果就說明運行成功,可以去項目裏查看了。