代码生成器
This commit is contained in:
parent
3b408b8fbd
commit
780245f8c2
75
src/main/java/com/huangge1199/blog/config/Codegen.java
Normal file
75
src/main/java/com/huangge1199/blog/config/Codegen.java
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package com.huangge1199.blog.config;
|
||||||
|
|
||||||
|
import com.mybatisflex.codegen.Generator;
|
||||||
|
import com.mybatisflex.codegen.config.GlobalConfig;
|
||||||
|
import com.mybatisflex.core.service.IService;
|
||||||
|
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
||||||
|
import com.zaxxer.hikari.HikariDataSource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author hyy
|
||||||
|
* @Classname Codegen
|
||||||
|
* @Description 代码生成器
|
||||||
|
* @Date 2024/3/13 13:31:21
|
||||||
|
*/
|
||||||
|
public class Codegen {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// 配置数据源
|
||||||
|
HikariDataSource dataSource = new HikariDataSource();
|
||||||
|
dataSource.setJdbcUrl("jdbc:mysql://192.168.0.197:3306/blog?characterEncoding=utf-8");
|
||||||
|
dataSource.setUsername("root");
|
||||||
|
dataSource.setPassword("huangge1199");
|
||||||
|
|
||||||
|
// 创建配置内容,两种风格都可以。
|
||||||
|
GlobalConfig globalConfig = createGlobalConfigUseStyle();
|
||||||
|
|
||||||
|
// 通过 datasource 和 globalConfig 创建代码生成器
|
||||||
|
Generator generator = new Generator(dataSource, globalConfig);
|
||||||
|
|
||||||
|
// 生成代码
|
||||||
|
generator.generate();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GlobalConfig createGlobalConfigUseStyle() {
|
||||||
|
// 创建配置内容
|
||||||
|
GlobalConfig globalConfig = new GlobalConfig();
|
||||||
|
|
||||||
|
// 设置根包
|
||||||
|
globalConfig.getPackageConfig()
|
||||||
|
.setBasePackage("com.huangge1199.blog");
|
||||||
|
|
||||||
|
// 设置表前缀和只生成哪些表,setGenerateTable 未配置时,生成所有表
|
||||||
|
globalConfig.getStrategyConfig()
|
||||||
|
.setTablePrefix("tb_");
|
||||||
|
|
||||||
|
// 设置生成 entity 并启用 Lombok
|
||||||
|
globalConfig.enableEntity()
|
||||||
|
.setWithLombok(true)
|
||||||
|
.setJdkVersion(17);
|
||||||
|
|
||||||
|
// 设置生成 mapper
|
||||||
|
globalConfig.enableMapper()
|
||||||
|
.setClassSuffix("Mapper");
|
||||||
|
|
||||||
|
// 设置生成 service
|
||||||
|
globalConfig.getServiceConfig()
|
||||||
|
.setClassSuffix("Service")
|
||||||
|
.setSuperClass(IService.class);
|
||||||
|
|
||||||
|
// 设置生成 serviceImpl
|
||||||
|
globalConfig.getServiceImplConfig()
|
||||||
|
.setClassSuffix("ServiceImpl")
|
||||||
|
.setSuperClass(ServiceImpl.class);
|
||||||
|
|
||||||
|
// 设置生成 controller
|
||||||
|
globalConfig.getControllerConfig()
|
||||||
|
.setClassSuffix("Controller");
|
||||||
|
|
||||||
|
// 设置生成 mapperXml
|
||||||
|
globalConfig.getMapperXmlConfig()
|
||||||
|
.setFileSuffix("Mapper");
|
||||||
|
|
||||||
|
return globalConfig;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user