|
@@ -1,15 +1,16 @@
|
|
|
package com.ruoyi.framework.mybatis.interceptor;
|
|
|
|
|
|
-import cn.hutool.extra.spring.SpringUtil;
|
|
|
import com.alibaba.druid.pool.DruidDataSource;
|
|
|
+import com.alibaba.druid.stat.JdbcDataSourceStat;
|
|
|
+import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
|
|
+import com.baomidou.dynamic.datasource.ds.ItemDataSource;
|
|
|
+import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.inner.BaseMultiTableInnerInterceptor;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
|
|
|
-import com.ruoyi.common.enums.DataSourceType;
|
|
|
-import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
|
|
|
import com.ruoyi.framework.mybatis.holder.LogicHolder;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import net.sf.jsqlparser.JSQLParserException;
|
|
@@ -38,6 +39,7 @@ import org.apache.ibatis.session.RowBounds;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
+import javax.sql.DataSource;
|
|
|
import java.sql.Connection;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.HashMap;
|
|
@@ -70,32 +72,23 @@ public class LogicInterceptor extends BaseMultiTableInnerInterceptor implements
|
|
|
*/
|
|
|
private static final Map<String, List<String>> notIncludeLogicIdTableNameMap = new HashMap<>();
|
|
|
|
|
|
- public LogicInterceptor() {
|
|
|
- // 获取主库数据源
|
|
|
- DruidDataSource masterDataSource = SpringUtil.getBean("masterDataSource");
|
|
|
- putTableMap(masterDataSource, DataSourceType.MASTER);
|
|
|
-
|
|
|
- DruidDataSource slaveDataSource = SpringUtil.getBean("slaveDataSource");
|
|
|
- putTableMap(slaveDataSource, DataSourceType.SLAVE);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 添加不存在租户字段的表map
|
|
|
- *
|
|
|
- * @param dataSource 数据源
|
|
|
- * @param dataSourceType 数据源类型
|
|
|
- */
|
|
|
- private void putTableMap(DruidDataSource dataSource, DataSourceType dataSourceType) {
|
|
|
- // 获取链接url
|
|
|
- String url = dataSource.getUrl();
|
|
|
- // 获取数据库名
|
|
|
- String dbName = url.split("/")[3].split("\\?")[0];
|
|
|
- // 查询不包含租户字段的表名
|
|
|
- String sql = "SELECT DISTINCT table_name FROM information_schema.COLUMNS WHERE table_schema = ? AND column_name != ?";
|
|
|
- // 获取jdbc
|
|
|
- JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
|
|
|
- List<String> notIncludeTenantIdTableNameList = jdbcTemplate.queryForList(sql, String.class, dbName, delFlagName);
|
|
|
- notIncludeLogicIdTableNameMap.put(dataSourceType.name(), notIncludeTenantIdTableNameList);
|
|
|
+ public LogicInterceptor(DataSource dataSource) {
|
|
|
+ DynamicRoutingDataSource dynamicRoutingDataSource = (DynamicRoutingDataSource) dataSource;
|
|
|
+ Map<String, DataSource> dataSources = dynamicRoutingDataSource.getDataSources();
|
|
|
+ dataSources.forEach((k, v) -> {
|
|
|
+ // 获取dataSourceStat
|
|
|
+ JdbcDataSourceStat dataSourceStat = ((DruidDataSource) ((ItemDataSource) v).getRealDataSource()).getDataSourceStat();
|
|
|
+ // 获取链接url
|
|
|
+ String url = dataSourceStat.getUrl();
|
|
|
+ // 获取数据库名
|
|
|
+ String dbName = url.split("/")[3].split("\\?")[0];
|
|
|
+ // 查询包含逻辑删除字段的表名sql
|
|
|
+ String sql = "SELECT DISTINCT table_name FROM information_schema.COLUMNS WHERE table_schema = ? AND column_name != ?";
|
|
|
+ // 执行sql找出不包含逻辑删除字段的表名
|
|
|
+ List<String> notContainsDelFlagTableNameList = new JdbcTemplate(v).queryForList(sql, String.class, dbName, delFlagName);
|
|
|
+ // 数据赋值
|
|
|
+ notIncludeLogicIdTableNameMap.put(dataSourceStat.getName(), notContainsDelFlagTableNameList);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -297,7 +290,7 @@ public class LogicInterceptor extends BaseMultiTableInnerInterceptor implements
|
|
|
}
|
|
|
|
|
|
// 执行sql的数据源名称
|
|
|
- String dataSourceType = DynamicDataSourceContextHolder.getDataSourceType();
|
|
|
+ String dataSourceType = DynamicDataSourceContextHolder.peek();
|
|
|
// 获取数据源中不包含逻辑删除字段的表名
|
|
|
List<String> tableNameList = notIncludeLogicIdTableNameMap.get(dataSourceType);
|
|
|
// 如果包涵则跳过拼接逻辑删除
|