MyBatisPlusConfig.java 3.3 KB
Newer Older
水库浪子 已提交
1 2
package com.linkwechat.framework.config;

水库浪子 已提交
3 4 5 6 7 8
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.ConfigurationCustomizer;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
水库浪子 已提交
9
import com.github.pagehelper.PageInterceptor;
水库浪子 已提交
10
import com.linkwechat.common.config.WeComeConfig;
11
import com.linkwechat.common.core.domain.entity.WeCorpAccount;
12
import com.linkwechat.common.utils.SecurityUtils;
水库浪子 已提交
13
import net.sf.jsqlparser.expression.Expression;
14
import net.sf.jsqlparser.expression.StringValue;
水库浪子 已提交
15
import org.apache.ibatis.plugin.Interceptor;
水库浪子 已提交
16
import org.springframework.beans.factory.annotation.Autowired;
水库浪子 已提交
17 18 19
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

水库浪子 已提交
20 21
import java.util.Arrays;

水库浪子 已提交
22 23 24 25 26 27 28 29 30

/**
 * Mybatis支持*匹配扫描包
 * 
 * @author ruoyi
 */
@Configuration
public class MyBatisPlusConfig
{
水库浪子 已提交
31 32 33
    @Autowired
    WeComeConfig weComeConfig;

水库浪子 已提交
34 35 36 37




38

水库浪子 已提交
39 40 41 42 43 44 45 46 47 48 49 50
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 如果用了分页插件注意先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor
        // 用了分页插件必须设置 MybatisConfiguration#useDeprecatedExecutor = false
        //        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());

        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(
                new TenantLineHandler() {
                    // 获取租户 ID 值表达式,只支持单个 ID 值
                    @Override
                    public Expression getTenantId() {
51

52 53 54 55 56 57 58
                        WeCorpAccount weCorpAccount
                                = SecurityUtils.getLoginUser().getUser().getWeCorpAccount();
                        if(null != weCorpAccount){
                            return new StringValue(weCorpAccount.getCorpId());
                        }

                        return null;
水库浪子 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
                    }
                    // 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件,
                    // 这里设置 role表不需要该条件
                    @Override
                    public boolean ignoreTable(String tableName) {

                        if (Arrays.asList(weComeConfig.getNeedTenant())
                                .contains(tableName)){
                            return false;
                        }
                        return true;
                    }

                    @Override
                    public String getTenantIdColumn() {
                        return "corp_id";
                    }
                }));
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }
水库浪子 已提交
85 86 87 88 89 90

    @Bean
    public Interceptor[] plugins() {
        return new Interceptor[]{new PageInterceptor()};
    }
}