Search in sources :

Example 1 with InnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor in project project by Ahaochan.

the class MyBatisPlusConfig method mybatisPlusInterceptor.

/**
 * @see <a href="https://mp.baomidou.com/guide/interceptor.html">插件主体(必看!)(since 3.4.0)</a>
 */
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
    List<InnerInterceptor> innerInterceptors = Arrays.asList(// new TenantLineInnerInterceptor(), new DynamicTableNameInnerInterceptor(),
    new PaginationInnerInterceptor(), new OptimisticLockerInnerInterceptor(), // update 和 delete 必须要有 where
    new BlockAttackInnerInterceptor());
    mybatisPlusInterceptor.setInterceptors(innerInterceptors);
    return mybatisPlusInterceptor;
}
Also used : 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) BlockAttackInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor) InnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor) PaginationInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor) OptimisticLockerInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor) Bean(org.springframework.context.annotation.Bean)

Example 2 with InnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor in project albedo by somowhere.

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("detected application.database.multiTenantType={},{} mode enabled", 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 boolean ignoreMapId(String mapperId) {
                return databaseProperties.getIgnoreMapperIds() != null && databaseProperties.getIgnoreMapperIds().contains(mapperId);
            }

            @Override
            public Expression getTenantId() {
                return new StringValue(ContextUtil.getTenant());
            }
        });
        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.albedo.java.plugins.database.interceptor.TenantLineInnerInterceptor) TenantLineHandler(com.albedo.java.plugins.database.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(com.albedo.java.plugins.database.interceptor.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.albedo.java.plugins.database.interceptor.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 3 with InnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor in project albedo by somowhere.

the class AuthorityMybatisAutoConfiguration method getPaginationBeforeInnerInterceptor.

/**
 * 数据权限插件
 *
 * @return 数据权限插件
 */
@Override
protected List<InnerInterceptor> getPaginationBeforeInnerInterceptor() {
    List<InnerInterceptor> list = new ArrayList<>();
    Boolean isDataScope = databaseProperties.getIsDataScope();
    if (isDataScope) {
        list.add(new OptimisticLockerInnerInterceptor());
        list.add(new DataScopeInterceptor());
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) OptimisticLockerInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor) InnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor) OptimisticLockerInnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor) DataScopeInterceptor(com.albedo.java.plugins.database.mybatis.datascope.DataScopeInterceptor)

Example 4 with InnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor 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 5 with InnerInterceptor

use of com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor in project lamp-job by zuihou.

the class ExtendExecutorMybatisAutoConfiguration method getPaginationBeforeInnerInterceptor.

/**
 * 数据权限插件
 *
 * @return 数据权限插件
 */
@Override
protected List<InnerInterceptor> getPaginationBeforeInnerInterceptor() {
    List<InnerInterceptor> list = new ArrayList<>();
    Boolean isDataScope = databaseProperties.getIsDataScope();
    if (isDataScope) {
        list.add(new DataScopeInnerInterceptor(userId -> SpringUtils.getBean(UserService.class).getDataScopeById(userId)));
    }
    return list;
}
Also used : Configuration(org.springframework.context.annotation.Configuration) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) DataScopeInnerInterceptor(top.tangyh.basic.database.mybatis.auth.DataScopeInnerInterceptor) UserService(top.tangyh.lamp.authority.service.auth.UserService) BaseMybatisConfiguration(top.tangyh.basic.database.datasource.BaseMybatisConfiguration) EnableConfigurationProperties(org.springframework.boot.context.properties.EnableConfigurationProperties) InnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor) SpringUtils(top.tangyh.basic.utils.SpringUtils) DatabaseProperties(top.tangyh.basic.database.properties.DatabaseProperties) ArrayList(java.util.ArrayList) DataScopeInnerInterceptor(top.tangyh.basic.database.mybatis.auth.DataScopeInnerInterceptor) UserService(top.tangyh.lamp.authority.service.auth.UserService) ArrayList(java.util.ArrayList) DataScopeInnerInterceptor(top.tangyh.basic.database.mybatis.auth.DataScopeInnerInterceptor) InnerInterceptor(com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor)

Aggregations

InnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor)7 MybatisPlusInterceptor (com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor)4 BlockAttackInnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor)4 PaginationInnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor)4 Bean (org.springframework.context.annotation.Bean)4 OptimisticLockerInnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor)3 ArrayList (java.util.ArrayList)3 IllegalSQLInnerInterceptor (com.baomidou.mybatisplus.extension.plugins.inner.IllegalSQLInnerInterceptor)2 List (java.util.List)2 Slf4j (lombok.extern.slf4j.Slf4j)2 Expression (net.sf.jsqlparser.expression.Expression)2 StringValue (net.sf.jsqlparser.expression.StringValue)2 ConditionalOnExpression (org.springframework.boot.autoconfigure.condition.ConditionalOnExpression)2 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)2 EnableConfigurationProperties (org.springframework.boot.context.properties.EnableConfigurationProperties)2 Configuration (org.springframework.context.annotation.Configuration)2 Order (org.springframework.core.annotation.Order)2 BaseMybatisConfiguration (top.tangyh.basic.database.datasource.BaseMybatisConfiguration)2 DataScopeInnerInterceptor (top.tangyh.basic.database.mybatis.auth.DataScopeInnerInterceptor)2 DatabaseProperties (top.tangyh.basic.database.properties.DatabaseProperties)2