当前位置:网站首页>Use of PageHelper
Use of PageHelper
2022-07-24 12:46:00 【Little happy】
Catalog
PageHelper Integrate SpringBoot
1、 Create project
Create a springboot project
Import correlation dependency
<dependencies>
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Database driven -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--lombok Use -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--pagehelper-->
<!-- Paging plug-ins -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
<!-- Integrate mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
Create directory structure

establish application.yml file , And add the following configuration
spring:
datasource:
url: jdbc:mysql://localhost:3306/pagehelperdemodat?useUnicode=true&characterEncoding=UTF-8
username: root
password: th123456
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 2、 Create database
CREATE DATABASE pagehelperdemodat;
USE pagehelperdemodat;
CREATE TABLE users(
id INT PRIMARY KEY AUTO_INCREMENT COMMENT 'id Primary key ',
username VARCHAR(20) NOT NULL COMMENT ' user name ',
PASSWORD VARCHAR(20) NOT NULL COMMENT' User password '
);
INSERT INTO users (username,PASSWORD) VALUES(" Xiaokaixin 1","123456");
INSERT INTO users (username,PASSWORD) VALUES(" Xiaokaixin 2","123456");
INSERT INTO users (username,PASSWORD) VALUES(" Xiaokaixin 3","123456");
INSERT INTO users (username,PASSWORD) VALUES(" Xiaokaixin 4","123456");
INSERT INTO users (username,PASSWORD) VALUES(" Xiaokaixin 5","123456");
INSERT INTO users (username,PASSWORD) VALUES(" Xiaokaixin 6","123456");
INSERT INTO users (username,PASSWORD) VALUES(" Xiaokaixin 7","123456");
INSERT INTO users (username,PASSWORD) VALUES(" Xiaokaixin 8","123456");
3、 Contents of relevant documents
User.java
package com.xiaokaixin.pagehelper.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
/** * @Author xiaokaixin * @Date 2021/9/11 18:01 * @Version 1.0 */
@Data
@AllArgsConstructor
public class User {
private Integer id;
private String username;
private String password;
}
UserDao.java
package com.xiaokaixin.pagehelper.dao;
import com.xiaokaixin.pagehelper.entity.User;
import java.util.List;
/** * @Author xiaokaixin * @Date 2021/9/11 18:01 * @Version 1.0 */
public interface UserDao {
// Query so users
List<User> getAllUser();
}
UserMapper.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.xiaokaixin.pagehelper.dao.UserDao">
<select id="getAllUser" resultType="com.xiaokaixin.pagehelper.entity.User">
select * from users
</select>
</mapper>
UserService
package com.xiaokaixin.pagehelper.service;
import com.xiaokaixin.pagehelper.entity.User;
import java.util.List;
/** * @Author xiaokaixin * @Date 2021/9/11 18:05 * @Version 1.0 */
public interface UserService {
// Query so users
List<User> getAllUser();
}
UserServiceImpl
package com.xiaokaixin.pagehelper.service.impl;
import com.xiaokaixin.pagehelper.dao.UserDao;
import com.xiaokaixin.pagehelper.entity.User;
import com.xiaokaixin.pagehelper.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/** * @Author xiaokaixin * @Date 2021/9/11 18:05 * @Version 1.0 */
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserDao userDao;
@Override
public List<User> getAllUser() {
return userDao.getAllUser();
}
}
UserController
package com.xiaokaixin.pagehelper.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaokaixin.pagehelper.entity.User;
import com.xiaokaixin.pagehelper.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.RequestParam;
import java.util.List;
/** * @Author xiaokaixin * @Date 2021/9/11 18:08 * @Version 1.0 */
@Controller
public class UserController {
@Autowired
UserService userService;
@GetMapping("/")
public String findUser(Model model, @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
String orderBy = "id asc";
PageHelper.startPage(pageNum,5,orderBy);
List<User> list = userService.getAllUser();
PageInfo<User> pageInfo = new PageInfo<User>(list);
model.addAttribute("pageInfo",pageInfo);
return "index";
}
}
4、 Description of relevant parameters
// The current page
private int pageNum;
// The number of pages
private int pageSize;
// 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 number of records -- The number of data pieces that need to be paged
private long total;
// Total number of pages
private int pages;
// The result set displayed on the page , For example, the current page should be displayed 20 Data , Then this list For this 20 Data
private List<T> list;
// Page number of the previous page
private int prePage;
// Next page number
private int nextPage;
// Is it the first page , The default is false, The first page is set to true
private boolean isFirstPage ;
// If it is the last page, the default is false, The last page is set to true
private boolean isLastPage ;
// Is there a previous page , The default is false, The previous page is set to true
private boolean hasPreviousPage ;
// Is there a next page , The default is false, The next page is set to true
private boolean hasNextPage ;
// Number of navigation pages , The so-called navigation page number , Those displayed on the page 1.2.3.4...
// For example, if there are two pages of data , Set this value to 2
private int navigatePages;
// All navigation page numbers , A total of two pages is [1,2]
private int[] navigatepageNums;
// The first page number value on the navigation bar
private int navigateFirstPage;
// The last page number on the navigation bar
private int navigateLastPage;
5、index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title> Paging testing </title>
</head>
<body>
<H3> Query 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> At present <span th:text="${pageInfo.pageNum}"></span> page , total <span th:text="${pageInfo.pages}"></span> page , common <span th:text="${pageInfo.total}"></span> Bar record </p>
<a th:href="@{/}"> home page </a>
<a th:href="@{/(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}"> The previous page </a>
<a th:href="@{/(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}"> The next page </a>
<a th:href="@{/(pageNum=${pageInfo.pages})}"> Tail page </a>
</body>
</html>
6、 Final effect

边栏推荐
- Cluster construction based on kubernetes v1.24.0 (III)
- Getting started with SQL join use examples to learn left connection, inner connection and self connection
- Snowflake algorithm (PHP)
- 使用TypeFace设置TextView的文字字体
- I used annotations to configure the serialization of redis in one module project, and then introduced this module in another module. Why is this configuration
- Leetcode-81. search rotation sort array II (binary search returns true/false)
- What can breaking through the memory wall bring? See the actual battle of volcano engine intelligent recommendation service to save money and increase efficiency
- 02 linear structure 2 multiplication and addition of univariate polynomials (linked list solution)
- Reserved instances & Savings Plans
- Try... Finally summary
猜你喜欢

Cluster construction based on kubernetes v1.24.0 (I)

MobileViT:挑战MobileNet端侧霸主
让一套代码完美适配各种屏幕

使用TypeFace设置TextView的文字字体

What can breaking through the memory wall bring? See the actual battle of volcano engine intelligent recommendation service to save money and increase efficiency

Node takes effect after using NVM to install under Windows system, but NPM does not take effect

Support liuhaiping

thinkphp 实现数据库备份

Implementing deep learning framework from zero -- further exploration of the implementation of multilayer bidirectional RNN

从零实现深度学习框架——再探多层双向RNN的实现
随机推荐
Buckle practice - 31 effective tic tac toe games
Generator and async solve asynchronous programming
nacos部署
Localstorage
Set up CI server with Jenkins
Cluster construction based on kubernetes v1.24.0 (I)
1.4.1 many, many symbols (cin/cout unbinding, O3 optimization)
1.9. touch pad test
基于matlab的语音处理
The sixth question of ape Anthropology
Seckill implementation diagram
6-16 vulnerability exploitation -rlogin maximum permission login
Zabbix5.0.8-odbc monitoring Oracle11g
Deep and shallow copies of objects, extends
权限系统就该这么设计,yyds
Getting started with SQL join use examples to learn left connection, inner connection and self connection
SSM在线租房售房平台多城市版本
Implement is by yourself_ default_ constructible
Correct use of qwaitcondition
Ah, I am uncle card space