pageHelper分页查询


前言

本篇文章介绍在java中使用pageHelper完成分页的功能。
使用的数据库为mysql,持久层框架mybatis。


一、集成

1.引入库

<!-- pageHelper -->
<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper-spring-boot-starter</artifactId>
	<version>1.2.5</version>
</dependency>

2.配置yml

pagehelper:
    helperDialect: mysql
    reasonable: true
    supportMethodsArguments: true
    params: count=countSql

二、编码

1.Controller

@Controller
@RequestMapping("StudentInfoController")
public class StudentInfoController{
	@Autowired
	private StudentInfoService studentInfoService;

	@PostMapping("lists")
	@ResponseBody
	public Object lists(int pageNum,int pageSize,String name){
		PageInfo<StudentInfo> page=studentInfoService.lists(pageNum,pageSize,name) ;
		StudentResult result=new StudentResult(200, "请求成功", page.getList());
		return  result;
	}
}

2.Service

public class StudentInfoService{
	@Autowired
	private StudentInfoMapper studentInfoMapper;
	/**
	 * 分页查询
	 * @param pageNum
	 * @param pageSize
	 * @param name
	 * @return
	 */
	public PageInfo<StudentInfo> lists(int pageNum,int pageSize,String name){
		PageHelper.startPage(pageNum, pageSize);
		List<StudentInfo> list= studentInfoMapper.selectList(name);
		PageInfo<StudentInfo> pageInfo = new PageInfo<StudentInfo>(list);
		return  pageInfo;
	}
}

3.Mapper

@Repository
public interface StudentInfoMapper {
	List<StudentInfo> selectList(@Param("name") String name);
}

4.Mapper.xml

  <select id="selectList" resultMap="BaseResultMap">
    select * from t_student_info
    <where>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR}
      </if>
    </where>
    order by CAST(id AS SIGNED)
  </select>

5.StudentResult

public class StudentResult implements Serializable {
    private int code;
    private String msg;
    private Object data;

    public StudentResult() {
    }

    public StudentResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public StudentResult(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "StudentResult{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }
}

三、测试

1.根据页码和页数

在这里插入图片描述
在这里插入图片描述

2.根据name

在这里插入图片描述


总结

回到顶部

  • 15
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值