自定义Mybatis-plus插件(限制最大查询数量)

这篇具有很好参考价值的文章主要介绍了自定义Mybatis-plus插件(限制最大查询数量)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

自定义Mybatis-plus插件(限制最大查询数量)

需求背景

​ 一次查询如果结果返回太多(1万或更多),往往会导致系统性能下降,有时更会内存不足,影响系统稳定性,故需要做限制。

解决思路

1.经分析最后决定,应限制一次查询返回的最大结果数量不应该超出1万,对于一次返回结果大于限制的时候应该抛出异常,而不应该截取(limit 10000)最大结果(结果需求不匹配)。

2.利用mybatis拦截器技术,统一拦截sql,并真对大结果的查询先做一次count查询。

步骤一

1.1 定义拦截器PreCheckBigQueryInnerInterceptor

public class PreCheckBigQueryInnerInterceptor implements InnerInterceptor {}
1.2 重写willDoQuery方法
 public boolean willDoQuery(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        // 解析sql
        Statement stmt = CCJSqlParserUtil.parse(boundSql.getSql());
        if (stmt instanceof Select) {
            PlainSelect selectStmt = (PlainSelect) ((Select) stmt).getSelectBody();
            if (Objects.nonNull(selectStmt.getLimit())) {
                //包含limit查询
                return true;
            }
            for (SelectItem selectItem : selectStmt.getSelectItems()) {
                //计数查询 count();
                SelectExpressionItem selectExpressionItem = (SelectExpressionItem) selectItem;
                if (selectExpressionItem.getExpression() instanceof Function) {
                    //包含function查询
                    return true;
                }
            }
            Long aLong = doQueryCount(executor, ms, parameter, rowBounds, resultHandler, boundSql);
            if (aLong == 0L) {
                return false;
            }
            if (aLong > 20) {
                throw new RuntimeException("单个查询结果大于20条!!!");
            }
        }
        return true;
    }
1.3 代码解析
1.3.1 利用CCJSqlParserUtil解析sql,并判断sql类型,只对Select的SQL拦击.
1.3.2 对于已有limit的sql查询,直接放行.
1.3.3 对于包含function查询(例如count(1)计算,max()...),直接放行.
1.3.4 否则判断为大结果查询,执行(doQueryCount)与查询数量.
1.3.5 对于大于指定数量的结果,抛出异常.
1.4 定义doQueryCount方法
private Long doQueryCount(Executor executor, MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
        MappedStatement countMs = buildAutoCountMappedStatement(ms);
        String countSqlStr = autoCountSql(true, boundSql.getSql());
        PluginUtils.MPBoundSql mpBoundSql = PluginUtils.mpBoundSql(boundSql);
        BoundSql countSql = new BoundSql(countMs.getConfiguration(), countSqlStr, mpBoundSql.parameterMappings(), parameter);
        PluginUtils.setAdditionalParameter(countSql, mpBoundSql.additionalParameters());
        CacheKey cacheKey = executor.createCacheKey(countMs, parameter, rowBounds, countSql);
        Object result = executor.query(countMs, parameter, rowBounds, resultHandler, cacheKey, countSql).get(0);
        System.out.println(result);
        return (result == null ? 0L : Long.parseLong(result.toString()));
    }
代码解读:参考PaginationInnerInterceptor(mybatis-plus)分页插件
1.4.1:构造MappedStatement对象buildAutoCountMappedStatement(ms),MappedStatement相当于一个存储 SQL 语句、输入参数和输出结果映射等信息的封装体,它对应一条 SQL 语句,并包含了该 SQL 语句执行所需的所有信息。如下代码
<mapper namespace="com.example.UserMapper">
   <select id="selectAllUsers" resultType="com.example.User">
       SELECT * FROM user
   </select>
</mapper>

注意:必须重新构造,不能直接使用入参中的ms

1.4.2:autoCountSql(true, boundSql.getSql()) 定义并优化计数查询语句
String.format("SELECT COUNT(1) FROM (%s) TOTAL", originalSql);
1.4.3: 执行查询executor.query

步骤二

1.1 注册拦截器PreCheckBigQueryInnerInterceptor

@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
    MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));//分页插件(Mybatis-plus)
    interceptor.addInnerInterceptor(new BlockAttackInnerInterceptor());//防止全表更新(Mybatis-plus)
    interceptor.addInnerInterceptor(new PreCheckBigQueryInnerInterceptor());//防止全表查询(自定义插件)
    return interceptor;
}

知识小结:

  1. MybatisPlusInterceptor
public class MybatisPlusInterceptor implements Interceptor {
    @Setter
    private List<InnerInterceptor> interceptors = new ArrayList<>();
}

​ 他是基于mybatis的Interceptor接口做的拦截器,上文中我们 注册拦截器PreCheckBigQueryInnerInterceptor的拦截器其实添加到MybatisPlusInterceptor.interceptors集合中。

  1. 为啥重写willDoQuery见代码而不是beforeQuery
 public Object intercept(Invocation invocation) throws Throwable {
       ......
                for (InnerInterceptor query : interceptors) {
                    if (!query.willDoQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql)) {
                        return Collections.emptyList();
                    }
                    query.beforeQuery(executor, ms, parameter, rowBounds, resultHandler, boundSql);
                }
     ......
        return invocation.proceed();
 }

2.1 willDoQuery先于beforeQuery方法,且一定会执行文章来源地址https://www.toymoban.com/news/detail-418378.html

到了这里,关于自定义Mybatis-plus插件(限制最大查询数量)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • mybatis-plus之排序查询

    一、orderByAsc 排序:ORDER BY 字段, ... ASC 例:  orderByAsc(\\\"id\\\", \\\"name\\\") --- order by id ASC,name ASC #二、orderByDesc 排序:ORDER BY 字段, ... DESC 例:  orderByDesc(\\\"id\\\", \\\"name\\\") --- order by id DESC,name DESC #三、orderBy 排序:ORDER BY 字段, ... 例:  orderBy(true, true, \\\"id\\\", \\\"name\\\") --- order by id ASC,name ASC  倒叙排序  

    2024年02月15日
    浏览(31)
  • MyBatis-Plus条件查询问题解决

    问题描述 系统中有用户注册的功能,但是会出现重复注册的现象,代码中有做过重复校验,但是没有生效。 问题解决 首先排查数据生成时间点不同,相差时间有长有短,不是用户同时多次点击的原因,应该是用户这边不同时间重复多次注册导致的,但是程序中防止重复校验

    2024年02月16日
    浏览(32)
  • spring boot集成mybatis-plus——Mybatis Plus 查询数据(图文讲解)

     更新时间 2023-01-03 16:07:12 大家好,我是小哈。 本小节中,我们将学习如何通过 Mybatis Plus 查询数据库表中的数据。 在前面小节中,我们已经定义好了一个用于测试的用户表, 执行脚本如下: 定义一个名为  User  实体类: 不明白 Mybatis Plus 实体类注解的小伙伴,可参考前面

    2024年02月02日
    浏览(49)
  • mybatis-plus实现分页查询

    分页查询使用的方法是: IPage:用来构建分页查询条件 Wrapper:用来构建条件查询的条件,目前我们没有可直接传为Null IPage:返回值,你会发现构建分页条件和方法的返回值都是IPage IPage是一个接口,我们需要找到它的实现类来构建它,具体的实现类,可以进入到IPage类中按ctrl+

    2023年04月08日
    浏览(39)
  • MyBatis-Plus(三.Wrapper条件查询)

    Wrapper是Mybatis-plus中特有的 条件封装接口 也就是把 查询的条件 封装到Wrapper实现类中 它的各个实现类有什么作用呢, 我觉得直接 顾名思义 吧  QueryWrapper(删,查) 因为参数一般都从前端传来的数据中得到, 所以必须用条件封装的第一个参数确认它不为null UpdateWrapper(改) LambdaQuer

    2024年02月04日
    浏览(30)
  • SpringBoot(整合MyBatis + MyBatis-Plus + MyBatisX插件使用)

    1.需求分析 2.数据库表设计 3.数据库环境配置 1.新建maven项目 2.pom.xml 引入依赖 3.application.yml 配置数据源 数据库名 用户名 密码 驱动是mysql8的(因为上面使用了版本仲裁) 4.Application.java 编写启动类 5.测试 6.配置类切换druid数据源 7.测试数据源是否成功切换 4.Mybatis基础配置 1

    2024年03月20日
    浏览(45)
  • springboot使用Mybatis-plus分页插件

    在  pom.xml   文件中添加 MyBatis Plus 和分页插件的依赖: 注意替换  {mybatis-plus-version}  为对应的版本号。 在 Spring Boot 的配置文件  application.yml   中添加分页插件的配置参数: 注意代码中的注释,其中   PaginationInterceptor  表示使用 MyBatis Plus 提供的分页插件。 在接口层使用

    2024年02月07日
    浏览(34)
  • MyBatis-Plus 实战教程四 idea插件

    MybatisPlus提供了很多的插件功能,进一步拓展其功能。目前已有的插件有: PaginationInnerInterceptor:自动分页 TenantLineInnerInterceptor:多租户 DynamicTableNameInnerInterceptor:动态表名 OptimisticLockerInnerInterceptor:乐观锁 IllegalSQLInnerInterceptor:sql 性能规范 BlockAttackInnerInterceptor:防止全表更

    2024年02月06日
    浏览(35)
  • MyBatis-Plus分页查询(快速上手运用)

    Mybatis-Plus知识点[MyBatis+MyBatis-Plus的基础运用]_心态还需努力呀的博客-CSDN博客   Mybatis-Plus+SpringBoot结合运用_心态还需努力呀的博客-CSDN博客 MyBaits-Plus中@TableField和@TableId用法_心态还需努力呀的博客-CSDN博客 MyBatis-Plus中的更新操作(通过id更新和条件更新)_心态还需努力呀的博

    2024年02月16日
    浏览(40)
  • Mybatis-Plus高级查询LambdaQueryWrapper&QueryWrapper

    目录 前言 Wrapper 查询构造器 查询条件 前期准备 查询条件 allEq eq ne gt ge lt le between,notBetween like,notLike likeLeft likeRight isNull 空值查询 isNotNull 非空值查询 in notIn inSql、notInSql groupBy orderBy、orderByAsc、orderByDesc or、and 解决方法 last exists、notExists 总结 附加MySQL语句执行顺序 我刚刚毕

    2024年02月04日
    浏览(52)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包