Mybatis中使用PageHelper详解

Mybatis中使用PageHelper详解

引入依赖

<dependency>
    <groupId>com.github.pagehelpergroupId>
    <artifactId>pagehelper-spring-boot-starterartifactId>
    <version>1.2.1version>
dependency>
编写mapper.xml文件
update>

<select id="selectByPage" resultType="User">
    SELECT * FROM user_info
select>

User对象

package com.liusl.hrm.model;

import java.io.Serializable;
import java.util.Date;

/**
 * created by l1 on 2017/12/25.
 * 用户实体类
 */
public class User implements Serializable{
    private Integer id;
    private String loginname;
    private String password;
    private Integer status;
    private Date createDate;
    private String username;

    //无参构造方法
    public User(){
        super();
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLoginname() {
        return loginname;
    }

    public void setLoginname(String loginname) {
        this.loginname = loginname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

编写UserDao类

/**
 * 查询所有数据,分页用
 * @return List
 */
public List selectByPage();
测试
@Test
public void selectByPage() throws Exception {
    List list = new ArrayList();
    Page page = PageHelper.startPage(2, 10);
    userDao.selectByPage();
    List result = page.getResult();
    System.out.println(JSON.toJSONString(result,true));
} 
  

查询语句一定要在PageHelper.startPage紧跟方法
两个参数分别是:要查询的页数和每页的条数

推荐文档

https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md
https://www.cnblogs.com/onetwo/p/7371778.html

你可能感兴趣的:(Springboot,mybatis,pagehelper,springboot)