SpringBoot2.2.3+Mybatis-Plus3.3.2+jsqlparser3.2多租户踩的坑 @SqlParser(filter = true) 失效的问题

至于为什么需要多租户模式架构大家请出门右拐往左走,不送

另想学习搭建多租户模式的简单教程的可以参考下面文章:

https://juejin.cn/post/6966770686652219406

 

下班是哥们实践的结果;

我们面对业务系统时,大多数时CRUD。但是在面对查询的时候,我们项目整合了pagehelper,
当插件。这是封装分页组件和jsqlparser3.2修改sql就会出问题。

 

 

selectDbTableList_COUNT

当是应用 pagehelper 分页的时候,pagehelper分页会自动为查询方法添加_count方法,但是在配置中没有任何设置的地方需要排除它所导致。

解决办法:

paginationInterceptor.setSqlParserFilter(new ISqlParserFilter() {
            @Override
            public boolean doFilter(MetaObject metaObject) {
                MappedStatement mappedStatement = SqlParserHelper.getMappedStatement(metaObject);
                if (mappedStatement.getId().contains("_COUNT")) {
                    return true;
                }
                return false;
            }
        });

只要是pagehelper 生成的_count 的方法可以使用不用多租户的使用,这样就可以不添加 租户id;

从而解决@SqlParser(filter = true)

 

核心配置如下:

import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.core.parser.ISqlParserFilter;
import com.baomidou.mybatisplus.core.parser.SqlParserHelper;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 */
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
public class MybatisPlusConfig {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        List sqlParserList = new ArrayList<>();
        TenantSqlParser tenantSqlParser = new TenantSqlParser();

        tenantSqlParser.setTenantHandler(new TenantHandler() {
            @Override
            public Expression getTenantId(boolean select) {
                ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
                HttpServletRequest request = requestAttributes.getRequest();
                String tenantId = request.getHeader("租户id");
                return new StringValue(tenantId);
            }

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

            @Override
            public boolean doTableFilter(String tableName) {
                List IGNORE_TENANT_TABLES= Arrays.asList("dept",
                        "information_schema.tables");

                return IGNORE_TENANT_TABLES.stream().anyMatch(e->e.equalsIgnoreCase(tableName));
            }
        });
        sqlParserList.add(tenantSqlParser);
        paginationInterceptor.setSqlParserList(sqlParserList);
        paginationInterceptor.setSqlParserFilter(new ISqlParserFilter() {
            @Override
            public boolean doFilter(MetaObject metaObject) {
                MappedStatement mappedStatement = SqlParserHelper.getMappedStatement(metaObject);
                if (mappedStatement.getId().contains("_COUNT")) {
                    return true;
                }
                return false;
            }
        });
        return paginationInterceptor;
    }

}

 

你可能感兴趣的:(无奈,java)