代码生成器
This commit is contained in:
@@ -3,22 +3,29 @@ package com.czg;
|
||||
import com.mybatisflex.codegen.Generator;
|
||||
import com.mybatisflex.codegen.config.ColumnConfig;
|
||||
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 ww
|
||||
*/
|
||||
public class Main {
|
||||
private final static String BASE_URL = "obmt6die8bni6i00-mi.aliyun-cn-hangzhou-internet.oceanbase.cloud";
|
||||
private final static String PORT = "3306";
|
||||
private final static String USERNAME = "admin";
|
||||
private final static String PASSWORD = "kWF9I/3[Mu,H";
|
||||
private final static String DATABASE = "czg_cashier";
|
||||
|
||||
public static void main(String[] args) {
|
||||
//配置数据源
|
||||
HikariDataSource dataSource = new HikariDataSource();
|
||||
dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/your-database?characterEncoding=utf-8");
|
||||
dataSource.setUsername("root");
|
||||
dataSource.setPassword("******");
|
||||
dataSource.setJdbcUrl("jdbc:mysql://" + BASE_URL + ":" + PORT + "/" + DATABASE);
|
||||
dataSource.setUsername(USERNAME);
|
||||
dataSource.setPassword(PASSWORD);
|
||||
|
||||
//创建配置内容,两种风格都可以。
|
||||
GlobalConfig globalConfig = createGlobalConfigUseStyle1();
|
||||
//GlobalConfig globalConfig = createGlobalConfigUseStyle2();
|
||||
GlobalConfig globalConfig = createGlobalConfigUseStyle();
|
||||
|
||||
//通过 datasource 和 globalConfig 创建代码生成器
|
||||
Generator generator = new Generator(dataSource, globalConfig);
|
||||
@@ -27,65 +34,77 @@ public class Main {
|
||||
generator.generate();
|
||||
}
|
||||
|
||||
public static GlobalConfig createGlobalConfigUseStyle1() {
|
||||
public static GlobalConfig createGlobalConfigUseStyle() {
|
||||
//创建配置内容
|
||||
GlobalConfig globalConfig = new GlobalConfig();
|
||||
|
||||
//设置根包
|
||||
globalConfig.setBasePackage("com.test");
|
||||
|
||||
//设置表前缀和只生成哪些表
|
||||
globalConfig.setTablePrefix("tb_");
|
||||
globalConfig.setGenerateTable("tb_account", "tb_account_session");
|
||||
|
||||
//设置生成 entity 并启用 Lombok
|
||||
globalConfig.setEntityGenerateEnable(true);
|
||||
globalConfig.setEntityWithLombok(true);
|
||||
//设置项目的JDK版本,项目的JDK为14及以上时建议设置该项,小于14则可以不设置
|
||||
globalConfig.setEntityJdkVersion(17);
|
||||
|
||||
//设置生成 mapper
|
||||
globalConfig.setMapperGenerateEnable(true);
|
||||
|
||||
//可以单独配置某个列
|
||||
ColumnConfig columnConfig = new ColumnConfig();
|
||||
columnConfig.setColumnName("tenant_id");
|
||||
columnConfig.setLarge(true);
|
||||
columnConfig.setVersion(true);
|
||||
globalConfig.setColumnConfig("tb_account", columnConfig);
|
||||
|
||||
return globalConfig;
|
||||
}
|
||||
|
||||
public static GlobalConfig createGlobalConfigUseStyle2() {
|
||||
//创建配置内容
|
||||
GlobalConfig globalConfig = new GlobalConfig();
|
||||
String currentWorkingDirectory = System.getProperty("user.dir");
|
||||
String projectPath = currentWorkingDirectory + "/cash-service/system-service";
|
||||
// String projectPath = currentWorkingDirectory + "/cash-service/account-service";
|
||||
// String projectPath = currentWorkingDirectory + "/cash-service/product-service";
|
||||
// String projectPath = currentWorkingDirectory + "/cash-service/order-service";
|
||||
|
||||
//设置根包
|
||||
globalConfig.getPackageConfig()
|
||||
.setBasePackage("com.test");
|
||||
.setSourceDir(projectPath + "/src/main/java")
|
||||
.setBasePackage("com.czg.service.system");
|
||||
|
||||
globalConfig.getServiceConfig()
|
||||
.setClassSuffix("Service")
|
||||
.setSuperClass(IService.class);
|
||||
globalConfig.enableService();
|
||||
|
||||
globalConfig.getServiceImplConfig()
|
||||
.setClassSuffix("ServiceImpl")
|
||||
.setSuperClass(ServiceImpl.class);
|
||||
globalConfig.enableServiceImpl();
|
||||
|
||||
globalConfig.getMapperConfig()
|
||||
.setClassSuffix("Mapper");
|
||||
globalConfig.enableMapper();
|
||||
|
||||
globalConfig.setMapperXmlPath(projectPath + "/src/main/resources/mapper");
|
||||
globalConfig.enableMapperXml();
|
||||
|
||||
//设置表前缀和只生成哪些表,setGenerateTable 未配置时,生成所有表
|
||||
globalConfig.getStrategyConfig()
|
||||
.setTablePrefix("tb_")
|
||||
.setGenerateTable("tb_account", "tb_account_session");
|
||||
.setTablePrefix("")
|
||||
.setGenerateTable("sys_params");
|
||||
|
||||
//设置生成 entity 并启用 Lombok
|
||||
globalConfig.enableEntity()
|
||||
.setWithLombok(true)
|
||||
.setJdkVersion(17);
|
||||
.setJdkVersion(23);
|
||||
|
||||
//设置生成 mapper
|
||||
globalConfig.enableMapper();
|
||||
|
||||
//可以单独配置某个列
|
||||
ColumnConfig columnConfig = new ColumnConfig();
|
||||
columnConfig.setColumnName("tenant_id");
|
||||
columnConfig.setLarge(true);
|
||||
columnConfig.setVersion(true);
|
||||
ColumnConfig createTime = new ColumnConfig();
|
||||
createTime.setColumnName("create_time");
|
||||
createTime.setOnInsertValue("now()");
|
||||
globalConfig.getStrategyConfig()
|
||||
.setColumnConfig("tb_account", columnConfig);
|
||||
.setColumnConfig(createTime);
|
||||
|
||||
ColumnConfig updateTime = new ColumnConfig();
|
||||
updateTime.setColumnName("update_time");
|
||||
updateTime.setOnUpdateValue("now()");
|
||||
updateTime.setOnInsertValue("now()");
|
||||
globalConfig.getStrategyConfig()
|
||||
.setColumnConfig(updateTime);
|
||||
|
||||
ColumnConfig deleted = new ColumnConfig();
|
||||
deleted.setColumnName("is_del");
|
||||
deleted.setLogicDelete(true);
|
||||
globalConfig.getStrategyConfig()
|
||||
.setColumnConfig(deleted);
|
||||
|
||||
// ColumnConfig version = new ColumnConfig();
|
||||
// version.setColumnName("version");
|
||||
// version.setVersion(true);
|
||||
// globalConfig.getStrategyConfig()
|
||||
// .setColumnConfig(version);
|
||||
|
||||
return globalConfig;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user