Search in sources :

Example 1 with CustomTenantSqlParser

use of com.github.lybgeek.mybatisplus.tenant.parser.CustomTenantSqlParser in project springboot-learning by lyb-geek.

the class MyBatisPlusTenantConfig method tenantHandler.

/**
 * 租户处理
 * @param paginationInterceptor
 */
private void tenantHandler(PaginationInterceptor paginationInterceptor) {
    // 创建SQL解析器集合
    List<ISqlParser> sqlParserList = new ArrayList<>();
    // 创建租户SQL解析器
    TenantSqlParser tenantSqlParser = new CustomTenantSqlParser();
    // 设置租户处理器
    tenantSqlParser.setTenantHandler(new TenantHandler() {

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

        @Override
        public String getTenantIdColumn() {
            // 对应数据库租户ID的列名
            return "tenant_id";
        }

        @Override
        public boolean doTableFilter(String tableName) {
            List<String> skipFillTenantIdTables = myBatisPlusTenantProperties.getSkipFillTenantIdTables();
            return skipFillTenantIdTables.contains(tableName);
        }
    });
    sqlParserList.add(tenantSqlParser);
    paginationInterceptor.setSqlParserList(sqlParserList);
}
Also used : TenantHandler(com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler) ISqlParser(com.baomidou.mybatisplus.core.parser.ISqlParser) Expression(net.sf.jsqlparser.expression.Expression) CustomTenantSqlParser(com.github.lybgeek.mybatisplus.tenant.parser.CustomTenantSqlParser) ArrayList(java.util.ArrayList) LongValue(net.sf.jsqlparser.expression.LongValue) ArrayList(java.util.ArrayList) List(java.util.List) TenantSqlParser(com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser) CustomTenantSqlParser(com.github.lybgeek.mybatisplus.tenant.parser.CustomTenantSqlParser)

Aggregations

ISqlParser (com.baomidou.mybatisplus.core.parser.ISqlParser)1 TenantHandler (com.baomidou.mybatisplus.extension.plugins.tenant.TenantHandler)1 TenantSqlParser (com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser)1 CustomTenantSqlParser (com.github.lybgeek.mybatisplus.tenant.parser.CustomTenantSqlParser)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Expression (net.sf.jsqlparser.expression.Expression)1 LongValue (net.sf.jsqlparser.expression.LongValue)1