【1】點擊:文件--->模塊,選擇 Spring Initializr,直接點擊下一個html
【2】這個頁面選項是選擇SpringBoot須要的啓動依賴,在這裏能夠有不少選項,這裏選擇 Web 和 Mysql 而後點擊下一步java
【3】保存路徑,點擊完成mysql
<!-- 分頁插件 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.12</version> </dependency>
將application.properties配置文件改爲yml後綴,即application.yml,進行以下配置git
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
我們就以用戶數據庫爲例來進行測試,數據庫建立以下:github
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");
插入多條數據方便分頁查看web
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 + '\'' + '}'; } }
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(); }
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(); } }
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的參數:spring
//當前頁 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;
修改 Application ,在類前添加 MapperScan 註解,修改後以下:sql
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); } }
<!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 ,能夠看到網頁顯示用戶信息表格,點擊上一頁下一頁能夠進行頁面切換數據庫