JavaWeb(SpringBoot3+vue3)开发+教学管理系统项目实战之PageHelper分页插件
一,原始分页操作方法:
C层:
- package com.zidiu.controller;
-
- import com.zidiu.pojo.Emp;
- import com.zidiu.pojo.PageResult;
- import com.zidiu.pojo.Result;
- import com.zidiu.service.EmpService;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestParam;
- import org.springframework.web.bind.annotation.RestController;
-
- @RequestMapping("/emps")
- @Slf4j
- @RestController
- public class EmpController {
- @Autowired
- private EmpService empService;
-
- /**
- * 查询员工列表带分页
- */
- @GetMapping
- public Result page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer pageSize) {
- log.info("查询员工信息,页码page: {}, 每页记录数pageSize: {}", page, pageSize);
- PageResult<Emp> pageResult = empService.page(page, pageSize);
- return Result.success(pageResult);
- }
- }
复制代码 Service层:
- package com.zidiu.service;
-
- import com.zidiu.pojo.Emp;
- import com.zidiu.pojo.PageResult;
-
- import java.util.List;
-
- public interface EmpService {
- /**
- * 分页查询
- * @param page 页码
- * @param pageSize 每页记录数
- */
- PageResult<Emp> page(Integer page, Integer pageSize);
- }
复制代码 S实现层:
- package com.zidiu.service.impl;
-
- import com.zidiu.mapper.EmpMapper;
- import com.zidiu.pojo.Emp;
- import com.zidiu.pojo.PageResult;
- import com.zidiu.service.EmpService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
-
- @Service
- public class EmpServiceImpl implements EmpService {
- @Autowired
- private EmpMapper empMapper;
-
- /**
- * 查询员工列表
- */
- @Override
- public PageResult<Emp> page(Integer page, Integer pageSize) {
- // 1,获取总记录数
- Long total = empMapper.count();
- // 2,获取分页查询结果列表
- int start = (page - 1) * pageSize;
- List<Emp> rows = empMapper.list(start, pageSize);
- // 3,封装PageResult并返回
- return new PageResult<Emp>(total, rows);
- }
- }
复制代码 M层:
- package com.zidiu.mapper;
-
- import com.zidiu.pojo.Emp;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Select;
-
- import java.util.List;
-
- @Mapper
- public interface EmpMapper {
- /**
- * 查询所有员工信息
- */
- @Select("select e.*, d.name as deptName from emp e left join dept d on e.dept_id = d.id limit #{start}, #{pageSize}")
- List<Emp> list(int start, int pageSize);
-
- /**
- * 查询员工总数
- */
- @Select("select count(*) from emp e left join dept d on e.dept_id = d.id")
- Long count();
- }
复制代码 二,PageHelper分页插件
1). 在pom.xml引入依赖
- <!--分页插件PageHelper-->
- <dependency>
- <groupId>com.github.pagehelper</groupId>
- <artifactId>pagehelper-spring-boot-starter</artifactId>
- <version>2.1.1</version>
- </dependency>
复制代码 2). EmpMapper
- package com.zidiu.mapper;
-
- import com.zidiu.pojo.Emp;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Select;
-
- import java.util.List;
-
- @Mapper
- public interface EmpMapper {
- /**
- * 查询所有员工信息
- */
- /**
- * 查询所有的员工及其对应的部门名称
- */
- @Select("select e.*, d.name deptName from emp as e left join dept as d on e.dept_id = d.id")
- public List<Emp> list();
-
- /**
- * 查询员工总数
- */
- @Select("select count(*) from emp e left join dept d on e.dept_id = d.id")
- Long count();
- }
复制代码 3). EmpServiceImpl
- package com.zidiu.service.impl;
-
- import com.github.pagehelper.Page;
- import com.github.pagehelper.PageHelper;
- import com.zidiu.mapper.EmpMapper;
- import com.zidiu.pojo.Emp;
- import com.zidiu.pojo.PageResult;
- import com.zidiu.service.EmpService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
-
- @Service
- public class EmpServiceImpl implements EmpService {
- @Autowired
- private EmpMapper empMapper;
-
- /**
- * 查询员工列表
- */
- @Override
- public PageResult<Emp> page(Integer page, Integer pageSize) {
- //1. 设置分页参数,页码和每页记录数
- PageHelper.startPage(page,pageSize);
- //2. 执行查询
- List<Emp> empList = empMapper.list();
- Page<Emp> p = (Page<Emp>) empList;
-
- //3. 封装结果
- return new PageResult<Emp>(p.getTotal(), p.getResult());
- }
- }
复制代码
SpringBoot3+Vue3开发综合实战项目:
JavaWeb(SpringBoot3+vue3)开发+教学管理系统项目实战
|