|
@@ -1,97 +0,0 @@
|
|
|
-package com.chelvc.framework.database.interceptor;
|
|
|
-
|
|
|
-import java.sql.Connection;
|
|
|
-import java.util.Map;
|
|
|
-import javax.sql.DataSource;
|
|
|
-
|
|
|
-import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
|
|
|
-import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
|
|
|
-import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
|
|
|
-import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
|
|
-import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
|
|
-import com.chelvc.framework.database.util.SQLUtils;
|
|
|
-import com.google.common.collect.Maps;
|
|
|
-import com.zaxxer.hikari.HikariDataSource;
|
|
|
-import lombok.NonNull;
|
|
|
-import org.apache.ibatis.cache.CacheKey;
|
|
|
-import org.apache.ibatis.executor.Executor;
|
|
|
-import org.apache.ibatis.executor.statement.StatementHandler;
|
|
|
-import org.apache.ibatis.mapping.BoundSql;
|
|
|
-import org.apache.ibatis.mapping.MappedStatement;
|
|
|
-import org.apache.ibatis.plugin.Intercepts;
|
|
|
-import org.apache.ibatis.plugin.Invocation;
|
|
|
-import org.apache.ibatis.plugin.Signature;
|
|
|
-import org.apache.ibatis.session.ResultHandler;
|
|
|
-import org.apache.ibatis.session.RowBounds;
|
|
|
-import org.springframework.context.ApplicationContext;
|
|
|
-
|
|
|
-/**
|
|
|
- * 自定义Mybatis拦截器
|
|
|
- *
|
|
|
- * @author Woody
|
|
|
- * @date 2025/3/16
|
|
|
- */
|
|
|
-@Intercepts({
|
|
|
- @Signature(type = StatementHandler.class, method = "getBoundSql", args = {}),
|
|
|
- @Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}),
|
|
|
- @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}),
|
|
|
- @Signature(
|
|
|
- type = Executor.class, method = "query",
|
|
|
- args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}
|
|
|
- ),
|
|
|
- @Signature(
|
|
|
- type = Executor.class, method = "query",
|
|
|
- args = {
|
|
|
- MappedStatement.class, Object.class, RowBounds.class,
|
|
|
- ResultHandler.class, CacheKey.class, BoundSql.class
|
|
|
- }
|
|
|
- )
|
|
|
-})
|
|
|
-public class CustomizeMybatisInterceptor extends MybatisPlusInterceptor {
|
|
|
- private final boolean dynamicDatasource;
|
|
|
- private final Map<String, String> schemas;
|
|
|
-
|
|
|
- public CustomizeMybatisInterceptor(@NonNull ApplicationContext applicationContext) {
|
|
|
- DataSource dataSource = applicationContext.getBean(DataSource.class);
|
|
|
- if (this.dynamicDatasource = dataSource instanceof DynamicRoutingDataSource) {
|
|
|
- DynamicDataSourceProperties properties = applicationContext.getBean(DynamicDataSourceProperties.class);
|
|
|
- Map<String, DataSourceProperty> configs = properties.getDatasource();
|
|
|
- this.schemas = Maps.newHashMapWithExpectedSize(configs.size());
|
|
|
- configs.forEach((key, config) -> this.schemas.put(key, SQLUtils.getSchema(config.getUrl())));
|
|
|
- } else {
|
|
|
- this.schemas = Maps.newHashMapWithExpectedSize(1);
|
|
|
- if (dataSource instanceof HikariDataSource) {
|
|
|
- this.schemas.put(null, SQLUtils.getSchema(((HikariDataSource) dataSource).getJdbcUrl()));
|
|
|
- } else {
|
|
|
- throw new RuntimeException("Unknown datasource: " + dataSource);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取方法调用上下文
|
|
|
- *
|
|
|
- * @param invocation 调用信息
|
|
|
- * @return 上下文实例
|
|
|
- */
|
|
|
- private Tables.Context getContext(Invocation invocation) {
|
|
|
- Object[] args = invocation.getArgs();
|
|
|
- String id = ((MappedStatement) args[0]).getId();
|
|
|
- String namespace = id.substring(0, id.lastIndexOf('.'));
|
|
|
- String schema = this.schemas.get(this.dynamicDatasource ? DynamicDataSourceContextHolder.peek() : null);
|
|
|
- return new Tables.Context(schema, namespace);
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public Object intercept(Invocation invocation) throws Throwable {
|
|
|
- if (invocation.getTarget() instanceof Executor) {
|
|
|
- Tables.setContext(this.getContext(invocation));
|
|
|
- try {
|
|
|
- return super.intercept(invocation);
|
|
|
- } finally {
|
|
|
- Tables.setContext(null);
|
|
|
- }
|
|
|
- }
|
|
|
- return super.intercept(invocation);
|
|
|
- }
|
|
|
-}
|