springboot 整合 tkmapper(通用mapper) + pagehelper 分页失效

本文主要是介绍springboot 整合 tkmapper(通用mapper) + pagehelper 分页失效,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • 引入pom文件
  • 开启扫描tkmapper
  • service层代码
  • 分页失效,返回全部数据

引入pom文件

		<dependency><groupId>tk.mybatis</groupId><artifactId>mapper-spring-boot-starter</artifactId><version>2.1.5</version></dependency><!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.2.13</version></dependency>

开启扫描tkmapper

@MapperScan("com.carenmg.shangpu.mapper")
@SpringBootApplication
public class ShangpuApplication {public static void main(String[] args) {SpringApplication.run(ShangpuApplication.class, args);}
}

service层代码

public PageInfo<Info> findByPage(Integer page, Integer limit, Integer types_pid, Integer city_pid, Integer city_id, Integer types_id, Integer mark_id, Integer mark, String keyword) {PageHelper.startPage(page, limit);//分页代码Example example = new Example(Info.class);if (!ObjectUtils.isEmpty(keyword)) {example.and().andLike("title", "%" + keyword + "%");}if (mark.equals(1)) {//已处理example.and().andNotEqualTo("mark_id", 1);if(mark_id.equals(0)){List<Integer> allMark = markService.findAllId();example.and().andIn("mark_id",allMark);}else{example.and().andEqualTo("mark_id",mark_id);}} else {//未处理example.and().andEqualTo("mark_id", 1);}if (city_id.equals(0)) {//全部List<Integer> allCitiesIdByPid = cityService.findAllCitiesIdByPid(city_pid);example.and().andIn("city_id", allCitiesIdByPid);} else {//区域example.and().andEqualTo("city_id", city_id);}if (types_id.equals(0)) {//全部List<Integer> allTypesIdByPid = typesService.findAllTypesIdByPid(types_pid);example.and().andIn("types_id",allTypesIdByPid);} else {//部分example.and().andEqualTo("types_id",types_id);}List<Info> infos = mapper.selectByExample(example);return new PageInfo<>(infos);}

分页失效,返回全部数据

{"code": 0,"msg": "OK","data": {"total": 26,"list": [...//这里省略了],"pageNum": 1,"pageSize": 26,"size": 26,"startRow": 0,"endRow": 25,"pages": 1,"prePage": 0,"nextPage": 0,"isFirstPage": true,"isLastPage": true,"hasPreviousPage": false,"hasNextPage": false,"navigatePages": 8,"navigatepageNums": [1],"navigateFirstPage": 1,"navigateLastPage": 1}
}

发现返回数据一共有26条,但是我设置的是10条;分析日志如下:

2021-04-05 15:40:40.560  INFO 2437 --- [nio-8000-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-04-05 15:40:40.560  INFO 2437 --- [nio-8000-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-04-05 15:40:40.564  INFO 2437 --- [nio-8000-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 4 ms
2021-04-05 15:40:40.571  INFO 2437 --- [nio-8000-exec-2] com.zaxxer.hikari.HikariDataSource       : defaultDataSource - Starting...
2021-04-05 15:40:40.582  INFO 2437 --- [nio-8000-exec-2] com.zaxxer.hikari.HikariDataSource       : defaultDataSource - Start completed.
2021-04-05 15:40:40.583 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.m.M.selectByExample_COUNT          : ==>  Preparing: SELECT count(0) FROM fa_mark 
2021-04-05 15:40:40.583 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.m.M.selectByExample_COUNT          : ==> Parameters: 
2021-04-05 15:40:40.584 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.m.M.selectByExample_COUNT          : <==      Total: 1
2021-04-05 15:40:40.585 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.mapper.MarkMapper.selectByExample  : ==>  Preparing: SELECT id,title,remark FROM fa_mark order by id ASC LIMIT ? 
2021-04-05 15:40:40.585 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.mapper.MarkMapper.selectByExample  : ==> Parameters: 10(Integer)
2021-04-05 15:40:40.586 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.mapper.MarkMapper.selectByExample  : <==      Total: 5
2021-04-05 15:40:40.589 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.mapper.InfoMapper.selectByExample  : ==>  Preparing: SELECT id,url,title,contact,phone,qrcode,city_id,types_id,mark,mark_id,created_time,updated_time,house_id FROM fa_info WHERE ( ( mark_id <> ? ) and ( mark_id in ( ? , ? , ? , ? , ? ) ) and ( city_id = ? ) and ( types_id = ? ) ) 
2021-04-05 15:40:40.589 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.mapper.InfoMapper.selectByExample  : ==> Parameters: 1(Integer), 1(Integer), 2(Integer), 3(Integer), 4(Integer), 5(Integer), 10(Integer), 10(Integer)
2021-04-05 15:40:40.595 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.mapper.InfoMapper.selectByExample  : <==      Total: 26

发现sql不对劲,怎么给另一个sevice的查询分页了?将分页代码放到需要分页的查询之前:

//前面的省略了,删除方法第一行的分页代码,放到这里就可以了
PageHelper.startPage(page, limit);
List<Info> infos = mapper.selectByExample(example);
return new PageInfo<>(infos);

返回数据

{"code": 0,"msg": "OK","data": {"total": 26,"list": [...],//这里省略了"pageNum": 1,"pageSize": 10,"size": 10,"startRow": 1,"endRow": 10,"pages": 3,"prePage": 0,"nextPage": 2,"isFirstPage": true,"isLastPage": false,"hasPreviousPage": false,"hasNextPage": true,"navigatePages": 8,"navigatepageNums": [1,2,3],"navigateFirstPage": 1,"navigateLastPage": 3}
}

查看log日志

2021-04-05 15:45:11.384  INFO 2437 --- [nio-8000-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-04-05 15:45:11.385  INFO 2437 --- [nio-8000-exec-2] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2021-04-05 15:45:11.388  INFO 2437 --- [nio-8000-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 3 ms
2021-04-05 15:45:11.403  INFO 2437 --- [nio-8000-exec-2] com.zaxxer.hikari.HikariDataSource       : defaultDataSource - Starting...
2021-04-05 15:45:11.414  INFO 2437 --- [nio-8000-exec-2] com.zaxxer.hikari.HikariDataSource       : defaultDataSource - Start completed.
2021-04-05 15:45:11.414 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.mapper.MarkMapper.selectByExample  : ==>  Preparing: SELECT id,title,remark FROM fa_mark order by id ASC 
2021-04-05 15:45:11.414 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.mapper.MarkMapper.selectByExample  : ==> Parameters: 
2021-04-05 15:45:11.415 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.mapper.MarkMapper.selectByExample  : <==      Total: 5
2021-04-05 15:45:11.424 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.m.I.selectByExample_COUNT          : ==>  Preparing: SELECT count(0) FROM fa_info WHERE ((mark_id <> ?) AND (mark_id IN (?, ?, ?, ?, ?)) AND (city_id = ?) AND (types_id = ?)) 
2021-04-05 15:45:11.424 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.m.I.selectByExample_COUNT          : ==> Parameters: 1(Integer), 1(Integer), 2(Integer), 3(Integer), 4(Integer), 5(Integer), 10(Integer), 10(Integer)
2021-04-05 15:45:11.425 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.m.I.selectByExample_COUNT          : <==      Total: 1
2021-04-05 15:45:11.425 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.mapper.InfoMapper.selectByExample  : ==>  Preparing: SELECT id,url,title,contact,phone,qrcode,city_id,types_id,mark,mark_id,created_time,updated_time,house_id FROM fa_info WHERE ( ( mark_id <> ? ) and ( mark_id in ( ? , ? , ? , ? , ? ) ) and ( city_id = ? ) and ( types_id = ? ) ) LIMIT ? 
2021-04-05 15:45:11.426 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.mapper.InfoMapper.selectByExample  : ==> Parameters: 1(Integer), 1(Integer), 2(Integer), 3(Integer), 4(Integer), 5(Integer), 10(Integer), 10(Integer), 10(Integer)
2021-04-05 15:45:11.428 DEBUG 2437 --- [nio-8000-exec-2] c.c.s.mapper.InfoMapper.selectByExample  : <==      Total: 10

limit应用在了fa_info这个表,正确

这篇关于springboot 整合 tkmapper(通用mapper) + pagehelper 分页失效的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/598353

相关文章

springboot健康检查监控全过程

《springboot健康检查监控全过程》文章介绍了SpringBoot如何使用Actuator和Micrometer进行健康检查和监控,通过配置和自定义健康指示器,开发者可以实时监控应用组件的状态,... 目录1. 引言重要性2. 配置Spring Boot ActuatorSpring Boot Act

使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)

《使用Java解析JSON数据并提取特定字段的实现步骤(以提取mailNo为例)》在现代软件开发中,处理JSON数据是一项非常常见的任务,无论是从API接口获取数据,还是将数据存储为JSON格式,解析... 目录1. 背景介绍1.1 jsON简介1.2 实际案例2. 准备工作2.1 环境搭建2.1.1 添加

Java实现任务管理器性能网络监控数据的方法详解

《Java实现任务管理器性能网络监控数据的方法详解》在现代操作系统中,任务管理器是一个非常重要的工具,用于监控和管理计算机的运行状态,包括CPU使用率、内存占用等,对于开发者和系统管理员来说,了解这些... 目录引言一、背景知识二、准备工作1. Maven依赖2. Gradle依赖三、代码实现四、代码详解五

java如何分布式锁实现和选型

《java如何分布式锁实现和选型》文章介绍了分布式锁的重要性以及在分布式系统中常见的问题和需求,它详细阐述了如何使用分布式锁来确保数据的一致性和系统的高可用性,文章还提供了基于数据库、Redis和Zo... 目录引言:分布式锁的重要性与分布式系统中的常见问题和需求分布式锁的重要性分布式系统中常见的问题和需求

SpringBoot基于MyBatis-Plus实现Lambda Query查询的示例代码

《SpringBoot基于MyBatis-Plus实现LambdaQuery查询的示例代码》MyBatis-Plus是MyBatis的增强工具,简化了数据库操作,并提高了开发效率,它提供了多种查询方... 目录引言基础环境配置依赖配置(Maven)application.yml 配置表结构设计demo_st

在Ubuntu上部署SpringBoot应用的操作步骤

《在Ubuntu上部署SpringBoot应用的操作步骤》随着云计算和容器化技术的普及,Linux服务器已成为部署Web应用程序的主流平台之一,Java作为一种跨平台的编程语言,具有广泛的应用场景,本... 目录一、部署准备二、安装 Java 环境1. 安装 JDK2. 验证 Java 安装三、安装 mys

Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单

《Springboot的ThreadPoolTaskScheduler线程池轻松搞定15分钟不操作自动取消订单》:本文主要介绍Springboot的ThreadPoolTaskScheduler线... 目录ThreadPoolTaskScheduler线程池实现15分钟不操作自动取消订单概要1,创建订单后

oracle数据库索引失效的问题及解决

《oracle数据库索引失效的问题及解决》本文总结了在Oracle数据库中索引失效的一些常见场景,包括使用isnull、isnotnull、!=、、、函数处理、like前置%查询以及范围索引和等值索引... 目录oracle数据库索引失效问题场景环境索引失效情况及验证结论一结论二结论三结论四结论五总结ora

JAVA中整型数组、字符串数组、整型数和字符串 的创建与转换的方法

《JAVA中整型数组、字符串数组、整型数和字符串的创建与转换的方法》本文介绍了Java中字符串、字符数组和整型数组的创建方法,以及它们之间的转换方法,还详细讲解了字符串中的一些常用方法,如index... 目录一、字符串、字符数组和整型数组的创建1、字符串的创建方法1.1 通过引用字符数组来创建字符串1.2

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.