springBoot整合PageHelper实现分页

准备

依赖

        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.2.3</version>
        </dependency>

 

application.xml

pagehelper:
# helper-dialect:需要进行分页的数据库,如果不配置会默认进行分析
helper-dialect: mysql

# reasonable分页参数合理化,默认是false。
# 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页;
# 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据
reasonable: true

# support-methods-arguments支持通过 Mapper 接口参数来传递分页参数
# 默认值 false
support-methods-arguments: true

# params为了支持startPage(Object params)方法,增加了该参数来配置参数映射,
# 用于从对象中根据属性名取值
# 默认值为 countSql
params: count=countsql


Mapper.xml

sal语句中不用手动添加limit,拦截器直接拼接的limit,所以sql尾部不要加分号

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.dao.UserMapper">

  <sql id="Base_Column_List">
    user_id, card_type, card_no, user_name, user_sex, user_age, user_role
  </sql>
  <!--查询的Sql语句-->
  <select id="selectAll" resultType="com.example.entity.User">
    select <include refid="Base_Column_List"/> from user
  </select>
 </mapper>

 

test

@SpringBootTest
class SpringBootSsm08ApplicationTests {
    @Resource
    private UserMapper userMapper;
    @Test
    public void testMapper() {
        //这句话一定要在执行查询语句之前先声明,确定需要查询的页数和每页查询几条数据
        PageHelper.startPage(2,3);
        List<User> userList = userMapper.selectAll();
        PageInfo<User> pageInfo = new PageInfo<>(userList);
        List<User> list = pageInfo.getList();
        //输出一下pageInfo对象
        System.out.println(pageInfo);
        System.out.println("---------------");
        for(User user:list){
            System.out.println(user);
        }
    }
 }

 

访问验证

拦截后自动填加limit

2023-05-25 17:06:32.569  INFO 15896 --- [nio-8080-exec-1] o.a.c.c.C.[.[localhost].[/webServices]   : Initializing Spring DispatcherServlet 'dispatcherServlet'
2023-05-25 17:06:32.569  INFO 15896 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2023-05-25 17:06:32.570  INFO 15896 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
Creating a new SqlSession
SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19606068] was not registered for synchronization because synchronization is not active
Cache Hit Ratio [SQL_CACHE]: 0.0
JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@2b8d70ec] will not be managed by Spring
==>  Preparing: SELECT count(0) FROM people
==> Parameters: 
<==    Columns: count(0)
<==        Row: 3
<==      Total: 1
==>  Preparing: select * from people LIMIT ?
==> Parameters: 2(Integer)
<==    Columns: id, name, age, sex, birthday
<==        Row: 1, 张三, 23, 男, 1997-02-23
<==        Row: 2, 李四, 25, 男, 1998-02-23
<==      Total: 2
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@19606068]
pageInfo  PageInfo{pageNum=1, pageSize=2, size=2, startRow=1, endRow=2, total=3, pages=2, list=Page{count=true, pageNum=1, pageSize=2, startRow=0, endRow=2, total=3, pages=2, reasonable=true, pageSizeZero=true}[People(id=1, name=张三, sex=男, age=23, birthday=Sun Feb 23 00:00:00 CST 1997, employee=null), People(id=2, name=李四, sex=男, age=25, birthday=Mon Feb 23 00:00:00 CST 1998, employee=null)], prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=8, navigateFirstPage=1, navigateLastPage=2, navigatepageNums=[1, 2]}
---------------
People(id=1, name=张三, sex=男, age=23, birthday=Sun Feb 23 00:00:00 CST 1997, employee=null)
People(id=2, name=李四, sex=男, age=25, birthday=Mon Feb 23 00:00:00 CST 1998, employee=null)

 

PageInfo对象解析

{
pageNum=1, 
pageSize=2, 
size=2, 
startRow=1, 
endRow=2, 
total=3, 
pages=2, 
list=Page{count=true, pageNum=1, pageSize=2, startRow=0, endRow=2, total=3, pages=2, reasonable=true, pageSizeZero=true}[People(id=1, name=张三, sex=男, age=23, birthday=Sun Feb 23 00:00:00 CST 1997, employee=null), People(id=2, name=李四, sex=男, age=25, birthday=Mon Feb 23 00:00:00 CST 1998, employee=null)], prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=8, navigateFirstPage=1, navigateLastPage=2, navigatepageNums=[1, 2]}

 

posted on 2023-05-25 14:52  or追梦者  阅读(155)  评论(0编辑  收藏  举报