Search in sources :

Example 6 with TenantLineInnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor in project lamp-util by zuihou.

the class BaseMybatisConfiguration method mybatisPlusInterceptor.

/**
 * 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
 * <p>
 * 注意:
 * 如果内部插件都是使用,需要注意顺序关系,建议使用如下顺序
 * 多租户插件,动态表名插件
 * 分页插件,乐观锁插件
 * sql性能规范插件,防止全表更新与删除插件
 * 总结: 对sql进行单次改造的优先放入,不对sql进行改造的最后放入
 * <p>
 * 参考:
 * https://mybatis.plus/guide/interceptor.html#%E4%BD%BF%E7%94%A8%E6%96%B9%E5%BC%8F-%E4%BB%A5%E5%88%86%E9%A1%B5%E6%8F%92%E4%BB%B6%E4%B8%BE%E4%BE%8B
 */
@Bean
@Order(5)
@ConditionalOnMissingBean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    log.info("检测到 lamp.database.multiTenantType={},已启用 {} 模式", databaseProperties.getMultiTenantType().name(), databaseProperties.getMultiTenantType().getDescribe());
    if (StrUtil.equalsAny(databaseProperties.getMultiTenantType().name(), MultiTenantType.SCHEMA.name(), MultiTenantType.SCHEMA_COLUMN.name())) {
        // SCHEMA 动态表名插件
        SchemaInterceptor schemaInterceptor = new SchemaInterceptor(databaseProperties.getTenantDatabasePrefix());
        interceptor.addInnerInterceptor(schemaInterceptor);
    }
    if (StrUtil.equalsAny(databaseProperties.getMultiTenantType().name(), MultiTenantType.COLUMN.name(), MultiTenantType.SCHEMA_COLUMN.name(), MultiTenantType.DATASOURCE_COLUMN.name())) {
        // COLUMN 模式 多租户插件
        TenantLineInnerInterceptor tli = new TenantLineInnerInterceptor();
        tli.setTenantLineHandler(new TenantLineHandler() {

            @Override
            public String getTenantIdColumn() {
                return databaseProperties.getTenantIdColumn();
            }

            @Override
            public boolean ignoreTable(String tableName) {
                return databaseProperties.getIgnoreTables() != null && databaseProperties.getIgnoreTables().contains(tableName);
            }

            @Override
            public Expression getTenantId() {
                return MultiTenantType.COLUMN.eq(databaseProperties.getMultiTenantType()) ? new StringValue(ContextUtil.getTenant()) : new StringValue(ContextUtil.getSubTenant());
            }
        });
        interceptor.addInnerInterceptor(tli);
    }
    List<InnerInterceptor> beforeInnerInterceptor = getPaginationBeforeInnerInterceptor();
    if (!beforeInnerInterceptor.isEmpty()) {
        beforeInnerInterceptor.forEach(interceptor::addInnerInterceptor);
    }
    // 分页插件
    PaginationInnerInterceptor paginationInterceptor = new PaginationInnerInterceptor();
    // 单页分页条数限制
    paginationInterceptor.setMaxLimit(databaseProperties.getMaxLimit());
    // 数据库类型
    paginationInterceptor.setDbType(databaseProperties.getDbType());
    // 溢出总页数后是否进行处理
    paginationInterceptor.setOverflow(databaseProperties.getOverflow());
    // 生成 countSql 优化掉 join 现在只支持 left join
    paginationInterceptor.setOptimizeJoin(databaseProperties.getOptimizeJoin());
    interceptor.addInnerInterceptor(paginationInterceptor);
    List<InnerInterceptor> afterInnerInterceptor = getPaginationAfterInnerInterceptor();
    if (!afterInnerInterceptor.isEmpty()) {
        afterInnerInterceptor.forEach(interceptor::addInnerInterceptor);
    }
    // 防止全表更新与删除插件
    if (databaseProperties.getIsBlockAttack()) {
        interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
    }
    // sql性能规范插件
    if (databaseProperties.getIsIllegalSql()) {
        interceptor.addInnerInterceptor(new IllegalSQLInnerInterceptor());
    }
    return interceptor;
}
Also used : TenantLineInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor) TenantLineHandler(com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler) BlockAttackInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor) IllegalSQLInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.IllegalSQLInnerInterceptor) MybatisPlusInterceptor(com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor) PaginationInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor) SchemaInterceptor(top.tangyh.basic.database.plugins.SchemaInterceptor) ConditionalOnExpression(org.springframework.boot.autoconfigure.condition.ConditionalOnExpression) Expression(net.sf.jsqlparser.expression.Expression) StringValue(net.sf.jsqlparser.expression.StringValue) InnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor) IllegalSQLInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.IllegalSQLInnerInterceptor) PaginationInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor) TenantLineInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor) BlockAttackInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor) Order(org.springframework.core.annotation.Order) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 7 with TenantLineInnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor in project mybatis-plus-samples by baomidou.

the class MybatisPlusConfig method mybatisPlusInterceptor.

/**
 * 新多租户插件配置,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存万一出现问题
 */
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {

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

        // 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件
        @Override
        public boolean ignoreTable(String tableName) {
            return !"user".equalsIgnoreCase(tableName);
        }
    }));
    // interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
    return interceptor;
}
Also used : TenantLineInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor) TenantLineHandler(com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler) MybatisPlusInterceptor(com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor) LongValue(net.sf.jsqlparser.expression.LongValue) Bean(org.springframework.context.annotation.Bean)

Example 8 with TenantLineInnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor in project sakura-boot by yanjingfan.

the class MybatisPlusConfig method tenantPaginationInterceptor.

/**
 * 多租户分页
 * @return
 */
@ConditionalOnProperty(prefix = "tenant", name = "column")
@Bean
public MybatisPlusInterceptor tenantPaginationInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    TenantLineInnerInterceptor tenantInnerInterceptor = new TenantLineInnerInterceptor();
    tenantInnerInterceptor.setTenantLineHandler(new TenantLineHandler() {

        @Override
        public Expression getTenantId() {
            // 可将租户id和租户字段放置在请求头中
            try {
                String tenantId = request.getHeader("tenantId");
                if (tenantId == null || "".equals(tenantId.trim())) {
                    return new LongValue(-1);
                }
                return new LongValue(Long.parseLong(tenantId));
            } catch (Exception e) {
                return new LongValue(-1);
            }
        }

        @Override
        public String getTenantIdColumn() {
            return environment.getProperty("tenant.column");
        // 可将租户id和租户字段放置在请求头中
        // return request.getHeader("tenantColumn");
        }

        @Override
        public boolean ignoreTable(String tableName) {
            // 忽略uid-generator模块相关表
            if ("worker_node".equalsIgnoreCase(tableName)) {
                return true;
            }
            return false;
        }
    });
    interceptor.addInnerInterceptor(tenantInnerInterceptor);
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    return interceptor;
}
Also used : TenantLineInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor) TenantLineHandler(com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler) Expression(net.sf.jsqlparser.expression.Expression) MybatisPlusInterceptor(com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor) LongValue(net.sf.jsqlparser.expression.LongValue) PaginationInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) Bean(org.springframework.context.annotation.Bean)

Example 9 with TenantLineInnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor in project knife-starter by 1120023921.

the class MybatisPlusAutoConfig method mybatisPlusInterceptor.

/**
 * @param tenantLineHandler 租户处理器
 * @description Mybaits插件配置
 * @author 胡昊
 * @email huhao9277@gmail.com
 */
@Bean
@ConditionalOnMissingBean(MybatisPlusInterceptor.class)
public MybatisPlusInterceptor mybatisPlusInterceptor(@Autowired(required = false) TenantLineHandler tenantLineHandler) {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    if (tenantLineHandler != null) {
        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(tenantLineHandler));
    }
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
    return interceptor;
}
Also used : TenantLineInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor) MybatisPlusInterceptor(com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor) PaginationInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor) OptimisticLockerInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

TenantLineInnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor)9 Bean (org.springframework.context.annotation.Bean)8 MybatisPlusInterceptor (com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor)7 PaginationInnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor)6 TenantLineHandler (com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler)4 LongValue (net.sf.jsqlparser.expression.LongValue)3 BlockAttackInnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor)2 OptimisticLockerInnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor)2 Expression (net.sf.jsqlparser.expression.Expression)2 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 TenantDatabaseInterceptor (cn.iocoder.yudao.framework.tenant.core.db.TenantDatabaseInterceptor)1 IllegalSQLInnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.IllegalSQLInnerInterceptor)1 InnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor)1 StringValue (net.sf.jsqlparser.expression.StringValue)1 ConditionalOnExpression (org.springframework.boot.autoconfigure.condition.ConditionalOnExpression)1 ConditionalOnProperty (org.springframework.boot.autoconfigure.condition.ConditionalOnProperty)1 FilterRegistrationBean (org.springframework.boot.web.servlet.FilterRegistrationBean)1 Order (org.springframework.core.annotation.Order)1 SchemaInterceptor (top.tangyh.basic.database.plugins.SchemaInterceptor)1