This commit is contained in:
开源海哥 2023-07-14 10:22:50 +08:00
commit a6ea26d272
9 changed files with 100 additions and 42 deletions

View File

@ -250,7 +250,7 @@ public class Book implements Serializable {
## 多对多 `@RelationManyToOne`
## 多对多 `@RelationManyToMany`
假设一个账户可以有多个角色,一个角色也可以有多个账户,他们是多对多的关系,需要通过中间件表 `tb_role_mapping` 来维护:

View File

@ -56,6 +56,11 @@ public class EntityConfig {
*/
private boolean withLombok;
/**
* 实体类数据源
*/
private String dataSource;
/**
* 获取类前缀
*/
@ -146,4 +151,19 @@ public class EntityConfig {
return this;
}
}
/**
* 获取实体类数据源
*/
public String getDataSource() {
return dataSource;
}
/**
* 设置实体类数据源
*/
public EntityConfig setDataSource(String dataSource) {
this.dataSource = dataSource;
return this;
}
}

View File

@ -877,6 +877,20 @@ public class GlobalConfig {
getEntityConfig().setWithLombok(entityWithLombok);
}
/**
* @see EntityConfig#getDataSource()
*/
public String getEntityDataSource() {
return getEntityConfig().getDataSource();
}
/**
* @see EntityConfig#setDataSource(String)
*/
public void setEntityDataSource(String dataSource) {
getEntityConfig().setDataSource(dataSource);
}
public boolean isMapperGenerateEnable() {
return mapperGenerateEnable;
}
@ -1248,28 +1262,28 @@ public class GlobalConfig {
/**
* @see TableDefConfig#getPropertiesNameStyle()
*/
public TableDefConfig.NameStyle getPropertiesNameStyle() {
public TableDefConfig.NameStyle getTableDefPropertiesNameStyle() {
return getTableDefConfig().getPropertiesNameStyle();
}
/**
* @see TableDefConfig#setPropertiesNameStyle(TableDefConfig.NameStyle)
*/
public void setPropertiesNameStyle(TableDefConfig.NameStyle propertiesNameStyle) {
public void setTableDefPropertiesNameStyle(TableDefConfig.NameStyle propertiesNameStyle) {
getTableDefConfig().setPropertiesNameStyle(propertiesNameStyle);
}
/**
* @see TableDefConfig#getInstanceSuffix()
*/
public String getInstanceSuffix() {
public String getTableDefInstanceSuffix() {
return getTableDefConfig().getInstanceSuffix();
}
/**
* @see TableDefConfig#setInstanceSuffix(String)
*/
public void setInstanceSuffix(String instanceSuffix) {
public void setTableDefInstanceSuffix(String instanceSuffix) {
getTableDefConfig().setInstanceSuffix(instanceSuffix);
}

View File

@ -223,6 +223,12 @@ public class Table {
tableAnnotation.append(", schema = \"").append(globalSchema).append("\"");
}
// 添加 dataSource 配置因为代码生成器是一个数据源生成的所以这些实体类应该都是一个数据源
String dataSource = globalConfig.getEntityDataSource();
if (StringUtil.isNotBlank(dataSource)) {
tableAnnotation.append(", dataSource = \"").append(dataSource).append("\"");
}
if (tableConfig != null) {
if (StringUtil.isNotBlank(tableConfig.getSchema())) {
tableAnnotation.append(", schema = \"").append(tableConfig.getSchema()).append("\"");

View File

@ -23,6 +23,7 @@ import com.mybatisflex.codegen.config.TableConfig;
import com.mybatisflex.codegen.config.TableDefConfig;
import com.mybatisflex.spring.service.impl.CacheableServiceImpl;
import com.zaxxer.hikari.HikariDataSource;
import org.junit.Test;
import java.util.function.UnaryOperator;
@ -100,19 +101,19 @@ public class GeneratorTest {
//设置注解生成配置
globalConfig.getJavadocConfig()
.setAuthor("王帅")
.setTableCommentFormat(tableFormat);
.setAuthor("王帅")
.setTableCommentFormat(tableFormat);
//设置生成文件目录和根包
globalConfig.getPackageConfig()
.setSourceDir(System.getProperty("user.dir") + "/src/test/java")
.setMapperXmlPath(System.getProperty("user.dir") + "/src/test/java/resources/mapper")
.setBasePackage("com.test");
.setSourceDir(System.getProperty("user.dir") + "/src/test/java")
.setMapperXmlPath(System.getProperty("user.dir") + "/src/test/java/resources/mapper")
.setBasePackage("com.test");
//设置表前缀和只生成哪些表
globalConfig.getStrategyConfig()
.setTablePrefix("sys_")
.setGenerateTable("sys_user");
.setTablePrefix("sys_")
.setGenerateTable("sys_user");
//设置模板路径
//globalConfig.getTemplateConfig()
@ -120,9 +121,9 @@ public class GeneratorTest {
//配置生成 entity
globalConfig.enableEntity()
.setOverwriteEnable(true)
.setWithLombok(true)
.setSupperClass(BaseEntity.class);
.setOverwriteEnable(true)
.setWithLombok(true)
.setSupperClass(BaseEntity.class);
//配置生成 mapper
globalConfig.enableMapper();
@ -130,8 +131,8 @@ public class GeneratorTest {
globalConfig.enableService();
//配置生成 serviceImpl
globalConfig.enableServiceImpl()
.setSupperClass(CacheableServiceImpl.class)
.setCacheExample(true);
.setSupperClass(CacheableServiceImpl.class)
.setCacheExample(true);
//配置生成 controller
globalConfig.enableController();
//配置生成 tableDef
@ -148,7 +149,7 @@ public class GeneratorTest {
generator.generate();
}
// @Test
@Test
public void testCodeGen3() {
//配置数据源
HikariDataSource dataSource = new HikariDataSource();
@ -163,14 +164,14 @@ public class GeneratorTest {
//设置注解生成配置
globalConfig.getJavadocConfig()
.setAuthor("王帅")
.setTableCommentFormat(tableFormat);
.setAuthor("王帅")
.setTableCommentFormat(tableFormat);
//设置生成文件目录和根包
globalConfig.getPackageConfig()
.setSourceDir(System.getProperty("user.dir") + "/src/test/java")
.setMapperXmlPath(System.getProperty("user.dir") + "/src/test/java/resources/mapper")
.setBasePackage("com.test");
.setSourceDir(System.getProperty("user.dir") + "/src/test/java")
.setMapperXmlPath(System.getProperty("user.dir") + "/src/test/java/resources/mapper")
.setBasePackage("com.test");
ColumnConfig columnConfig = new ColumnConfig();
columnConfig.setColumnName("phonenumber");
@ -186,26 +187,27 @@ public class GeneratorTest {
//设置表前缀和只生成哪些表
globalConfig.getStrategyConfig()
.setTablePrefix("sys_")
.setGenerateTable("sys_user")
.setColumnConfig(logicDelete)
.setTableConfig(tableConfig);
.setTablePrefix("sys_")
.setGenerateTable("sys_user")
.setColumnConfig(logicDelete)
.setTableConfig(tableConfig);
//配置生成 tableDef
globalConfig.enableTableDef()
.setInstanceSuffix("Def")
.setPropertiesNameStyle(TableDefConfig.NameStyle.LOWER_CAMEL_CASE)
.setOverwriteEnable(true);
.setInstanceSuffix("Def")
.setPropertiesNameStyle(TableDefConfig.NameStyle.LOWER_CAMEL_CASE)
.setOverwriteEnable(true);
// 配置生成 entity
globalConfig.enableEntity()
.setOverwriteEnable(true)
.setWithLombok(true);
.setOverwriteEnable(true)
.setDataSource("ds1")
.setWithLombok(true);
// 配置生成 mapper
globalConfig.enableMapper()
.setOverwriteEnable(true)
.setMapperAnnotation(true);
.setOverwriteEnable(true)
.setMapperAnnotation(true);
//通过 datasource globalConfig 创建代码生成器
Generator generator = new Generator(dataSource, globalConfig);

View File

@ -121,6 +121,7 @@ public class MybatisFlexProcessor extends AbstractProcessor {
// mapper 配置
String mapperGenerateEnable = configuration.get(ConfigurationKey.MAPPER_GENERATE_ENABLE);
String mapperAnnotation = configuration.get(ConfigurationKey.MAPPER_ANNOTATION);
String mapperPackage = configuration.get(ConfigurationKey.MAPPER_PACKAGE);
String mapperBaseClass = configuration.get(ConfigurationKey.MAPPER_BASE_CLASS);
@ -191,7 +192,8 @@ public class MybatisFlexProcessor extends AbstractProcessor {
if ("true".equalsIgnoreCase(mapperGenerateEnable) && table.mapperGenerateEnable()) {
String realMapperPackage = StrUtil.isBlank(mapperPackage) ? StrUtil.buildMapperPackage(entityClass) : mapperPackage;
String mapperClassName = entityClassName.concat("Mapper");
String mapperClassContent = ContentBuilder.buildMapper(entityClass, entityClassName, realMapperPackage, mapperClassName, mapperBaseClass);
boolean mapperAnnotationEnable = "true".equalsIgnoreCase(mapperAnnotation);
String mapperClassContent = ContentBuilder.buildMapper(entityClass, entityClassName, realMapperPackage, mapperClassName, mapperBaseClass, mapperAnnotationEnable);
processGenClass(genPath, realMapperPackage, mapperClassName, mapperClassContent);
}
@ -385,4 +387,4 @@ public class MybatisFlexProcessor extends AbstractProcessor {
}
}
}
}

View File

@ -40,11 +40,19 @@ public class ContentBuilder {
* 构建 Mapper 文件内容
*/
public static String buildMapper(String entityClass, String entityClassName,
String mappersPackage, String mapperClassName, String baseMapperClass) {
String mappersPackage, String mapperClassName, String baseMapperClass, boolean mapperAnnotationEnable) {
StringBuilder content = new StringBuilder("package ");
content.append(mappersPackage).append(";\n\n");
content.append("import ").append(baseMapperClass).append(";\n");
content.append("import ").append(entityClass).append(";\n\n");
content.append(mapperAnnotationEnable ? "" : "");
if (mapperAnnotationEnable) {
content.append("import org.apache.ibatis.annotations.Mapper;\n");
content.append("import ").append(baseMapperClass).append(";\n");
content.append("import ").append(entityClass).append(";\n\n");
content.append("@Mapper\n");
} else {
content.append("import ").append(baseMapperClass).append(";\n");
content.append("import ").append(entityClass).append(";\n\n");
}
String realEntityClassName = StrUtil.getClassName(entityClass);
String baseMapperClassName = StrUtil.getClassName(baseMapperClass);
content.append("public interface ").append(mapperClassName).append(" extends ").append(baseMapperClassName).append("<").append(realEntityClassName).append("> {\n}");
@ -126,4 +134,4 @@ public class ContentBuilder {
.append(" = new ").append(tableDefClassName).append("();\n");
}
}
}

View File

@ -56,6 +56,11 @@ public enum ConfigurationKey {
*/
MAPPER_GENERATE_ENABLE("processor.mapper.generateEnable", "false"),
/**
* 开启 @Mapper 注解
*/
MAPPER_ANNOTATION("processor.mapper.annotation", "false"),
/**
* 自定义 Mapper 的父类
*/
@ -114,4 +119,4 @@ public enum ConfigurationKey {
return defaultValue;
}
}
}

View File

@ -1,4 +1,5 @@
processor.mapper.generateEnable=true
processor.mapper.annotation=true
processor.tableDef.ignoreEntitySuffixes=Entity
#processor.allInTables.enable=true
processor.mapper.baseClass=com.mybatisflex.test.mapper.MyBaseMapper