当前位置: 首页 > news >正文

Mybatis分页插件的使用问题记录

项目中配置的分页插件依赖为

<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.1.7</version></dependency>

之前的项目代码编写分页的方式为,通过传入的条件,先查询总条数,这是第一次sql执行,然后,根据传入的页码、条数,拼凑limit条件,查询分页记录,这是第二次sql执行,如下

int start = (Integer.valueOf(pageNum)-1)*Integer.valueOf(pageSize);int end = Integer.valueOf(pageSize);String dataType = "dataType";List<DataTypeList>list = dataTypeMapper.queryDataTypeList(request.getParameter(dataType),start,end);for (DataTypeList dataTypeList : list) {Date date = new Date(dataTypeList.getCheckTime());dataTypeList.setCheckDate(sdf.format(date));}page.setList(list);page.setPageSize(pageSize);int total = dataTypeMapper.countByExample(request.getParameter(dataType));page.setTotal(String.valueOf(total));page.setPageNum(pageNum);page.setPages(String.valueOf((total%Integer.valueOf(pageSize))>0?(total/Integer.valueOf(pageSize)+1):(total/Integer.valueOf(pageSize))));

可是这样的写法,有没有分页插件压根没影响啊,而且特别蠢,于是考虑改造,通过如下代码实现分页

PageHelper.startPage(Integer.valueOf(pageNum),Integer.valueOf(pageSize));List<DataTypeList>list = dataTypeMapper.queryDataTypeList1(request.getParameter("templateId"));PageInfo<DataTypeList> pageInfo = new PageInfo<DataTypeList>(list);

运行之后有了总条数等信息,但是分页却不生效,查询的是所有记录,于是,考虑增加配置,如下

pagehelper.helperDialect=mysq1
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSq1

如果自定义了数据库连接配置,那么需要用下面的方式,将分页插件定义到mybatis配置中,如下

@Beanpublic SqlSessionFactory mnrSqlSessionFactory(DataSource mnrDataSource)throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(mnrDataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MnrDataSourceConfig.MAPPER_LOCATION));org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();configuration.setMapUnderscoreToCamelCase(true);sessionFactory.setConfiguration(configuration);PageInterceptor pageHelper = new PageInterceptor();Properties properties = new Properties();properties.setProperty("reasonable","true");pageHelper.setProperties(properties);sessionFactory.setPlugins(new Interceptor[]{pageHelper});return sessionFactory.getObject();}

我这里加了驼峰自动转换,当然,也可以在xml文件配置,如下

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 其他配置 --><property name="plugins"><array><bean class="com.github.pagehelper.PageInterceptor"><property name="properties"><!-- 这里可以配置PageHelper的属性 --><props><prop key="reasonable">true</prop><prop key="supportMethodsArguments">true</prop><prop key="returnPageInfo">check</prop></props></property></bean></array></property>
</bean>

另外,如果更改依赖为pageHelper-spring-boot-starter的话

<dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.0</version>
</dependency>

直接编写分页代码即可,不需要额外配置
starter的autoConfiguratuin中自动配置了分页插件

http://www.lryc.cn/news/507318.html

相关文章:

  • 36. Three.js案例-创建带光照和阴影的球体与平面
  • CentOS 7 安装、测试和部署FastDFS
  • 全志H618 Android12修改doucmentsui选中图片资源详情信息
  • 【083】基于51单片机智能烘手器【Proteus仿真+Keil程序+报告+原理图】
  • uniApp使用腾讯地图提示未添加maps模块
  • 未来趋势系列 篇五:自主可控科技题材解析和股票梳理
  • Springboot 学习 之 logback-spring.xml 日志压缩 .tmp 临时文件问题
  • maven-resources-production:ratel-fast: java.lang.IndexOutOfBoundsException
  • K8s docker-compose的入门
  • 去雾Cycle-GAN损失函数
  • word实现两栏格式公式居中,编号右对齐
  • vtie项目中使用到了TailwindCSS,如何打包成一个单独的CSS文件(优化、压缩)
  • shell脚本案例
  • 完整微服务设计 功能实现
  • JWT令牌与微服务
  • C# WinForm移除非法字符的输入框
  • 智慧商城:基于请求数据动态渲染购物车列表
  • 医疗信息化浪潮下 SSM+Vue 医院预约挂号系统的崛起
  • QScreen在Qt5.15与Qt6.8版本下的区别
  • 模具生产过程中的标签使用流程图
  • Unity-URP设置单独渲染UI相机
  • 如何使用java来解析一个pdf文件呢?
  • asp.net core发布配置端口号,支持linux
  • M3D: 基于多模态大模型的新型3D医学影像分析框架,将3D医学图像分析从“看图片“提升到“理解空间“的层次,支持检索、报告生成、问答、定位和分割等8类任务
  • JavaScript中,常用crypto模块进行rsa加密,crypto-js模块进行md5算法
  • 机器学习04-为什么Relu函数
  • 基于Arduino的自动开瓶系统
  • 通过使用 contenteditable=“true“,我们彻底防止了 iOS 系统键盘的弹出
  • 20241217使用M6000显卡在WIN10下跑whisper来识别中英文字幕
  • 搜索召回:召回聚合