SpringBoot+PageHelper实现分页查询

1.添加pom文件依赖

<!-- spring boot的分页插件PageHelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>

2.配置方式(1)application.proprieties(两种配置方式都可实现,配置其一就行)

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

 配置方式(2)MybatisConfig

/**
* PageHelper分页配置
*/
@Bean
public PageHelper pageHelper() {
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum", "true");
properties.setProperty("rowBoundsWithCount", "true");
properties.setProperty("pageSizeZero", "true");
properties.setProperty("reasonable", "true");
//配置mysql数据库的方言
properties.setProperty("dialect", "mysql");
properties.setProperty("reasonable", "true");
pageHelper.setProperties(properties);
return pageHelper;
}

3.开始代码Controller层

package com.lyancafe.material.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyancafe.material.service.UserService;
import com.lyancafe.material.dao.UserDao;
import com.lyancafe.material.model.UserModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("user")
/**
* @author scy 2018.8.7
*/
public class UserController {

private static final Logger logger = LoggerFactory.getLogger(UserController.class);

@Autowired
private UserService userService;
/**
* 分页查询所有用户列表
*
* @param pageNum
* @return
*/
@RequestMapping(value = "getAllUser", method = RequestMethod.GET)
public String getAllUser(Model model, @RequestParam(defaultValue = "1", required = true, value = "pageNum") Integer pageNum) {
//每页显示记录数
Integer pageSize = 8;
//分页查询,startPage是告诉拦截器说我要开始分页了。分页参数是这两个。
PageHelper.startPage(pageNum, pageSize);
List<UserModel> userModel = userService.findAll();
PageInfo pageInfo = new PageInfo(userModel);
model.addAttribute("userModel", userModel);
model.addAttribute("pageInfo", pageInfo);
return "allUser";
}
}

3.ServiceImpl层

package com.lyancafe.material.Service;

import com.lyancafe.material.dao.UserDao;
import com.lyancafe.material.model.UserModel;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
/**
* @author 孙创业
*/
public class UserServiceImpl implements UserService {


@Autowired
private UserDao userDao;
/**
* 查询User的全部数据
*/
@Override
public List<UserModel> findAll() {
return userDao.findAll();
}
}

4.Service层

package com.lyancafe.material.Service;

import com.lyancafe.material.model.UserModel;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
* @author 孙创业
*/
public interface UserService {
/**
* 根据所有用户
* @return
*/
List<UserModel> findAll();
}

5.Dao层

package com.lyancafe.material.dao;

import com.lyancafe.material.model.UserModel;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
* @author 孙创业
*/
public interface UserDao {
/**
* 根据所有用户
* @return
*/
List<UserModel> findAll();

}

6.Model(实体类)

package com.lyancafe.material.model;

/**
* @author 孙创业
*/
public class UserModel {
private int id;
private String userName;
private String password;
private String realName;
private String tel;
private int age;
private String address;

public UserModel() {
}
public UserModel(int id) {
this.id = id;
}
public UserModel(int id, String userName, String password, String realName, String tel, int age, String address) {
this.id = id;
this.userName = userName;
this.password = password;
this.realName = realName;
this.tel = tel;
this.age = age;
this.address = address;
}

public String getTel() {
return tel;
}

public void setTel(String tel) {
this.tel = tel;
}

public int getId() {
return id;
}

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

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassword() {
return password;
}

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

public String getRealName() {
return realName;
}

public void setRealName(String realName) {
this.realName = realName;
}

public int getAge() {
return age;
}

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

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return "UserModel{" +
"id=" + id +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", realName='" + realName + '\'' +
", tel='" + tel + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}

7.Dao.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.lyancafe.material.dao.UserDao">
<select id="findAll" resultType="com.lyancafe.material.model.UserModel">
select *
from t_user
</select>
</mapper>

8.allUser.jsp页面(其中的css样式就不上传了)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用户列表</title>
<link rel="stylesheet" type="text/css" href="<%=path%>css/allUser.css">
<script src="<%=path%>js/allUser.js"></script>
</head>
<body>
<h6><a href="<%=basePath%>user/toAddUser"><input type="button" id="btn_allUser" value="添加用户"></a></h6>
<div id="login_frame">
<p id="image_logo"><img src="<%=path%>images/lyancafe.png"></p>
<table border="1">
<tbody>
<tr>
<th>用户Id</th>
<th>用户名</th>
<th>年龄</th>
<th>联系方式</th>
<th>真实姓名</th>
<th>地址</th>
<th>操作</th>
</tr>
<c:if test="${!empty userModel}">
<c:forEach items="${userModel}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.userName }</td>
<td>${user.age }</td>
<td>${user.tel}</td>
<td>${user.realName}</td>
<td>${user.address}</td>
<td>
<div id="login_control">
<a href="<%=basePath%>user/getUser?id=${user.id}">
<input type="button" id="btn_editUser" value="编辑">
</a>
<a href="<%=basePath%>user/delUser?id=${user.id}">
<input type="button" id="btn_del" value="删除">
</a>
</div>
</td>
</tr>
</c:forEach>
</c:if>
</tbody>
</table>
<div align="center">
<font size="2">总${pageInfo.total}条记录</font>
<font size="2">共 ${pageInfo.pages}页</font>
<font size="2">第${pageInfo.pageNum}页</font>
<a href="/user/getAllUser?pageNum=1">首页</a>
<c:choose>
<c:when test="${pageInfo.pageNum - 1 > 0}">
<a href="/user/getAllUser?pageNum=${pageInfo.pageNum - 1}">上一页</a>
</c:when>
<c:when test="${pageInfo.pageNum - 1 <= 0}">
<a href="/user/getAllUser?pageNum=1">上一页</a>
</c:when>
</c:choose>
<c:choose>
<c:when test="${pageInfo.pages==0}">
<a href="/user/getAllUser?pageNum=${pageInfo.pageNum}">下一页</a>
</c:when>
<c:when test="${pageInfo.pageNum + 1 < pageInfo.pages}">
<a href="/user/getAllUser?pageNum=${pageInfo.pageNum + 1}">下一页</a>
</c:when>
<c:when test="${pageInfo.pageNum + 1 >= pageInfo.pages}">
<a href="/user/getAllUser?pageNum=${pageInfo.pages}">下一页</a>
</c:when>
</c:choose>
<c:choose>
<c:when test="${pageInfo.pages==0}">
<a href="/user/getAllUser?pageNum=${pageInfo.pageNum}">尾页</a>
</c:when>
<c:otherwise>
<a href="/user/getAllUser?pageNum=${pageInfo.pages}">尾页</a>
</c:otherwise>
</c:choose>
</div>
</div>
</body>
</html>

访问路径:http://localhost:8080/user/getAllUser

有什么问题可以联系我,如果没有及时回复,加qq:501397578

posted @ 2018-08-22 18:31  暖瞳123  阅读(1253)  评论(0编辑  收藏  举报