2026-04-17 69e4436441fb64b45a43108f5f374cca29554fda
refactor(MybatisPlusConfig): 重构MyBatis Plus配置类

- 移除租户插件相关配置和注释代码
- 简化类文档注释
- 调整拦截器注册顺序,将数据权限SQL拦截器放在分页插件之前
- 优化分页拦截器的beforeQuery方法实现,移除多余注释
- 更新方法文档注释为简洁描述
- 格式化代码缩进和括号位置
已修改1个文件
53 ■■■■ 文件已修改
src/main/java/com/ruoyi/framework/config/MybatisPlusConfig.java 53 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ruoyi/framework/config/MybatisPlusConfig.java
@@ -7,8 +7,6 @@
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import com.ruoyi.common.handler.CustomTenantLineHandler;
import com.ruoyi.common.interceptor.DataScopeSqlInterceptor;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
@@ -23,9 +21,7 @@
import java.sql.SQLException;
/**
 * Mybatis Plus 配置
 *
 * @author ruoyi
 * MyBatis Plus config.
 */
@EnableTransactionManagement(proxyTargetClass = true)
@Configuration
@@ -35,66 +31,47 @@
    private DataScopeSqlInterceptor dataScopeSqlInterceptor;
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor()
    {
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 租户插件
//        TenantLineInnerInterceptor tenantLineInnerInterceptor = new TenantLineInnerInterceptor(new CustomTenantLineHandler());
//        interceptor.addInnerInterceptor(tenantLineInnerInterceptor);
        // 分页插件
        interceptor.addInnerInterceptor(paginationInnerInterceptor());
        // 乐观锁插件
        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
        // 阻断插件
        interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
        // 数据权限插件
        // Rewrite the original SQL before pagination generates the count query.
        interceptor.addInnerInterceptor(dataScopeSqlInterceptor);
        interceptor.addInnerInterceptor(paginationInnerInterceptor());
        interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
        interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
        return interceptor;
    }
    /**
     * 分页插件,自动识别数据库类型 https://baomidou.com/guide/interceptor-pagination.html
     * Pagination interceptor.
     */
//    public PaginationInnerInterceptor paginationInnerInterceptor()
//    {
//        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
//        // 设置数据库类型为mysql
//        paginationInnerInterceptor.setDbType(DbType.MYSQL);
//        // 设置最大单页限制数量,默认 500 条,-1 不受限制
//        paginationInnerInterceptor.setMaxLimit(-1L);
//        return paginationInnerInterceptor;
//    }
    public PaginationInnerInterceptor paginationInnerInterceptor() {
        PaginationInnerInterceptor interceptor = new PaginationInnerInterceptor(DbType.MYSQL) {
            @Override
            public void beforeQuery(Executor executor, MappedStatement ms, Object parameter,
                                    RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
                                    RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql)
                    throws SQLException {
                IPage<?> page = ParameterUtils.findPage(parameter).orElse(null);
                if (page != null && page.getSize() <= 0) {
                    // 当size<=0时,不进行分页
                    return;
                }
                super.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
            }
        };
        interceptor.setMaxLimit(1000L); // 建议设置合理的最大值
        interceptor.setMaxLimit(1000L);
        return interceptor;
    }
    /**
     * 乐观锁插件 https://baomidou.com/guide/interceptor-optimistic-locker.html
     * Optimistic lock interceptor.
     */
    public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor()
    {
    public OptimisticLockerInnerInterceptor optimisticLockerInnerInterceptor() {
        return new OptimisticLockerInnerInterceptor();
    }
    /**
     * 如果是对全表的删除或更新操作,就会终止该操作 https://baomidou.com/guide/interceptor-block-attack.html
     * Block full-table update and delete.
     */
    public BlockAttackInnerInterceptor blockAttackInnerInterceptor()
    {
    public BlockAttackInnerInterceptor blockAttackInnerInterceptor() {
        return new BlockAttackInnerInterceptor();
    }
}
}