当前位置:网站首页>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

边栏推荐
- Native Crash的一切
- EfficientFormer:轻量化ViT Backbone
- How to render millions of 2D objects smoothly with webgpu?
- 猿人学第六题
- 2022.07.15 summer training personal qualifying (10)
- 做自媒体视频剪辑有免费可商用的素材网站吗?
- It is difficult for Chinese consumers and industrial chains to leave apple, and iPhone has too much influence
- Use abp Zero builds a third-party login module (III): web side development
- Why does 2.tostring() report an error
- Correct use of qwaitcondition
猜你喜欢
权限系统就该这么设计,yyds

Promise

Use abp Zero builds a third-party login module (4): wechat applet development

Cluster construction based on kubernetes v1.24.0 (I)

English语法_不定代词 - 概述

English grammar_ Indefinite pronouns - Overview

【C语言】详细的文件操作相关知识

Wechat applet - drawing dashboard

深圳地铁12号线第二批工程验收通过 预计7月28日试运行

基于Kubernetes v1.24.0的集群搭建(一)
随机推荐
基于matlab的声音识别
Wechat applet - drawing dashboard
Ah, I am uncle card space
Take chef and ansible as examples to get started with server configuration
The sixth question of ape Anthropology
The seventh topic of ape Anthropology
[C language] dynamic memory management
Ape anthropology topic 5
树莓派自建 NAS 云盘之——数据自动备份
Raspberry pie self built NAS cloud disk -- automatic data backup
Set up CI server with Jenkins
猿人学第五题
【C语言】动态内存管理
Implement is by yourself_ default_ constructible
Case summary of SSH service suddenly unable to connect
ASP. Net core deployment Manual: 1. Deployment Basics
ERROR: [Synth 8-439] module ‘xxx‘ not found not found 错误解决办法
SSM在线校园相册管理平台
Correct use of qwaitcondition
Compatibility problems of call, apply, bind and bind