当前位置:   article > 正文

springboot+mybatis+pagehelper实现分页查询_mybatis pagereq

mybatis pagereq

案例代码下载地址:https://github.com/snowlavenderlove/springbootPagehelper.git

1.数据库springimport_excel,表user,表结构如下:

2. 创建项目springbootPagehelper,创建springboot项目教程博文:https://blog.csdn.net/qq_37231511/article/details/90669242

3.通过mybatis-generator自动生成代码,mybatis-generator教程博文:https://blog.csdn.net/qq_37231511/article/details/90692784,将生成的代码放到项目中,如图

4. 编辑pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>1.5.12.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.xue</groupId>
  12. <artifactId>springbootPagehelper</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springbootPagehelper</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. <maven-jar-plugin.version>3.0.0</maven-jar-plugin.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-data-jpa</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework.boot</groupId>
  27. <artifactId>spring-boot-starter-web</artifactId>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.mybatis.spring.boot</groupId>
  31. <artifactId>mybatis-spring-boot-starter</artifactId>
  32. <version>1.3.0</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>mysql</groupId>
  36. <artifactId>mysql-connector-java</artifactId>
  37. <scope>runtime</scope>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-test</artifactId>
  42. <scope>test</scope>
  43. </dependency>
  44. <dependency>
  45. <groupId>commons-logging</groupId>
  46. <artifactId>commons-logging</artifactId>
  47. <version>1.2</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>com.alibaba</groupId>
  51. <artifactId>druid</artifactId>
  52. <version>1.1.17</version>
  53. </dependency>
  54. <dependency>
  55. <groupId>com.github.pagehelper</groupId>
  56. <artifactId>pagehelper-spring-boot-starter</artifactId>
  57. <version>1.1.0-beta</version>
  58. </dependency>
  59. </dependencies>
  60. <build>
  61. <plugins>
  62. <plugin>
  63. <groupId>org.springframework.boot</groupId>
  64. <artifactId>spring-boot-maven-plugin</artifactId>
  65. </plugin>
  66. </plugins>
  67. </build>
  68. </project>

5 .编辑application.properties

  1. #mysql
  2. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  3. spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springimport_excel?&useUnicode=true&characterEncoding=UTF-8
  4. spring.datasource.username=root
  5. spring.datasource.password=123456
  6. #druid
  7. spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
  8. #dao
  9. mybatis.type-aliases-package==com.xue.repository.dao
  10. mybatis.mapper-locations=classpath*:com/xue/repository/mapper/*.xml
  11. #thymeleaf
  12. #spring.thymeleaf.prefix=classpath:/templates/
  13. #spring.thymeleaf.suffix=.jsp
  14. spring.thymeleaf.encoding=UTF-8
  15. #pagehelper
  16. #配置helperDialect属性来指定分页插件使用哪种数据库
  17. pagehelper.helperDialect=MySQL
  18. #分页合理化参数,默认值为false。当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询
  19. pagehelper.reasonable=false
  20. #支持通过 Mapper 接口参数来传递分页参数,默认值false,分页插件会从查询方法的参数值中,自动根据上面 params 配置的字段中取值,查找到合适的值时就会自动分页
  21. pagehelper.supportMethodsArguments=false
  22. #
  23. pagehelper.params=count=countSql
  24. #默认值为 false,该参数对使用 RowBounds 作为分页参数时有效。 当该参数设置为 true 时,会将 RowBounds 中的 offset 参数当成 pageNum 使用,可以用页码和页面大小两个参数进行分页
  25. pagehelper.offsetAsPageNum=true
  26. #默认值为 false,当该参数设置为 true 时,如果 pageSize=0 或者 RowBounds.limit = 0 就会查询出全部的结果(相当于没有执行分页查询,但是返回结果仍然是 Page 类型)
  27. pagehelper.pageSizeZero=true

6.在entity包下创建http包,并创建类UserReq、BasePageReq、UserListRes、BasePageRes

 UserReq

  1. package com.xue.entity.http;
  2. public class UserReq extends BasePageReq{
  3. private Integer id;
  4. private String username;
  5. private String password;
  6. public Integer getId() {
  7. return id;
  8. }
  9. public void setId(Integer id) {
  10. this.id = id;
  11. }
  12. public String getUsername() {
  13. return username;
  14. }
  15. public void setUsername(String username) {
  16. this.username = username;
  17. }
  18. public String getPassword() {
  19. return password;
  20. }
  21. public void setPassword(String password) {
  22. this.password = password;
  23. }
  24. }

BasePageReq:

 

  1. package com.xue.entity.http;
  2. public class BasePageReq {
  3. private int pageSize;
  4. private int pageIndex;
  5. public int getPageSize() {
  6. return pageSize;
  7. }
  8. public void setPageSize(int pageSize) {
  9. this.pageSize = pageSize;
  10. }
  11. public int getPageIndex() {
  12. return pageIndex;
  13. }
  14. public void setPageIndex(int pageIndex) {
  15. this.pageIndex = pageIndex;
  16. }
  17. }

 UserListRes:

  1. package com.xue.entity.http;
  2. import java.util.List;
  3. import com.xue.entity.model.User;
  4. public class UserListRes extends BasePageRes{
  5. private List<User> dataList;
  6. public List<User> getDataList() {
  7. return dataList;
  8. }
  9. public void setDataList(List<User> dataList) {
  10. this.dataList = dataList;
  11. }
  12. }

BasePageRes:

  1. package com.xue.entity.http;
  2. public class BasePageRes {
  3. private int allCount;
  4. public int getAllCount() {
  5. return allCount;
  6. }
  7. public void setAllCount(int allCount) {
  8. this.allCount = allCount;
  9. }
  10. }

 7.创建controller,创建类UserController

  1. package com.xue.controller;
  2. import javax.servlet.http.HttpServletRequest;
  3. import javax.servlet.http.HttpSession;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.ui.Model;
  7. import org.springframework.web.bind.annotation.RequestBody;
  8. import org.springframework.web.bind.annotation.RequestMapping;
  9. import org.springframework.web.bind.annotation.ResponseBody;
  10. import com.github.pagehelper.PageInfo;
  11. import com.xue.entity.http.UserListRes;
  12. import com.xue.entity.http.UserReq;
  13. import com.xue.entity.model.User;
  14. import com.xue.service.UserService;
  15. @Controller
  16. public class UserController {
  17. @Autowired
  18. private UserService userService;
  19. @RequestMapping("/select")
  20. @ResponseBody
  21. public UserListRes indexSelect(HttpSession session,HttpServletRequest request,@RequestBody UserReq inparam){
  22. UserListRes res = null;
  23. try {
  24. res = userService.selectAllUser(inparam);
  25. } catch (Exception e) {
  26. // TODO Auto-generated catch block
  27. e.printStackTrace();
  28. }
  29. if(null == res){
  30. System.out.println("未获得数据");
  31. }
  32. return res;
  33. }
  34. }

8.创建service层,创建接口UserService,并创建接口实现类UserServiceImpl

 UserService

  1. package com.xue.service;
  2. import com.github.pagehelper.PageInfo;
  3. import com.xue.entity.http.UserListRes;
  4. import com.xue.entity.http.UserReq;
  5. import com.xue.entity.model.User;
  6. public interface UserService {
  7. public UserListRes selectAllUser(UserReq req);
  8. }

UserServiceImpl

  1. package com.xue.service.impl;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.stereotype.Service;
  5. import com.github.pagehelper.PageHelper;
  6. import com.github.pagehelper.PageInfo;
  7. import com.xue.entity.http.UserListRes;
  8. import com.xue.entity.http.UserReq;
  9. import com.xue.entity.model.User;
  10. import com.xue.repository.dao.UserMapper;
  11. import com.xue.service.UserService;
  12. @Service
  13. public class UserServiceImpl implements UserService {
  14. @Autowired
  15. private UserMapper userMapper;
  16. @Override
  17. public UserListRes selectAllUser(UserReq req) {
  18. // pageIndex为当前页码;pageSize每页显示记录数;count=true则进行count查询总记录数
  19. PageHelper.startPage(req.getPageIndex(),req.getPageSize(),true);
  20. List<User> datas = userMapper.selectAllUser();
  21. PageInfo<User> page = new PageInfo<User>(datas);
  22. UserListRes res = new UserListRes();
  23. res.setAllCount((int)page.getTotal());
  24. res.setDataList(datas);
  25. return res;
  26. }
  27. }

9.dao层编辑UserMpper.java与UserMapper.xml

UserMapper.java最后添加:

    List<User> selectAllUser();
 UserMapper.xml最后添加:

  1. <select id="selectAllUser" resultMap="BaseResultMap">
  2. select * from user
  3. </select>

 10.主程序类SpringbootPagehelperApplication

  1. package com.xue;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. @SpringBootApplication
  6. @MapperScan("com.xue.repository.dao")
  7. public class SpringbootPagehelperApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(SpringbootPagehelperApplication.class, args);
  10. }
  11. }

11.至此代码完成,用postman进行测试,效果如图:

第一页,展示五条数据:

第二页,展示五条数据: 

 

注:如果遇到sql报错limit ?语句错误,或者是代码运行无问题,但是查出来的数据是全部数据,则是pagehelper版本问题导致,此案例springboot版本为1.5.2,pagehelper版本未1.1.0-beta,在这两个错误上卡了很久,网上有些解决方式为pom.xml中要配合其他依赖,有的为application.properties不用配置pagehelper属性,但是这两种方式都没有用,最后更换版本解决了问题

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号