一、项目结构
二、POM
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.chen</groupId>
<artifactId>springboot-pagehelper</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
三、User.java
package com.chen.pagehelper.entity;
import java.io.Serializable;
public class User implements Serializable {
private Integer id;
private String name;
private Integer sex;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSex() {
return sex;
}
public void setSex(Integer sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", sex=" + sex +
", age=" + age +
'}';
}
}
四、UserMapper.java
package com.chen.pagehelper.mapper;
import com.chen.pagehelper.entity.User;
import org.apache.ibatis.annotations.Select;
import java.util.List;
public interface UserMapper {
@Select("SELECT * FROM `user`")
List<User> findAllUser();
}
五、UserService.java
package com.chen.pagehelper.service;
import com.chen.pagehelper.entity.User;
import com.chen.pagehelper.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> findAllUser() {
return userMapper.findAllUser();
}
}
六、IndexController.java
package com.chen.pagehelper.controller;
import com.chen.pagehelper.entity.User;
import com.chen.pagehelper.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class IndexController {
@Autowired
private UserService userService;
@RequestMapping("/index")
public ModelAndView index(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "10") Integer pageSize) {
ModelAndView mav = new ModelAndView();
Map<String, Object> map = new HashMap<String, Object>();
PageHelper.startPage(pageNum, pageSize);
List<User> users = userService.findAllUser();
PageInfo pageInfo = new PageInfo(users, 10);
mav.addObject("userPage", pageInfo);
mav.setViewName("index");
return mav;
}
@RequestMapping("/index1")
public Map<String, Object> index1(@RequestParam(required = false, defaultValue = "1") Integer start,
@RequestParam(required = false, defaultValue = "2") Integer n) {
Map<String, Object> map = new HashMap<String, Object>();
PageHelper.startPage(start, n);
List<User> users = userService.findAllUser();
PageInfo pageInfo = new PageInfo(users);
map.put("userPage", pageInfo);
PageHelper.startPage(start, n);
users = userService.findAllUser();
PageInfo pageInfo1 = new PageInfo(users, 5);
map.put("userPage1", pageInfo1);
return map;
}
}
七、APP.java
package com.chen.pagehelper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.chen.pagehelper.mapper")
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
八、index.ftl
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>用户列表</title>
</head>
<style>
.table4_1 table {
width: 100%;
margin: 15px 0;
border: 0;
}
.table4_1 th {
background-color: #93DAFF;
color: #000000
}
.table4_1, .table4_1 th, .table4_1 td {
font-size: 0.95em;
text-align: center;
padding: 4px;
border-collapse: collapse;
}
.table4_1 th, .table4_1 td {
border: 1px solid #000000;
border-width: 1px 1;
}
.table4_1 tr {
border: 1px solid #c1e9fe;
}
.table4_1 tr:nth-child(odd) {
background-color: #dbf2fe;
}
.table4_1 tr:nth-child(even) {
background-color: #fdfdfd;
}
div.digg {
padding: 3px;
margin: 3px;
text-align: center
}
div.digg a {
border: #aaaadd 1px solid;
padding: 2px 5px;
margin: 2px;
color: #000099;
text-decoration: none
}
div.digg a:hover {
border: #000099 1px solid;
color: #000;
}
div.digg a:active {
border: #000099 1px solid;
color: #000;
}
div.digg span.current {
border: solid 1px #000099;
padding: 2px 5px;
font-weight: bold;
margin: 2px;
color: #fff;
background-color: #000099;
}
div.digg span.disabled {
border: #eee 1px solid;
padding: 2px 5px;
margin: 2px;
color: #ddd;
}
</style>
<body>
<#--
total:${userPage.total!}<br>
pageNum:${userPage.pageNum!}<br>
pageSize:${userPage.pageSize!}<br>
size:${userPage.size!}<br>
pageNumRow:${userPage.pageNumRow!}<br>
endRow:${userPage.endRow!}<br>
pages:${userPage.pages!}<br>
prePage:${userPage.prePage!}<br>
nextPage:${userPage.nextPage!}<br>
isFirstPage:${userPage.isFirstPage?string("true","false")}<br>
isLastPage:${userPage.isLastPage?string("true","false")}<br>
hasPreviousPage:${userPage.hasPreviousPage?string("true","false")}<br>
hasNextPage:${userPage.hasNextPage?string("true","false")}<br>
navigatePages:${userPage.navigatePages!}<br>
navigateFirstPage:${userPage.navigateFirstPage!}<br>
navigateLastPage:${userPage.navigateLastPage!}<br>
lastPage:${userPage.lastPage!}<br>
firstPage:${userPage.firstPage!}<br>
navigatepageNums:
<#list userPage.navigatepageNums as navigatepageNums>
${navigatepageNums}
</#list>
-->
<table class="table4_1 table" align="center" width="60%" border="2px">
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
</tr>
<#list userPage.list as user>
<tr>
<th>${user.name}</th>
<th>${user.age}</th>
<th>
<#if user.sex==1>男
<#else>女
</#if>
</th>
</tr>
</#list>
</table>
<div class="digg">
<#if userPage.isFirstPage>
<span class="disabled">首页</span>
<#else><a href="/index?pageNum=1&pageSize=${userPage.pageSize}">首页</a>
</#if>
<#if userPage.hasPreviousPage>
<a href="/index?pageNum=${userPage.prePage}&pageSize=${userPage.pageSize}">上一页</a>
<#else><span class="disabled">上一页</span>
</#if>
<#list userPage.navigatepageNums as navigatepageNums>
<#if userPage.pageNum==navigatepageNums>
<span class="current">${navigatepageNums}</span>
<#else><a href="/index?pageNum=${navigatepageNums}&pageSize=${userPage.pageSize}">${navigatepageNums}</a>
</#if>
</#list>
<#if userPage.hasNextPage>
<a href="/index?pageNum=${userPage.nextPage}&pageSize=${userPage.pageSize}">下一页</a>
<#else><span class="disabled">下一页</span>
</#if>
<#if userPage.isLastPage>
<span class="disabled">尾页</span>
<#else><a href="/index?pageNum=${userPage.pages}&pageSize=${userPage.pageSize}">尾页</a>
</#if>
</div>
</body>
</html>
九、git地址
https://gitee.com/c9547/springboot-pagehelper.git