JavaWeb(SpringBoot3+vue3)开发+教学管理系统项目实战之条件分页查询
基础版(待优化):
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.format.annotation.DateTimeFormat;
- 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;
-
- import java.time.LocalDate;
-
- @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, String name, Integer gender, @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate begin,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
- log.info("查询员工信息,参数: page: {}, pageSize: {}, name: {}, gender: {}, begin: {}, end: {}", page, pageSize, name, gender, begin, end);
- PageResult<Emp> pageResult = empService.page(page, pageSize, name, gender, begin, end);
- return Result.success(pageResult);
- }
- }
复制代码 S层:
- package com.zidiu.service;
-
- import com.zidiu.pojo.Emp;
- import com.zidiu.pojo.PageResult;
-
- import java.time.LocalDate;
- import java.util.List;
-
- public interface EmpService {
- /**
- * 分页查询
- * @param page 页码
- * @param pageSize 每页记录数
- */
- PageResult<Emp> page(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end);
- }
复制代码 实现层:
- 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.time.LocalDate;
- import java.util.List;
-
- @Service
- public class EmpServiceImpl implements EmpService {
- @Autowired
- private EmpMapper empMapper;
-
- /**
- * 查询员工列表
- */
- @Override
- public PageResult<Emp> page(Integer page, Integer pageSize, String name, Integer gender, LocalDate begin, LocalDate end) {
- //1. 设置分页参数,页码和每页记录数
- PageHelper.startPage(page,pageSize);
- //2. 执行查询
- List<Emp> empList = empMapper.list(name, gender, begin, end);
- Page<Emp> p = (Page<Emp>) empList;
-
- //3. 封装结果
- return new PageResult<Emp>(p.getTotal(), p.getResult());
- }
- }
复制代码 M层:
- package com.zidiu.mapper;
-
- import com.zidiu.pojo.Emp;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Select;
-
- import java.time.LocalDate;
- import java.util.List;
-
- @Mapper
- public interface EmpMapper {
- /**
- * 查询所有的员工及其对应的部门名称
- */
- List<Emp> list(String name, Integer gender, LocalDate begin, LocalDate end);
- }
复制代码 X层:
- <!--定义Mapper映射文件的约束和基本结构-->
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.zidiu.mapper.EmpMapper">
- <select id="list" resultType="com.zidiu.pojo.Emp">
- select e.*, d.name deptName from emp as e left join dept as d on e.dept_id = d.id
- where e.name like concat('%',#{name},'%')
- and e.gender = #{gender}
- and e.entry_date between #{begin} and #{end}
- </select>
- </mapper>
复制代码 优化版:==================================
实体类,添加一个接收参数类作为实体封装:
- package com.zidiu.pojo;
-
- import lombok.Data;
- import org.springframework.format.annotation.DateTimeFormat;
-
- import java.time.LocalDate;
- @Data
- public class EmpQueryParam {
- private Integer page = 1; //页码
- private Integer pageSize = 10; //每页展示记录数
- private String name; //姓名
- private Integer gender; //性别
- @DateTimeFormat(pattern = "yyyy-MM-dd")
- private LocalDate begin; //入职开始时间
- @DateTimeFormat(pattern = "yyyy-MM-dd")
- private LocalDate end; //入职结束时间
- }
复制代码 C层:
- package com.zidiu.controller;
-
- import com.zidiu.pojo.Emp;
- import com.zidiu.pojo.EmpQueryParam;
- 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.format.annotation.DateTimeFormat;
- 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;
-
- import java.time.LocalDate;
-
- @RequestMapping("/emps")
- @Slf4j
- @RestController
- public class EmpController {
- @Autowired
- private EmpService empService;
-
- /**
- * 查询员工列表带分页
- */
- @GetMapping
- public Result page(EmpQueryParam empQueryParam) {
- log.info("查询请求参数: {}", empQueryParam);
- PageResult pageResult = empService.page(empQueryParam);
- return Result.success(pageResult);
- }
- }
复制代码 S层:
- package com.zidiu.service;
-
- import com.zidiu.pojo.Emp;
- import com.zidiu.pojo.EmpQueryParam;
- import com.zidiu.pojo.PageResult;
-
- import java.time.LocalDate;
- import java.util.List;
-
- public interface EmpService {
- /**
- * 分页查询
- */
- PageResult<Emp> page(EmpQueryParam empQueryParam);
- }
复制代码 实现层:
- 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.EmpQueryParam;
- import com.zidiu.pojo.PageResult;
- import com.zidiu.service.EmpService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.time.LocalDate;
- import java.util.List;
-
- @Service
- public class EmpServiceImpl implements EmpService {
- @Autowired
- private EmpMapper empMapper;
-
- /**
- * 查询员工列表
- */
- @Override
- public PageResult page(EmpQueryParam empQueryParam) {
- //1. 设置PageHelper分页参数
- PageHelper.startPage(empQueryParam.getPage(), empQueryParam.getPageSize());
- //2. 执行查询
- List<Emp> empList = empMapper.list(empQueryParam);
- //3. 封装分页结果
- Page<Emp> p = (Page<Emp>)empList;
- return new PageResult(p.getTotal(), p.getResult());
- }
- }
复制代码 M层:
- package com.zidiu.mapper;
-
- import com.zidiu.pojo.Emp;
- import com.zidiu.pojo.EmpQueryParam;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Select;
-
- import java.time.LocalDate;
- import java.util.List;
-
- @Mapper
- public interface EmpMapper {
- /**
- * 查询所有的员工及其对应的部门名称
- */
- List<Emp> list(EmpQueryParam empQueryParam);
- }
复制代码 X层:
- <!--定义Mapper映射文件的约束和基本结构-->
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.zidiu.mapper.EmpMapper">
- <select id="list" resultType="com.zidiu.pojo.Emp">
- select e.*, d.name deptName from emp as e left join dept as d on e.dept_id = d.id
- <where>
- <if test="name != null and name != ''">
- e.name like concat('%',#{name},'%')
- </if>
- <if test="gender != null">
- and e.gender = #{gender}
- </if>
- <if test="begin != null and end != null">
- and e.entry_date between #{begin} and #{end}
- </if>
- </where>
- </select>
- </mapper>
复制代码 在这里呢,我们用到了两个动态SQL的标签:<if> <where>。 这两个标签的具体作用如下:
<if>:判断条件是否成立,如果条件为true,则拼接SQL。
<where>:根据查询条件,来生成where关键字,并会自动去除条件前面多余的and或or。
配置层:
- spring:
- application:
- name: tlias-web-management
- #mysql连接配置
- datasource:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3306/tlias
- username: root
- password: root
-
- mybatis:
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- # 开启驼峰命名
- map-underscore-to-camel-case: true
- # 分页插件,页码输入负数,查询是有问题的,查不到对应的数据了
- # 分页合理化参数,默认值为false。当该参数设置为true时,pageNum<=0时会查询第一页
- # pageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询。
- pagehelper:
- reasonable: true
- helper-dialect: mysql
复制代码 JavaWeb(SpringBoot3+vue3)开发+教学管理系统项目实战之PageHelper分页插件
SpringBoot3+Vue3开发综合实战项目:
JavaWeb(SpringBoot3+vue3)开发+教学管理系统项目实战
demo.zip
(41.48 KB, 下载次数: 0, 售价: 50 金币)
localhost.sql
(9.79 KB, 下载次数: 0, 售价: 50 金币)
|