From d68b4dd9e027272dd79f36eecb8fb50f2308df38 Mon Sep 17 00:00:00 2001 From: sgjj <995959152@qq.com> Date: Mon, 2 Nov 2020 16:46:41 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/MybatisInterceptorConfig.java | 43 +++++++++++++++++++ .../java/net/mingsoft/config/WebConfig.java | 20 --------- 2 files changed, 43 insertions(+), 20 deletions(-) create mode 100644 src/main/java/net/mingsoft/config/MybatisInterceptorConfig.java diff --git a/src/main/java/net/mingsoft/config/MybatisInterceptorConfig.java b/src/main/java/net/mingsoft/config/MybatisInterceptorConfig.java new file mode 100644 index 00000000..b962cd41 --- /dev/null +++ b/src/main/java/net/mingsoft/config/MybatisInterceptorConfig.java @@ -0,0 +1,43 @@ +package net.mingsoft.config; + +import com.baomidou.mybatisplus.annotation.DbType; +import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; +import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; +import com.baomidou.mybatisplus.extension.toolkit.JdbcUtils; +import net.mingsoft.interceptor.DMInnerInterceptor; +import net.mingsoft.interceptor.MysqlInnerInterceptor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.sql.DataSource; +import java.sql.SQLException; + +/** + * Mybatis 拦截器配置 + */ +@Configuration +public class MybatisInterceptorConfig { + + @Bean + public MybatisPlusInterceptor mybatisPlusInterceptor(DataSource dataSource, @Autowired(required = false) TenantLineHandler tenantLineHandler) { + MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); + if(tenantLineHandler!=null){ + interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(tenantLineHandler)); + } + try { + //mysql 添加转换sql + DbType dbType = JdbcUtils.getDbType(dataSource.getConnection().getMetaData().getURL()); + if(DbType.MYSQL==dbType){ + interceptor.addInnerInterceptor(new MysqlInnerInterceptor()); + }else if(DbType.DM==dbType){ + interceptor.addInnerInterceptor( new DMInnerInterceptor()); + } + } catch (SQLException e) { + e.printStackTrace(); + } + + return interceptor; + } +} diff --git a/src/main/java/net/mingsoft/config/WebConfig.java b/src/main/java/net/mingsoft/config/WebConfig.java index 80ce62f3..d888e416 100644 --- a/src/main/java/net/mingsoft/config/WebConfig.java +++ b/src/main/java/net/mingsoft/config/WebConfig.java @@ -59,26 +59,6 @@ public class WebConfig implements WebMvcConfigurer { return new ActionInterceptor(); } - @Bean - public MybatisPlusInterceptor mybatisPlusInterceptor(DataSource dataSource,@Autowired(required = false) TenantLineHandler tenantLineHandler) { - MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); - if(tenantLineHandler!=null){ - interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(tenantLineHandler)); - } - try { - //mysql 添加转换sql - DbType dbType = JdbcUtils.getDbType(dataSource.getConnection().getMetaData().getURL()); - if(DbType.MYSQL==dbType){ - interceptor.addInnerInterceptor(new MysqlInnerInterceptor()); - }else if(DbType.DM==dbType){ - interceptor.addInnerInterceptor( new DMInnerInterceptor()); - } - } catch (SQLException e) { - e.printStackTrace(); - } - - return interceptor; - } @Bean public ConfigurationCustomizer configurationCustomizer() { return configuration -> configuration.setUseDeprecatedExecutor(false);