支持tableName与schema属性占位符配置.
https://github.com/baomidou/mybatis-plus/issues/6264
This commit is contained in:
parent
423d43425f
commit
1cfc0596e3
@ -44,6 +44,7 @@ import org.apache.ibatis.logging.LogFactory;
|
||||
import org.apache.ibatis.mapping.MappedStatement;
|
||||
import org.apache.ibatis.mapping.ResultMap;
|
||||
import org.apache.ibatis.mapping.SqlCommandType;
|
||||
import org.apache.ibatis.parsing.PropertyParser;
|
||||
import org.apache.ibatis.reflection.Reflector;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
import org.apache.ibatis.type.SimpleTypeRegistry;
|
||||
@ -223,7 +224,7 @@ public class TableInfoHelper {
|
||||
GlobalConfig.DbConfig dbConfig = globalConfig.getDbConfig();
|
||||
AnnotationHandler annotationHandler = globalConfig.getAnnotationHandler();
|
||||
TableName table = annotationHandler.getAnnotation(clazz, TableName.class);
|
||||
|
||||
Configuration configuration = tableInfo.getConfiguration();
|
||||
String tableName = clazz.getSimpleName();
|
||||
String tablePrefix = dbConfig.getTablePrefix();
|
||||
String schema = dbConfig.getSchema();
|
||||
@ -232,7 +233,7 @@ public class TableInfoHelper {
|
||||
|
||||
if (table != null) {
|
||||
if (StringUtils.isNotBlank(table.value())) {
|
||||
tableName = table.value();
|
||||
tableName = PropertyParser.parse(table.value(), configuration.getVariables());
|
||||
if (StringUtils.isNotBlank(tablePrefix) && !table.keepGlobalPrefix()) {
|
||||
tablePrefixEffect = false;
|
||||
}
|
||||
@ -240,7 +241,7 @@ public class TableInfoHelper {
|
||||
tableName = initTableNameWithDbConfig(tableName, dbConfig);
|
||||
}
|
||||
if (StringUtils.isNotBlank(table.schema())) {
|
||||
schema = table.schema();
|
||||
schema = PropertyParser.parse(table.schema(), configuration.getVariables());
|
||||
}
|
||||
/* 表结果集映射 */
|
||||
if (StringUtils.isNotBlank(table.resultMap())) {
|
||||
|
@ -0,0 +1,17 @@
|
||||
package com.baomidou.mybatisplus.test.scheam;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author nieqiurong
|
||||
*/
|
||||
@Data
|
||||
@TableName(value = "${my.tableName}", schema = "${my.schema}")
|
||||
public class SchemaEntity {
|
||||
|
||||
private Long id;
|
||||
|
||||
private String name;
|
||||
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package com.baomidou.mybatisplus.test.scheam;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @author nieqiurong
|
||||
*/
|
||||
@Mapper
|
||||
public interface SchemaEntityMapper extends BaseMapper<SchemaEntity> {
|
||||
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.baomidou.mybatisplus.test.scheam;
|
||||
|
||||
import com.baomidou.mybatisplus.test.BaseDbTest;
|
||||
import org.apache.ibatis.session.Configuration;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* @author nieqiurong
|
||||
*/
|
||||
public class SchemaEntityTest extends BaseDbTest<SchemaEntityMapper> {
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
doTest(mapper -> {
|
||||
SchemaEntity schemaEntity = mapper.selectById(1);
|
||||
Assertions.assertNotNull(schemaEntity);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Consumer<Configuration> consumer() {
|
||||
return configuration -> {
|
||||
Properties properties = new Properties();
|
||||
properties.put("my.schema", "public");
|
||||
properties.put("my.tableName", "SCHEMA_ENTITY");
|
||||
configuration.setVariables(properties);
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String tableDataSql() {
|
||||
return "insert into SCHEMA_ENTITY(id,name) values(1,'1'),(2,'2');";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected List<String> tableSql() {
|
||||
return Arrays.asList("drop table if exists SCHEMA_ENTITY", "CREATE TABLE IF NOT EXISTS SCHEMA_ENTITY (" +
|
||||
"id BIGINT NOT NULL," +
|
||||
"name VARCHAR(30) NULL DEFAULT NULL," +
|
||||
"PRIMARY KEY (id))");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user