一、SpringBoot框架搭建
【1】點擊:文件--->模塊,選擇 Spring Initializr,直接點擊下一個
【2】這個頁面選項是選擇SpringBoot需要的啟動依賴,在這里可以有很多選項,這里選擇 Web 和 Mysql 然后點擊下一步
【3】保存路徑,點擊完成
二、配置
1、在pom文件中引入Pagehelper分頁插件
<!-- 分頁插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
2、配置分頁插件
將application.properties配置文件改成yml后綴,即application.yml,進行如下配置
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
3、創建數據庫
咱們就以用戶數據庫為例來進行測試,數據庫創建如下:
CREATE DATABASE pagehelperdemodat;
USE pagehelperdemodat;
CREATE TABLE users(
id INT PRIMARY KEY AUTO_INCREMENT COMMENT 'id主鍵',
username VARCHAR(20) NOT NULL COMMENT '用戶名',
PASSWORD VARCHAR(20) NOT NULL COMMENT'用戶密碼'
);
INSERT INTO users (username,PASSWORD) VALUES("onestar","123");
INSERT INTO users (username,PASSWORD) VALUES("twostar","456");
插入多條數據方便分頁查看
三、代碼編寫
1、創建用戶實體類:User
package com.star.entity;
/**
* @Description: 用戶實體類
* @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() {
return id;
}
public void setId(Integer 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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", PASSWORD='" + PASSWORD + '\'' +
'}';
}
}
2、創建用戶持久層接口:UserDao
package com.star.dao;
import com.star.entity.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
/**
* @Description: 用戶持久層接口
* @Author: ONESTAR
* @Date: Created in 22:59 2020/3/27
* @QQ: 316392836
* @URL: http://122.51.28.187:8080/
*/
public interface UserDao {
//查詢所有用戶
@Select("select * from users")
List<User> getAllUser();
}
3、創建業務層接口和實現類:UserService、UserServiceImpl
package com.star.service;
import com.star.entity.User;
import java.util.List;
/**
* @Description: 用戶業務層接口
* @Author: ONESTAR
* @Date: Created in 23:04 2020/3/27
* @QQ: 316392836
* @URL: http://122.51.28.187:8080/
*/
public interface UserService {
//查詢所有用戶
List<User> getAllUser();
}
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: 用戶業務層接口實現類
* @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() {
return userDao.getAllUser();
}
}
4、創建用戶控制層: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: 用戶控制層
* @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";
}
}
在這里可以看到,PageHelper.startPage(int PageNum,int PageSize):,這個是用來設置頁面的位置和展示的數據條目數的,我們設置每頁展示5條數據。PageInfo用來封裝頁面信息,返回給前台界面,一些PageInfo的參數:
//當前頁
private int pageNum;
//每頁的數量
private int pageSize;
//當前頁的數量
private int size;
//當前頁展示的數據的起始行
private int startRow;
//當前頁展示的數據的結束行
private int endRow;
//總記錄數--所需要進行分頁的數據條數
private long total;
//總頁數
private int pages;
//頁面展示的結果集,比如說當前頁要展示20條數據,則此list為這20條數據
private List<T> list;
//前一頁頁碼
private int prePage;
//下一頁頁碼
private int nextPage;
//是否為第一頁,默認為false,是第一頁則設置為true
private boolean isFirstPage ;
//是否為最后一頁默認為false,是最后一頁則設置為true
private boolean isLastPage ;
//是否有前一頁,默認為false,有前一頁則設置為true
private boolean hasPreviousPage ;
//是否有下一頁,默認為false,有后一頁則設置為true
private boolean hasNextPage ;
//導航頁碼數,所謂導航頁碼數,就是在頁面進行展示的那些1.2.3.4...
//比如一共有分為兩頁數據的話,則將此值設置為2
private int navigatePages;
//所有導航頁號,一共有兩頁的話則為[1,2]
private int[] navigatepageNums;
//導航條上的第一頁頁碼值
private int navigateFirstPage;
//導航條上的最后一頁頁碼值
private int navigateLastPage;
5、添加 MapperScan 注解
修改 Application ,在類前添加 MapperScan 注解,修改后如下:
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);
}
}
6、創建index.xml頁面
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>分頁測試</title>
</head>
<body>
<H3>查詢所有用戶</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>當前 <span th:text="${pageInfo.pageNum}"></span> 頁,總 <span th:text="${pageInfo.pages}"></span> 頁,共 <span th:text="${pageInfo.total}"></span> 條記錄</p>
<a th:href="@{/findUser}">首頁</a>
<a th:href="@{/findUser(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}">上一頁</a>
<a th:href="@{/findUser(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}">下一頁</a>
<a th:href="@{/findUser(pageNum=${pageInfo.pages})}">尾頁</a>
</body>
</html>
四、運行測試
啟動 SpringBoot 工程,在瀏覽器輸入:http://localhost:8080/findUser ,可以看到網頁顯示用戶信息表格,點擊上一頁下一頁可以進行頁面切換