其他分享
首页 > 其他分享> > mybatisplus 多租户自定义支持多个值

mybatisplus 多租户自定义支持多个值

作者:互联网

mybatisplus中的多租户只能查询一个值,但是实际业务中可能是多个值
例如:
where veid in(’’,’’,’’)
这样的情况
所以就读了下源码,并自定义封装了下
废话不多说,直接上代码
基于最新的Mybatisplus

 <dependency>
        <groupId>com.baomidou</groupId>
        <artifactId>mybatis-plus-boot-starter</artifactId>
        <version>3.4.3.4</version>
    </dependency>

继承mybatisplus的多租户拦截器

package com.hzz.mybatisplusdemo.config.sqlInjector;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Parenthesis;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.delete.Delete;
import net.sf.jsqlparser.statement.insert.Insert;
import net.sf.jsqlparser.statement.select.SelectBody;
import net.sf.jsqlparser.statement.update.Update;
import net.sf.jsqlparser.util.cnfexpression.MultipleExpression;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :hzz
 * @description:TODO
 * @date :2021/11/8 13:59
 */
public class MyTenantLineInnerInterceptor extends TenantLineInnerInterceptor {

    public MyTenantLineInnerInterceptor(TenantLineHandler tenantLineHandler) {
        super(tenantLineHandler);
    }

    /**
     * 重构mybatispuls的多租户改为支持in
     * @param currentExpression
     * @param table
     * @return
     */
    @Override
    protected Expression builderExpression(Expression currentExpression, Table table) {
        Column aliasColumn = this.getAliasColumn(table);
        boolean presenceOfField=true;

        if(presenceOfField) {
            InExpression inExpression = new InExpression();
            inExpression.setLeftExpression(aliasColumn);
            inExpression.setRightExpression(getTenantLineHandler().getTenantId());
            if (currentExpression == null) {
                return inExpression;
            } else {
                return currentExpression instanceof OrExpression ? new AndExpression(new Parenthesis(currentExpression), inExpression) : new AndExpression(currentExpression, inExpression);
            }
        }else{
              return currentExpression;
        }

    }

    @Override
    protected void processInsert(Insert insert, int index, String sql, Object obj) {

    }

    @Override
    protected void processUpdate(Update update, int index, String sql, Object obj) {

    }

    @Override
    protected void processDelete(Delete delete, int index, String sql, Object obj) {

    }

    @Override
    protected void processInsertSelect(SelectBody selectBody) {

    }

    public static void main(String[] args) {
        InExpression inExpression=new InExpression();
        inExpression.setLeftExpression(new Column("id"));

        List<Expression> childlist =new ArrayList<>();
        childlist.add(new StringValue("1"));
        childlist.add(new StringValue("2"));
        MultipleExpression multipleExpression=new MultipleExpression(childlist) {
            @Override
            public String getStringExpression() {
                return ",";
            }
        };
//        MultiExpressionList multiExpressionList=new MultiExpressionList();
        List<ExpressionList> expressionLists =new ArrayList<>();
        expressionLists.add(new ExpressionList(new StringValue("1"),new StringValue("2")));
        multiExpressionList.setExpressionLists(expressionLists);
        inExpression.setRightItemsList(multiExpressionList );
        inExpression.setRightExpression(multipleExpression);
        System.out.println(1);
    }
}

自定义租户配置类

package com.hzz.mybatisplusdemo.Tenant;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.util.cnfexpression.MultipleExpression;

import java.util.ArrayList;
import java.util.List;

/**
 * @author :hzz
 * @description:TODO
 * @date :2021/8/9 15:44
 */

public class MyTenantLineHandler  implements TenantLineHandler {
    @Override
    public Expression getTenantId() {
        List<Expression> childlist =new ArrayList<>();
        childlist.add(new StringValue("1"));
        childlist.add(new StringValue("2"));
        MultipleExpression multipleExpression=new MultipleExpression(childlist) {
            @Override
            public String getStringExpression() {
                return ",";
            }
        };
        return multipleExpression;
    }

    @Override
    public String getTenantIdColumn() {
        return "id";
    }

    /**
     * 这个地方需要排除没有这个权限字段的表,如果是嵌套或者连表,都会添加上这个多租户
     * @param tableName
     * @return
     */
    @Override
    public boolean ignoreTable(String tableName) {
//        if(tableName.equals("subselect")){
//            return false;
//        }
//        return true;
        return false;
    }
}

最后注入到mybatisplus的拦截器


    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        interceptor.addInnerInterceptor(new MyTenantLineInnerInterceptor(new MyTenantLineHandler()));
        return interceptor;
    }

实际效果
由于没有配置要进行多租户的表,所以所有表都添加了

标签:mybatisplus,自定义,租户,return,jsqlparser,new,import,net,sf
来源: https://blog.csdn.net/qq_37361535/article/details/121212070