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

相关文章

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J

Java中的String.valueOf()和toString()方法区别小结

《Java中的String.valueOf()和toString()方法区别小结》字符串操作是开发者日常编程任务中不可或缺的一部分,转换为字符串是一种常见需求,其中最常见的就是String.value... 目录String.valueOf()方法方法定义方法实现使用示例使用场景toString()方法方法

Java中List的contains()方法的使用小结

《Java中List的contains()方法的使用小结》List的contains()方法用于检查列表中是否包含指定的元素,借助equals()方法进行判断,下面就来介绍Java中List的c... 目录详细展开1. 方法签名2. 工作原理3. 使用示例4. 注意事项总结结论:List 的 contain

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis