Search in sources :

Example 6 with TenantHandler

use of com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler in project tutorials-java by Artister.

the class MybatisPlusConfig method paginationInterceptor.

/**
 * 多租户属于 SQL 解析部分,依赖 MP 分页插件
 */
@Bean
public PaginationInterceptor paginationInterceptor() {
    PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
    /*
         * 【测试多租户】 SQL 解析处理拦截器<br>
         * 这里固定写成住户 1 实际情况你可以从cookie读取,因此数据看不到 【 麻花藤 】 这条记录( 注意观察 SQL )<br>
         */
    List<ISqlParser> sqlParserList = new ArrayList<>();
    TenantSqlParser tenantSqlParser = new TenantSqlParser();
    tenantSqlParser.setTenantHandler(new TenantHandler() {

        @Override
        public Expression getTenantId() {
            return new LongValue(1L);
        }

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

        @Override
        public boolean doTableFilter(String tableName) {
            /*if ("user".equals(tableName)) {
                    return true;
                }*/
            return false;
        }
    });
    sqlParserList.add(tenantSqlParser);
    paginationInterceptor.setSqlParserList(sqlParserList);
    // });
    return paginationInterceptor;
}
Also used : TenantHandler(com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler) ISqlParser(com.baomidou.mybatisplus.core.parser.ISqlParser) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) LongValue(net.sf.jsqlparser.expression.LongValue) PaginationInterceptor(com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor) TenantSqlParser(com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser) Bean(org.springframework.context.annotation.Bean)

Aggregations

ISqlParser (com.baomidou.mybatisplus.core.parser.ISqlParser)6 TenantHandler (com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler)6 TenantSqlParser (com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser)6 ArrayList (java.util.ArrayList)6 Expression (net.sf.jsqlparser.expression.Expression)6 LongValue (net.sf.jsqlparser.expression.LongValue)6 ISqlParserFilter (com.baomidou.mybatisplus.core.parser.ISqlParserFilter)4 MetaObject (org.apache.ibatis.reflection.MetaObject)4 PaginationInterceptor (com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor)3 List (java.util.List)3 Bean (org.springframework.context.annotation.Bean)3 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)2 InExpression (net.sf.jsqlparser.expression.operators.relational.InExpression)2 Column (net.sf.jsqlparser.schema.Column)2 MappedStatement (org.apache.ibatis.mapping.MappedStatement)2 BlockAttackSqlParser (com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser)1 CustomTenantSqlParser (com.github.lybgeek.mybatisplus.tenant.parser.CustomTenantSqlParser)1