tlias管理系统-员工分页条件查询实现功能实现

This commit is contained in:
2025-10-11 12:02:44 +08:00
parent 1298e154a5
commit 7b98564143
5 changed files with 42 additions and 10 deletions

View File

@@ -6,11 +6,10 @@ import com.inmind.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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.util.List;
@RestController
@Slf4j
@@ -30,7 +29,13 @@ public class EmpController {
,@RequestParam(defaultValue = "10") Integer pageSize){
log.info("分页查询参数page {}pageSize:{}----{},{},{},{}",page,pageSize,name,gender,begin,end);
//调用业务层获取分页结果数据
PageBean pageBean = empService.page(page,pageSize);
PageBean pageBean = empService.page(page,pageSize,name,gender,begin,end);
return Result.success(pageBean);
}
@DeleteMapping("/emps/{ids}")
public Result delete(@PathVariable List<Integer> ids){
log.info("批量删除:{}",ids);
return Result.success();
}
}

View File

@@ -4,6 +4,7 @@ import com.inmind.pojo.Emp;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDate;
import java.util.List;
@Mapper
@@ -22,6 +23,6 @@ public interface EmpMapper {
List<Emp> getPageList(Integer startIndex,Integer pageSize);*/
//员工的基本查询sql
@Select("select * from emp")
public List<Emp> list();
//@Select("select * from emp"),被动态sql替换
public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
}

View File

@@ -2,7 +2,9 @@ package com.inmind.service;
import com.inmind.pojo.PageBean;
import java.time.LocalDate;
public interface EmpService {
//分页查询员工列表数据
PageBean page(Integer page, Integer pageSize);
//条件分页查询员工列表数据
PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end);
}

View File

@@ -9,6 +9,7 @@ import com.inmind.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.List;
@Service
@@ -34,11 +35,12 @@ public class EmpServiceImpl implements EmpService {
@Override
public PageBean page(Integer page, Integer pageSize) {
public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
//1.调用分页插件PageHelper的固定apistartPage
PageHelper.startPage(page, pageSize);
//2.执行原始的查询sql语句(此时已经被分页插件修改了底层sql它返回值其实是一个Page类型对象)
List<Emp> empList = empMapper.list();
//在原始sql上要添加条件查询在条件查询的基础上进行分页操作
List<Emp> empList = empMapper.list(name,gender,begin,end);
Page<Emp> p = (Page) empList;
//2.封装pagebean对象
PageBean pageBean = new PageBean(p.getTotal(), p.getResult());

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.inmind.mapper.EmpMapper">
<select id="list" resultType="com.inmind.pojo.Emp">
select * from emp
<where>
<if test="name != null and name != '' ">
name like concat('%',#{name},'%')
</if>
<if test="gender != null">
and gender = #{gender}
</if>
<if test="begin != null and end != null">
and entrydate between #{begin} and #{end}
</if>
</where>
order by update_time desc
</select>
</mapper>