Spring Boot之分页

2018-12-10 wjy329 Java学习 25

分页是我们 常用的功能,MySQL实现分页的原理 就是用limit,比如 select * from table limit 0,5;  意思就是从0行开始查询5条数据,那么要实现分页我们需要两个参数,起始位置和每页的数据量,起始位置需要计算,为了前端传值的方便,前端只需传入当前页码每页数据量即可,下面我们来具体实现:

数据准备:

-- ----------------------------
-- Table structure for person
-- ----------------------------
DROP TABLE IF EXISTS `person`;
CREATE TABLE `person` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of person
-- ----------------------------
INSERT INTO `person` VALUES ('1', 'wjy', '24');
INSERT INTO `person` VALUES ('2', 'wjy1', '25');
INSERT INTO `person` VALUES ('3', '25', '25');
INSERT INTO `person` VALUES ('4', 'wjy2', '25');
INSERT INTO `person` VALUES ('5', 'wjy3', '25');
INSERT INTO `person` VALUES ('6', 'wjy4', '26');
INSERT INTO `person` VALUES ('7', 'wjy5', '27');
INSERT INTO `person` VALUES ('8', 'wjy0', '24');

image.png

1.分页实体 

PageBean.java:

package com.wjy329.pagedemo.utils;

import java.util.List;

public class PageBean<T> {
//起始
private int offset;
// 当前页
private int currentPage;
// 每页显示的数据量
private int pageCount;
// 总记录数
private int totalCount;
// 总页数 = 总记录数 / 每页显示的行数 (+ 1)
private int totalPage;
// 分页查询到的数据
private List<T> pageData;

public int getOffset() {
return offset;
}

public void setOffset(int offset) {
this.offset = offset;
}

// 返回总页数
public int getTotalPage() {
if (totalCount % pageCount == 0) {
totalPage = totalCount / pageCount;
} else {
totalPage = totalCount / pageCount + 1;
}
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}

public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage){
this.currentPage = currentPage;
}
public int getPageCount() {
return pageCount;
}
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}

public List<T> getPageData() {
return pageData;
}
public void setPageData(List<T> pageData) {
this.pageData = pageData;
}
}


2.分页工具类

PageUtils.java:

package com.wjy329.pagedemo.utils;

import java.util.List;

public class PageUtils {
public static PageBean getPage(Integer currentPage, Integer pageCount, Integer total){
PageBean pageBean = new PageBean();
//如果传入的当前页面为空,那么默认为第一页
if(currentPage == null){
currentPage = 1;
}
//如果传入的数据量为空,默认显示5条
if(pageCount == null){
pageCount = 5;
}
//起始位置的计算
int offset = (currentPage-1)*pageCount;
pageBean.setCurrentPage(currentPage);
pageBean.setPageCount(pageCount);
pageBean.setTotalCount(total);
pageBean.setOffset(offset);

return pageBean;
}
}


3.实体类

person.java:

package com.wjy329.pagedemo.entity;


import java.io.Serializable;

public class Person implements Serializable {
private Integer id;

private String name;

private Integer age;

public Integer getId() {
return id;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}


4.PersonMapper.xml

<?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.wjy329.pagedemo.dao.PersonDao">
<resultMap id="BaseResultMap" type="com.wjy329.pagedemo.entity.Person">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="INTEGER" property="age" />
</resultMap>

<select id="findAllByPage" parameterType="com.wjy329.pagedemo.utils.PageBean" resultMap="BaseResultMap">
SELECT id,name,age
FROM person
LIMIT #{page.offset},#{page.pageCount}
</select>

<select id="getAllCnt" resultType="java.lang.Integer">
select count(id) from person
</select>

</mapper>


5.PersonDao.java

package com.wjy329.pagedemo.dao;

import com.wjy329.pagedemo.entity.Person;
import com.wjy329.pagedemo.utils.PageBean;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface PersonDao {
List<Person> findAllByPage(@Param(value="page")PageBean page);

Integer getAllCnt();
}


6.PersonService.java

package com.wjy329.pagedemo.service;

import com.wjy329.pagedemo.entity.Person;
import com.wjy329.pagedemo.utils.PageBean;

import java.util.List;

public interface PersonService {
PageBean queryByPage(Integer currentPage, Integer pageCount);
}


7.PersonServiceImpl.java

package com.wjy329.pagedemo.service.impl;

import com.wjy329.pagedemo.dao.PersonDao;
import com.wjy329.pagedemo.entity.Person;
import com.wjy329.pagedemo.service.PersonService;
import com.wjy329.pagedemo.utils.PageBean;
import com.wjy329.pagedemo.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PersonServiceImpl implements PersonService {

@Autowired
private PersonDao personDao;

@Override
public PageBean queryByPage(Integer currentPage,Integer pageCount) {
Integer total = 0;
List<Person> persons = null;
total = this.personDao.getAllCnt();
PageBean pageBean = PageUtils.getPage(currentPage,pageCount,total);
//查询出来的分页数据
persons = this.personDao.findAllByPage(pageBean);
pageBean.setPageData(persons);
return pageBean;
}
}


8.PersonController.java

package com.wjy329.pagedemo.controller;

import com.wjy329.pagedemo.entity.Person;
import com.wjy329.pagedemo.service.PersonService;
import com.wjy329.pagedemo.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/person")
public class PersonController {
@Autowired
private PersonService personService;

@RequestMapping("/getAll")
@ResponseBody
public PageBean getAll(Integer currentPage, Integer pageCount){
PageBean list = personService.queryByPage(currentPage, pageCount);
return list;
}
}


上面就是全部的代码,然后我们开始访问: localhost:80/person/getAll   由于没有传入参数,所以按工具类中的默认 值查询,也就是第一页,5条数据:

image.png

访问: localhost:80/person/getAll?currentPage=2&pageCount=5   返回第2页的5条数据

image.png

上面就是分页的简单实现,然后据需要配合前端 的页面传值和获取 数据等。

demo地址: https://github.com/wjy329/page-demo

本文作者:wjy329
版权声明:本博客除特殊说明外均属本人原创。如需转载请署名作者及文章出处。
评论