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` 来维护: 假设一个账户可以有多个角色,一个角色也可以有多个账户,他们是多对多的关系,需要通过中间件表 `tb_role_mapping` 来维护:

View File

@ -56,6 +56,11 @@ public class EntityConfig {
*/ */
private boolean withLombok; private boolean withLombok;
/**
* 实体类数据源
*/
private String dataSource;
/** /**
* 获取类前缀 * 获取类前缀
*/ */
@ -146,4 +151,19 @@ public class EntityConfig {
return this; 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); 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() { public boolean isMapperGenerateEnable() {
return mapperGenerateEnable; return mapperGenerateEnable;
} }
@ -1248,28 +1262,28 @@ public class GlobalConfig {
/** /**
* @see TableDefConfig#getPropertiesNameStyle() * @see TableDefConfig#getPropertiesNameStyle()
*/ */
public TableDefConfig.NameStyle getPropertiesNameStyle() { public TableDefConfig.NameStyle getTableDefPropertiesNameStyle() {
return getTableDefConfig().getPropertiesNameStyle(); return getTableDefConfig().getPropertiesNameStyle();
} }
/** /**
* @see TableDefConfig#setPropertiesNameStyle(TableDefConfig.NameStyle) * @see TableDefConfig#setPropertiesNameStyle(TableDefConfig.NameStyle)
*/ */
public void setPropertiesNameStyle(TableDefConfig.NameStyle propertiesNameStyle) { public void setTableDefPropertiesNameStyle(TableDefConfig.NameStyle propertiesNameStyle) {
getTableDefConfig().setPropertiesNameStyle(propertiesNameStyle); getTableDefConfig().setPropertiesNameStyle(propertiesNameStyle);
} }
/** /**
* @see TableDefConfig#getInstanceSuffix() * @see TableDefConfig#getInstanceSuffix()
*/ */
public String getInstanceSuffix() { public String getTableDefInstanceSuffix() {
return getTableDefConfig().getInstanceSuffix(); return getTableDefConfig().getInstanceSuffix();
} }
/** /**
* @see TableDefConfig#setInstanceSuffix(String) * @see TableDefConfig#setInstanceSuffix(String)
*/ */
public void setInstanceSuffix(String instanceSuffix) { public void setTableDefInstanceSuffix(String instanceSuffix) {
getTableDefConfig().setInstanceSuffix(instanceSuffix); getTableDefConfig().setInstanceSuffix(instanceSuffix);
} }

View File

@ -223,6 +223,12 @@ public class Table {
tableAnnotation.append(", schema = \"").append(globalSchema).append("\""); 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 (tableConfig != null) {
if (StringUtil.isNotBlank(tableConfig.getSchema())) { if (StringUtil.isNotBlank(tableConfig.getSchema())) {
tableAnnotation.append(", schema = \"").append(tableConfig.getSchema()).append("\""); 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.codegen.config.TableDefConfig;
import com.mybatisflex.spring.service.impl.CacheableServiceImpl; import com.mybatisflex.spring.service.impl.CacheableServiceImpl;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import org.junit.Test;
import java.util.function.UnaryOperator; import java.util.function.UnaryOperator;
@ -148,7 +149,7 @@ public class GeneratorTest {
generator.generate(); generator.generate();
} }
// @Test @Test
public void testCodeGen3() { public void testCodeGen3() {
//配置数据源 //配置数据源
HikariDataSource dataSource = new HikariDataSource(); HikariDataSource dataSource = new HikariDataSource();
@ -200,6 +201,7 @@ public class GeneratorTest {
// 配置生成 entity // 配置生成 entity
globalConfig.enableEntity() globalConfig.enableEntity()
.setOverwriteEnable(true) .setOverwriteEnable(true)
.setDataSource("ds1")
.setWithLombok(true); .setWithLombok(true);
// 配置生成 mapper // 配置生成 mapper

View File

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

View File

@ -40,11 +40,19 @@ public class ContentBuilder {
* 构建 Mapper 文件内容 * 构建 Mapper 文件内容
*/ */
public static String buildMapper(String entityClass, String entityClassName, 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 "); StringBuilder content = new StringBuilder("package ");
content.append(mappersPackage).append(";\n\n"); content.append(mappersPackage).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(baseMapperClass).append(";\n");
content.append("import ").append(entityClass).append(";\n\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 realEntityClassName = StrUtil.getClassName(entityClass);
String baseMapperClassName = StrUtil.getClassName(baseMapperClass); String baseMapperClassName = StrUtil.getClassName(baseMapperClass);
content.append("public interface ").append(mapperClassName).append(" extends ").append(baseMapperClassName).append("<").append(realEntityClassName).append("> {\n}"); content.append("public interface ").append(mapperClassName).append(" extends ").append(baseMapperClassName).append("<").append(realEntityClassName).append("> {\n}");

View File

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

View File

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