feat: 3.2.0
1.新增公共表前缀功能
This commit is contained in:
parent
76d29f5f6a
commit
789280aadf
@ -1,7 +1,6 @@
|
||||
package com.aizuda.easy.retry.template.datasource.config;
|
||||
|
||||
import com.aizuda.easy.retry.template.datasource.access.Access;
|
||||
import com.aizuda.easy.retry.template.datasource.access.AccessTemplate;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.aizuda.easy.retry.template.datasource.enums.DbTypeEnum;
|
||||
import com.aizuda.easy.retry.template.datasource.utils.RequestDataHelper;
|
||||
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
|
||||
@ -23,6 +22,7 @@ import javax.sql.DataSource;
|
||||
import java.text.MessageFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/**
|
||||
* @author: www.byteblogs.com
|
||||
@ -56,7 +56,8 @@ public class EasyRetryTemplateAutoConfiguration {
|
||||
@Bean
|
||||
public MybatisPlusInterceptor mybatisPlusInterceptor(Environment environment) {
|
||||
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
|
||||
interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor());
|
||||
String tablePrefix = Optional.ofNullable(environment.getProperty("mybatis-plus.global-config.db-config.table-prefix")).orElse(StrUtil.EMPTY);
|
||||
interceptor.addInnerInterceptor(dynamicTableNameInnerInterceptor(tablePrefix));
|
||||
String dbType = environment.getProperty("easy-retry.db-type");
|
||||
DbTypeEnum dbTypeEnum = DbTypeEnum.modeOf(dbType);
|
||||
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(dbTypeEnum.getMpDbType()));
|
||||
@ -64,15 +65,16 @@ public class EasyRetryTemplateAutoConfiguration {
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
public DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor() {
|
||||
public DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor(String tablePrefix) {
|
||||
DynamicTableNameInnerInterceptor dynamicTableNameInnerInterceptor = new DynamicTableNameInnerInterceptor();
|
||||
dynamicTableNameInnerInterceptor.setTableNameHandler((sql, tableName) -> {
|
||||
if (!TABLES.contains(tableName)) {
|
||||
return tableName;
|
||||
if (TABLES.contains(tableName)) {
|
||||
Integer partition = RequestDataHelper.getPartition();
|
||||
RequestDataHelper.remove();
|
||||
tableName = tableName + StrUtil.UNDERLINE + partition;
|
||||
}
|
||||
Integer partition = RequestDataHelper.getPartition();
|
||||
RequestDataHelper.remove();
|
||||
return tableName + "_"+ partition;
|
||||
|
||||
return tableName.startsWith(tablePrefix) ? tableName : tablePrefix + tableName;
|
||||
});
|
||||
|
||||
return dynamicTableNameInnerInterceptor;
|
||||
|
@ -32,6 +32,7 @@ mybatis-plus:
|
||||
capital-mode: false
|
||||
logic-delete-value: 1
|
||||
logic-not-delete-value: 0
|
||||
# table-prefix: er_
|
||||
configuration:
|
||||
map-underscore-to-camel-case: true
|
||||
cache-enabled: true
|
||||
|
@ -24,6 +24,7 @@ import com.aizuda.easy.retry.template.datasource.access.TaskAccess;
|
||||
import com.aizuda.easy.retry.template.datasource.persistence.mapper.SequenceAllocMapper;
|
||||
import com.aizuda.easy.retry.template.datasource.persistence.mapper.ServerNodeMapper;
|
||||
import com.aizuda.easy.retry.template.datasource.persistence.po.*;
|
||||
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
|
||||
@ -70,6 +71,8 @@ public class GroupConfigServiceImpl implements GroupConfigService {
|
||||
private SystemProperties systemProperties;
|
||||
@Autowired
|
||||
private JdbcTemplate jdbcTemplate;
|
||||
@Autowired
|
||||
private MybatisPlusProperties mybatisPlusProperties;
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
@ -338,10 +341,12 @@ public class GroupConfigServiceImpl implements GroupConfigService {
|
||||
String catalog = connection.getCatalog();
|
||||
String schema = connection.getSchema();
|
||||
|
||||
String tablePrefix = Optional.ofNullable(mybatisPlusProperties.getGlobalConfig().getDbConfig().getTablePrefix()).orElse(StrUtil.EMPTY);
|
||||
// https://gitee.com/aizuda/easy-retry/issues/I8DAMH
|
||||
String sql = MessageFormatter.arrayFormat("SELECT table_name\n"
|
||||
+ "FROM information_schema.tables\n"
|
||||
+ "WHERE table_name LIKE 'retry_task_%' AND (table_schema = '{}' OR table_schema = '{}' OR table_catalog = '{}' OR table_catalog = '{}')", new Object[]{schema, catalog, schema, catalog}).getMessage();
|
||||
+ "WHERE table_name LIKE '{}retry_task_%' AND (table_schema = '{}' OR table_schema = '{}' OR table_catalog = '{}' OR table_catalog = '{}')",
|
||||
new Object[]{tablePrefix, schema, catalog, schema, catalog}).getMessage();
|
||||
|
||||
List<String> tableList = jdbcTemplate.queryForList(sql, String.class);
|
||||
return tableList.stream().map(ReUtil::getFirstNumber).filter(i ->
|
||||
|
Loading…
Reference in New Issue
Block a user