First, SpringBoot framework construction
[1] Go to File –> Module, choose Spring Initializr, and click the next one
[2] The options on this page are to select the boot dependencies required by SpringBoot. There are many options available here. Here, select Web and Mysql and click Next
[3] Save the path and click Finish
Second, the configuration
1. Introduce Pagehelper pagination in POM files
<! -- Paging plugin -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
Copy the code
2. Configure the paging plug-in
Change the application.properties configuration file to the yml suffix, that is, application.yml, and perform the following configuration
spring:
datasource:
url: JDBC: mysql: / / 127.0.0.1:3306 / pagehelperdemodat? useUnicode=true&characterEncoding=UTF-8&serverTimezone=CST
username: root
password: 806188
driver-class-name: com.mysql.cj.jdbc.Driver
thymeleaf:
prefix: classpath:/templates/
check-template-location: true
suffix: .html
mode: HTML
encoding: UTF-8
cache: false
mybatis:
mapper-locations: classpath*:mapper/*.xml
pagehelper:
helper-dialect: mysql
params: count=countSql
reasonable: true
support-methods-arguments: true
Copy the code
Create a database
Let’s take the user database as an example to test, database creation is as follows:
CREATE DATABASE pagehelperdemodat;
USE pagehelperdemodat;
CREATE TABLE users(
id INT PRIMARY KEY AUTO_INCREMENT COMMENT 'id primary key',
username VARCHAR(20) NOT NULL COMMENT 'Username'.PASSWORD VARCHAR(20) NOT NULL COMMENT'User password'
);
INSERT INTO users (username,PASSWORD) VALUES("onestar"."123");
INSERT INTO users (username,PASSWORD) VALUES("twostar"."456");
Copy the code
Insert multiple pieces of data to facilitate paging view
Third, code writing
Create User entity class: User
package com.star.entity;
/ * * *@Description: user entity class *@Author: ONESTAR
* @Date: Created in 22:57 2020/3/27
* @QQ: 316392836
* @URL: http://122.51.28.187:8080/ * /
public class User {
private Integer id;
private String username;
private String PASSWORD;
public Integer getId(a) {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername(a) {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPASSWORD(a) {
return PASSWORD;
}
public void setPASSWORD(String PASSWORD) {
this.PASSWORD = PASSWORD;
}
@Override
public String toString(a) {
return "User{" +
"id=" + id +
", username='" + username + '\' ' +
", PASSWORD='" + PASSWORD + '\' ' +
'} '; }}Copy the code
Create user persistence layer interface: UserDao
package com.star.dao;
import com.star.entity.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/ * * *@Description: User persistence layer interface *@Author: ONESTAR
* @Date: Created in 22:59 2020/3/27
* @QQ: 316392836
* @URL: http://122.51.28.187:8080/ * /
public interface UserDao {
// Query all users
@Select("select * from users")
List<User> getAllUser(a);
}
Copy the code
3. Create the business layer interface and implementation classes UserService and UserServiceImpl
package com.star.service;
import com.star.entity.User;
import java.util.List;
/ * * *@Description: User business layer interface *@Author: ONESTAR
* @Date: Created in 23:04 2020/3/27
* @QQ: 316392836
* @URL: http://122.51.28.187:8080/ * /
public interface UserService {
// Query all users
List<User> getAllUser(a);
}
package com.star.service.impl;
import com.star.dao.UserDao;
import com.star.entity.User;
import com.star.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/ * * *@Description: User business layer interface implementation class *@Author: ONESTAR
* @Date: Created in 23:06 2020/3/27
* @QQ: 316392836
* @URL: http://122.51.28.187:8080/ * /
@Service
public class UserServiceImpl implements UserService {
@Resource
UserDao userDao;
@Override
public List<User> getAllUser(a) {
returnuserDao.getAllUser(); }}Copy the code
Create a user control layer: UserController
package com.star.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.star.entity.User;
import com.star.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
/ * * *@Description: User control layer *@Author: ONESTAR
* @Date: Created in 23:07 2020/3/27
* @QQ: 316392836
* @URL: http://122.51.28.187:8080/ * /
@Controller
public class UserController {
@Autowired
UserService userService;
@GetMapping("/findUser")
public String findUser(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
PageHelper.startPage(pageNum,5);
List<User> list = userService.getAllUser();
PageInfo<User> pageInfo = new PageInfo<User>(list);
model.addAttribute("pageInfo",pageInfo);
return "index"; }}Copy the code
Pagehelper. startPage(int PageNum,int PageSize): pageHelper. startPage(int PageNum,int PageSize): this is used to set the position of the page and the number of data items to display. PageInfo is used to encapsulate the page information and return it to the foreground interface. Some PageInfo parameters:
/ / the current page
private int pageNum;
// Number of pages per page
private int pageSize;
// The number of current pages
private int size;
// The starting line of the data displayed on the current page
private int startRow;
// The end line of the data displayed on the current page
private int endRow;
// Total records - the number of pieces of data that need to be paged
private long total;
/ / the total number of pages
private int pages;
// The result set of the page display, for example, the current page is to display 20 data, then the list is the 20 data
private List<T> list;
// The previous page number
private int prePage;
// Next page number
private int nextPage;
// If it is the first page, the default is false. If it is the first page, the default is true
private boolean isFirstPage ;
// Last page defaults to false, or true if it is the last page
private boolean isLastPage ;
// Whether there is a previous page, the default is false, if there is a previous page set to true
private boolean hasPreviousPage ;
// If there is a next page, the default is false and if there is a next page, the default is true
private boolean hasNextPage ;
// The number of navigation pages is the number of pages displayed on the page.
// If there are two pages of data, set this value to 2
private int navigatePages;
// all navigation page numbers are [1,2]
private int[] navigatepageNums;
// The first page of the navigation bar
private int navigateFirstPage;
// The last page of the navigation bar
private int navigateLastPage;
Copy the code
5. Add MapperScan annotations
Add MapperScan annotations to the Application class.
package com.star;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan({"com.star.dao"})
public class PagehelperdemoApplication {
public static void main(String[] args) { SpringApplication.run(PagehelperdemoApplication.class, args); }}Copy the code
6. Create the index.xml page
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Paging testing</title>
</head>
<body>
<H3>Querying All Users</H3>
<table border="1">
<tr>
<th>id</th>
<th>name</th>
<th>password</th>
</tr>
<tr th:each="user:${pageInfo.list}">
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.PASSWORD}"></td>
</tr>
</table>
<p>The current<span th:text="${pageInfo.pageNum}"></span>Page, the total<span th:text="${pageInfo.pages}"></span>Page,<span th:text="${pageInfo.total}"></span>records</p>
<a th:href="@{/findUser}">Home page</a>
<a th:href="@{/findUser(pageNum=${pageInfo.hasPreviousPage}? ${pageInfo.prePage}:1)}">The previous page</a>
<a th:href="@{/findUser(pageNum=${pageInfo.hasNextPage}? ${pageInfo.nextPage}:${pageInfo.pages})}">The next page</a>
<a th:href="@{/findUser(pageNum=${pageInfo.pages})}">back</a>
</body>
</html>
Copy the code
4. Run tests
Enter http://localhost:8080/findUser start SpringBoot project, in the browser, you can see page displays user information table, click on the back page can switch on the page