Search in sources :

Example 1 with TenantLineInnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor in project jeecg-boot by jeecgboot.

the class MybatisPlusSaasConfig method mybatisPlusInterceptor.

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    // 先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor
    interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {

        @Override
        public Expression getTenantId() {
            String tenant_id = oConvertUtils.getString(TenantContext.getTenant(), "0");
            return new LongValue(tenant_id);
        }

        @Override
        public String getTenantIdColumn() {
            return TENANT_FIELD_NAME;
        }

        // 返回 true 表示不走租户逻辑
        @Override
        public boolean ignoreTable(String tableName) {
            for (String temp : tenantTable) {
                if (temp.equalsIgnoreCase(tableName)) {
                    return false;
                }
            }
            return true;
        }
    }));
    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) PaginationInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 2 with TenantLineInnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor in project ieasy-server by baayso.

the class MybatisPlusConfig method mybatisPlusInterceptor.

/**
 * mybatis-plus 插件
 * <p>
 * 文档:https://baomidou.com/pages/2976a3/
 * <p>
 * 使用多个功能需要注意顺序关系,建议使用如下顺序<br>
 * 1) 多租户,动态表名<br>
 * 2) 分页,乐观锁<br>
 * 3) sql 性能规范,防止全表更新与删除<br>
 * 总结: 对 sql 进行单次改造的优先放入,不对 sql 进行改造的最后放入
 */
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    // 防止全表更新与删除插件
    interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());
    // 多租户插件
    TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor();
    // 行级租户SQL解析器
    tenantLineInnerInterceptor.setTenantLineHandler(new BasicTenantLineHandler());
    interceptor.addInnerInterceptor(tenantLineInnerInterceptor);
    // 分页插件
    // 如果使用了分页插件注意先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    // 乐观锁插件
    interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
    return interceptor;
}
Also used : TenantLineInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor) BlockAttackInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor) MybatisPlusInterceptor(com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor) PaginationInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor) OptimisticLockerInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 3 with TenantLineInnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor in project XHuiCloud by sindaZeng.

the class XHuiMybatisPlusConfig method mybatisPlusInterceptor.

/**
 * 数据处理
 */
/**
 * mybatis plus 拦截器配置
 * @return
 */
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(XHuiTenantHandler xHuiTenantHandler) {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    // 多租户支持
    TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor();
    tenantLineInnerInterceptor.setTenantLineHandler(xHuiTenantHandler);
    interceptor.addInnerInterceptor(tenantLineInnerInterceptor);
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
    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) ConditionalOnBean(org.springframework.boot.autoconfigure.condition.ConditionalOnBean) Bean(org.springframework.context.annotation.Bean)

Example 4 with TenantLineInnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor in project ruoyi-vue-pro by YunaiV.

the class YudaoTenantAutoConfiguration method tenantLineInnerInterceptor.

// ========== DB ==========
@Bean
public TenantLineInnerInterceptor tenantLineInnerInterceptor(TenantProperties properties, MybatisPlusInterceptor interceptor) {
    TenantLineInnerInterceptor inner = new TenantLineInnerInterceptor(new TenantDatabaseInterceptor(properties));
    // 添加到 interceptor 中
    // 需要加在首个,主要是为了在分页插件前面。这个是 MyBatis Plus 的规定
    MyBatisUtils.addInterceptor(interceptor, inner, 0);
    return inner;
}
Also used : TenantLineInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor) TenantDatabaseInterceptor(cn.iocoder.yudao.framework.tenant.core.db.TenantDatabaseInterceptor) FilterRegistrationBean(org.springframework.boot.web.servlet.FilterRegistrationBean) Bean(org.springframework.context.annotation.Bean)

Example 5 with TenantLineInnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor in project helio-starters by uncarbon97.

the class TenantLineSupport method support.

@Override
public void support(HelioProperties helioProperties, MybatisPlusInterceptor interceptor) {
    Collection<String> ignoredTables = helioProperties.getTenant().getIgnoredTables();
    // 添加行级租户内联拦截器
    interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new HelioLineTenantHandler(helioProperties.getTenant().getPrivilegedTenantId(), ignoredTables)));
    log.info("\n\n[多租户支持] >> 隔离级别: 行级");
    System.err.println("以下数据表不参与租户隔离: " + ignoredTables);
}
Also used : TenantLineInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor)

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