springboot集成mybatis自定义拦截器实现SQL拼接改写

需求是:要求在每条SQL的表名后面拼接上用户名,并单独形成jar给其他项目进行依赖
实现方式:自定义mybatis拦截器+springboot的自动装配
通过上面的需求和解决方案你需要掌握mybatis自定义插件(sql拦截器)以及springboot自动装配原理

项目结构

springboot集成mybatis自定义拦截器实现SQL拼接改写_第1张图片

通过spring.factories方式进行实现自动装配

创建 MyBatisDamengConfig类

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.izkml.local.dameng.config.MyBatisDamengConfig

配置类

package com.base.local.dameng.config;

import com.github.pagehelper.autoconfigure.PageHelperAutoConfiguration;
import com.base.local.dameng.interceptor.MybatisDamengInterceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @Description 适配达梦配置类
 * @Author fangh
 * @Date 2020/11/25 15:42
 */
@Configuration
@AutoConfigureAfter(PageHelperAutoConfiguration.class)
@ConditionalOnProperty(value = "dameng.enabled", havingValue = "true")
public class MyBatisDamengConfig {

    @Autowired
    private List sqlSessionFactoryList;

    /**
     * 添加mybatis拦截器
     */
    @PostConstruct
    public void addInterceptor() {
        MybatisDamengInterceptor interceptor = new MybatisDamengInterceptor();
        for (SqlSessionFactory sqlSessionFactory : sqlSessionFactoryList) {
            sqlSessionFactory.getConfiguration().addInterceptor(intercept

你可能感兴趣的:(Mybatis框架,springboot,mybatis,interceptor,springboot)