使用PageHelper实现分页的步骤:
第一步:创建数据库:
DROP DATABASE emp_dep;
CREATE DATABASE emp_dep;
USE emp_dep;
CREATE TABLE department(
id INT PRIMARY KEY AUTO_INCREMENT,
DepartmentName VARCHAR(50) NOT NULL
);
INSERT INTO department(id, DepartmentName)
VALUES (1001, '教学部'),
(1002, '市场部'),
(1003, '教研部'),
(1004, '运营部'),
(1005, '后勤部');
CREATE TABLE employee(
id INT PRIMARY KEY AUTO_INCREMENT,
EmployeeName VARCHAR(50) NOT NULL,
email VARCHAR(50),
gender INT,
birthday DATETIME,
did INT REFERENCES department (id)
);
INSERT INTO employee (EmployeeName, email, gender, birthday, did)
VALUES ('海康', '10086@qq.com', 1, NOW(), 1001),
('湛江', '10086@qq.com', 0, NOW(), 1002),
('桥头', '10086@qq.com', 1, NOW(), 1003),
('南粤', '10086@qq.com', 0, NOW(), 1004),
('粤西', '10086@qq.com', 1, NOW(), 1005),
('松竹', '10086@qq.com', 1, NOW(), 1001),
('南宁', '10086@qq.com', 0, NOW(), 1002),
('西藏', '10086@qq.com', 1, NOW(), 1003),
('内蒙', '10086@qq.com', 0, NOW(), 1004),
('西安', '10086@qq.com', 1, NOW(), 1005),
('南京', '10086@qq.com', 1, NOW(), 1001),
('瑞丽', '10086@qq.com', 0, NOW(), 1002),
('大理', '10086@qq.com', 1, NOW(), 1003),
('桂林', '10086@qq.com', 0, NOW(), 1004),
('新疆', '10086@qq.com', 1, NOW(), 1005),
('明天', '10086@qq.com', 1, NOW(), 1005),
('苏州', '10086@qq.com', 1, NOW(), 1001),
('郑州', '10086@qq.com', 0, NOW(), 1002),
('绿城', '10086@qq.com', 1, NOW(), 1003),
('昆明', '10086@qq.com', 0, NOW(), 1004),
('南兴', '10086@qq.com', 1, NOW(), 1005);
SELECT * FROM employee;
第二步:导入pox依赖
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.3.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
第三步:配置xml文件
主要配置数据源和pageHelper相关的配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/emp_dep?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC
username: root
password: root
pagehelper:
#指定数据库的方言
helper-dialect: mysql
#分页合理化参数,默认值为false。当该参数设置为 true 时,
#pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。
reasonable: true
#支持通过 Mapper 接口参数来传递分页参数,默认值false,
#分页插件会从查询方法的参数值中,自动根据上面 params 配置的字段中取值,查找到合适的值时就会自动分页
support-methods-arguments: true
#为了支持startPage(Object params)方法,增加了该参数来配置参数映射,用于从对象中根据属性名取值
params: =count=countSql
第四步:创建pojo类
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Employee {
private Integer id;
private String employeeName;
private String email;
private Integer gender;
private String birthday;
private Integer did;
private String departmentName;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Department {
private Integer id;
private String departmentName;
}
第五步:dao层
@Mapper@Repositorypublic interface EmployeeMapper{
// @Select("select emp.*,dep.departmentName from employee emp,department dep where emp.did=dep.id")
List<Employee> getAllEmployee();
}
@Mapper
@Repository
public interface DepartmentMapper {
// @Select("select dep.* from department")
List<Department> getAllDepartment();
}
第六步:Service层
public interface DepartmentService {
List<Department> getAllDepartment();
}
public interface EmployeeService {
List<Employee> getAllEmployee();
}
实现类
@Service
public class DepartmentServiceImp implements DepartmentService {
@Autowired
DepartmentMapper departmentMapper;
@Override
public List<Department> getAllDepartment() {
return departmentMapper.getAllDepartment();
}
}
@Service
public class EmployeeServiceImpl implements EmployeeService {
@Autowired
EmployeeMapper employeeMapper;
@Override
public List<Employee> getAllEmployee() {
return employeeMapper.getAllEmployee();
}
}
第七步:控制器
@Autowired
EmployeeServiceImpl employeeService;
@GetMapping("/getAllEmployee")
public String getAllEmployee(Model model, @RequestParam(defaultValue = "1" ,value = "pageNum")Integer pageNum){
PageHelper.startPage(pageNum,5);
List<Employee> list = employeeService.getAllEmployee();
PageInfo<Employee> pageInfo = new PageInfo<>(list);
model.addAttribute("pageInfo",pageInfo);
return "list";
}
第八步:编写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.haikang.springbootmybatis.dao.DepartmentMapper">
<select id="getAllDepartment" resultType="com.haikang.springbootmybatis.pojo.Department">
select * from department
</select>
</mapper>
<?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.haikang.springbootmybatis.dao.EmployeeMapper">
<select id="getAllEmployee" resultType="com.haikang.springbootmybatis.pojo.Employee">
select emp.*,dep.departmentName from employee emp,department dep where emp.did=dep.id
</select>
</mapper>
第九步:指定实现Dao层的xml文件的位置及返回实体的位置
mybatis:
mapper-locations: classpath:com/haikang/springbootmybatis/mapper/*.xml
# mapper-locations: classpath:com.haikang.springbootmybatis.mapper.*.xml
type-aliases-package: com.haikang.springbootmybatis.pojo
第十步:编写页面
<p>当前<span th:text="${pageInfo.pageNum}"></span> 页,共 <span th:text="${pageInfo.pages}"></span>页,共
<span th:text="${pageInfo.total}"></span> 条记录
</p>
<div class="ui pagination menu">
<a th:href="@{/getAllEmployee}" class="item">首页</a>
<!--判断是否有前一页,如果有就显示前一页,否则显示第一页,hasPreviousPage是否有上一页,返回是布尔值-->
<a th:href="@{/getAllEmployee(pageNum=${pageInfo.hasPreviousPage}?${pageInfo.prePage}:1)}" class="item">上一页</a>
<!--navigatepageNums是导航页码数,就是遍历所有页数-->
<b th:each="nav:${pageInfo.navigatepageNums}">
<a th:href="'/getAllEmployee?pageNum='+${nav}" th:text="${nav}"
th:if="${nav!=pageInfo.pageNum}" class="item"></a>
<span style="font-width: bold;background-color: red" th:if="${nav==pageInfo.pageNum}" th:text="${nav}" class="item"></span>
</b>
<!--判断是否有下一页,如果有就显示下一页,否则显示最后一页,hasNextPage是否有下一页,返回是布尔值-->
<a th:href="@{/getAllEmployee(pageNum=${pageInfo.hasNextPage}?${pageInfo.nextPage}:${pageInfo.pages})}" class="item">下一页</a>
<a th:href="@{/getAllEmployee(pageNum=${pageInfo.pages})}" class="item">尾页</a>
</div>
测试结果: