环境准备

  • 什么是拦截器
  • 为什么要这样写
    • 具体实现

什么是拦截器

SpringBoot  mybatis interceptor拦截器是你在执行SQL语句之前对执行的SQL语句进行修改
分页的原理是通过拦截器在执行SQL语句执行改变SQL语句实现物理的分页 limit

为什么要这样写

 <select id="getFileInfoMapPage" parameterType="java.lang.String" resultType="java.util.Map"><![CDATA[select rowguid,filepath from my_fileinfo where 1=1 limit #{pagenum},#{pagesize}]]></select>

我想通上面的方式实现对分页,但是在执行的过程中 limit ‘50’,‘20’
在limit中总是多了一个单引号
如果我的入参是Java.lang.Integer
这样就没有办法增加搜索条件

具体实现

首先controller

 @CrossOrigin("*")@RequestMapping("/index")public String index() {Map<String,String> map=new HashMap<String, String>();PageConfig config=new PageConfig();config.setEnd(3);config.setStart(0);String hsql=" and istoali=1 and filesize>=30388";config.setHsql(hsql);List<Map<?,?>> map_file=fileInfoService.getFileInfoMapByConditionMap(config);return "index";}

PageConfig是我自己写的一个类:

package com.muyan.util;public class PageConfig {private Integer end;private Integer start;private String hsql;public Integer getEnd() {return end;}public void setEnd(Integer end) {this.end = end;}public Integer getStart() {return start;}public void setStart(Integer start) {this.start = start;}public String getHsql() {return hsql;}public void setHsql(String hsql) {this.hsql = hsql;}}

其实实现分页的是方法:fileInfoService.getFileInfoMapByConditionMap(config)
我们来看对应的fileService中的方法:

 public List<Map<?,?>> getFileInfoMapByConditionMap(PageConfig config) {return myFileInfoMapper.getFileInfoMapPage(config);}

FileInfoMapper

 List<Map<?, ?>> getFileInfoMapPage(PageConfig config);

mapper.xml

 <select id="getFileInfoMapPage" parameterType="java.lang.String" resultType="java.util.Map"><![CDATA[select rowguid,filepath from my_fileinfo where 1=1]]></select>

然后是拦截器

package com.muyan.filter;import java.sql.Connection;
import java.util.Properties;import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.stereotype.Component;import com.muyan.util.PageConfig;@Component
@Intercepts({@Signature(method = "prepare", type = StatementHandler.class,args = {Connection.class,Integer.class})})
public class PageInterceptor  implements Interceptor {@Overridepublic Object intercept(Invocation invocation) throws Throwable {if(invocation.getTarget()  instanceof StatementHandler) {StatementHandler statementHandler=(StatementHandler)invocation.getTarget();BoundSql boundSql=statementHandler.getBoundSql();MetaObject metaObject = MetaObject.forObject(statementHandler, SystemMetaObject.DEFAULT_OBJECT_FACTORY, SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY,new DefaultReflectorFactory());MappedStatement mappedStatement = (MappedStatement)metaObject.getValue("delegate.mappedStatement");// 配置文件中SQL语句的IDString id = mappedStatement.getId();if(id.endsWith("page") || id.endsWith("Page")) {//通过BoundSql获得原始的sql语句之后,再次使用的是BoundSql的getParameterObject()来获取配置文件中的参数,因为得到的参数是一个map,调用对象的get方法得到Page对象,得到page对象之后就可以拼接分页sql了。metaObject.setValue(“delegate.boundSql.sql”,pageSql)修改原本不可以修改的值,修改原来的属性值为新的sql。PageConfig config=(PageConfig)boundSql.getParameterObject();String limitsql=" limit "+config.getStart()+","+config.getEnd();metaObject.setValue("delegate.boundSql.sql", boundSql.getSql()+config.getHsql()+limitsql);}   } return invocation.proceed();}@Overridepublic Object plugin(Object target) {return Plugin.wrap(target, this);}@Overridepublic void setProperties(Properties properties) {}}

上面拦截器的功能就是在执行语句执行对SQL语句进行修改,加上条件和limit
希望对你有所帮助

SpringBoot mybatis Interceptor分页实现相关推荐

  1. Springboot+Mybatis+PageHelper 分页、排序

    Springboot+Mybatis+PageHelper 分页.排序 升序 asc.降序 desc <!-- 继承 spring boot 父包--><parent>< ...

  2. SpringBoot集成MyBatis的分页插件PageHelper(回头草)

    俗话说:好?不吃回头草,但是在这里我建议不管你是好马还是不好马,都来吃吃,带你复习一下分页插件PageHelper. 昨天给各位总结了本人学习springboot整合mybatis第一阶段的一些学习心 ...

  3. 【Springboot学习笔记】SpringBoot+Mybatis+Thymeleaf+Layui数据表单从零开始实现按条件模糊分页查询的方法

    [Springboot学习笔记]SpringBoot+Mybatis+Thymeleaf+Layui数据表单从零开始实现按条件模糊分页查询的方法 目录 1.搭建环境 1.1直接从网上下载SpringB ...

  4. SpringBoot Mybatis解决使用PageHelper一对多分页问题

    SpringBoot Mybatis解决使用PageHelper一对多分页问题 参考文章: (1)SpringBoot Mybatis解决使用PageHelper一对多分页问题 (2)https:// ...

  5. SpringBoot集成MyBatis的分页插件PageHelper

    [写在前面] 项目的后台管理系统需要展示所有资源信息,select * 虽然方便但数据量过于庞大会严重降低查找效率,页面加载慢,用户体验差.分页自然是必要选择,但原生的方法过于繁杂.MyBatis的分 ...

  6. SpringBoot集成MyBatis-Plus分页插件

    1.说明 MyBatis使用分页查询功能, 需要配置分页插件, 如果没有配置, 则分页功能不生效. 2.分页查询API 下面列举了两个内置的分页查询API, 使用这些API时需要配置分页插件, 当然也 ...

  7. (二)、一步一步教你搭建SpringBoot+Mybatis

    SpringBoot+Mybatis 玩了一段时间了,上网转了转,发现网上有许多SpringBoot框架架构的相关教程,不过都比较简短,可能小萌新看懂需要花费很多脑力和时间,所以本人就决定写一篇所有人 ...

  8. Java项目:实现权限管理系统(java+SpringBoot+MyBatis+AOP+LayUI+Mysql)

    源码获取:博客首页 "资源" 里下载! springboot+mybatis使用面向切面编程(AOP)实现的权限管理系统. 共五个模块,角色管理,菜单管理,实验室管理,学生管理,管 ...

  9. Mybatis Interceptor 讲解

    为什么80%的码农都做不了架构师?>>>    Mybatis Interceptor 讲解 简介 Mybatis是比较流行的数据库持久层架构,可以很方便的与spring集成.框架比 ...

最新文章

  1. inteliji 优化
  2. 实战:网店活动付邮试用全攻略
  3. Win32 多文档多视图
  4. 检测网络是否稳定的计算机命令,如何查看自己的网络是否稳定
  5. 使用mit协议的编程语言_从使用诺基亚功能手机进行编程到如何为MIT初创公司工作的过程如何
  6. 拖拽功能 php,基于Vue实现拖拽功能
  7. 手机浏览器中屏蔽img的系统右键菜单context menu
  8. asp.net mysql 增删该查_asp.net 数据库的增删改查
  9. 智能计米器jk76怎么安装_春节智能锁消费指南:只看价格的后果有多严重?
  10. 源码实现 -- strdel
  11. python编程入门第3版pdf-Python编程入门(第3版) PDF扫描版[26MB]
  12. postgresql分页用法_postgresql分页数据重复问题的深入理解
  13. python中https请求的封装_python接口自动化9-https请求(SSL)
  14. GD32外部SPI Flash下载算法制作
  15. 四个球队,单循环比赛,平局每队各得一分,胜利得3分,输不得分,最后各队得分是连续的四个自然数,求第二名的得分...
  16. 局域网远程桌面无法连接到远程计算机,局域网无法远程连接桌面怎么解决
  17. python爬虫数据(中国人口信息)存入MYSQL数据库
  18. SMA(简单移动平均线)
  19. matlab使用记录--读取当前文件夹所有文件、找到最新创建的文件、app designer打开文件选择窗口
  20. 如何把照片压缩到20k一下_如何将一寸照片压缩到20k以内?

热门文章

  1. thinkJava@第五章@隐藏实施过程
  2. Docker常用命令汇总
  3. 猪八戒网CI/CD最佳实践之路
  4. 亿级系统的Redis缓存如何设计???
  5. 如何培养一个搞垮团队的Leader?
  6. 因 Redis Key 命令不规范,导致熬了一个通宵才把Key删完了!
  7. 事务处理不当,线上接口又双叒内存泄漏了!(附图解问题全过程)
  8. 百万级商品数据实时同步,查询结果秒出
  9. 拜托,别再问我贪心算法了!
  10. 这篇 ReentrantLock 看不懂,加我我给你发红包