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

This commit is contained in:
2025-11-16 16:38:37 +08:00
parent 636d9d7be5
commit 18a8f28e70
5 changed files with 60 additions and 17 deletions

View File

@@ -5,11 +5,14 @@ import com.inmind.pojo.Result;
import com.inmind.service.EmpService; import com.inmind.service.EmpService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; 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.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;
@RequestMapping("/emps") @RequestMapping("/emps")
@RestController @RestController
@Slf4j @Slf4j
@@ -20,19 +23,17 @@ public class EmpController {
@GetMapping @GetMapping
public Result page( public Result page(
@RequestParam(defaultValue = "1") Integer page @RequestParam(defaultValue = "1") Integer page
,@RequestParam(defaultValue = "10") Integer pageSize){ ,@RequestParam(defaultValue = "10") Integer pageSize
/*if (page == null) { , String name
page = 1; , Short gender
} ,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin
,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
if (pageSize == null) { )
pageSize = 10; {
}*/
log.info("分页查询:{}{}",page,pageSize); log.info("分页查询:{}{}",page,pageSize);
//调用员工业务层的分页查询功能 //调用员工业务层的分页查询功能
PageBean pageBean = empService.page(page,pageSize); PageBean pageBean = empService.page(page,pageSize,name,gender,begin,end);
return Result.success(pageBean); return Result.success(pageBean);
} }
} }

View File

@@ -4,6 +4,7 @@ import com.inmind.pojo.Emp;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Select;
import java.time.LocalDate;
import java.util.List; import java.util.List;
@Mapper @Mapper
@@ -17,13 +18,18 @@ public interface EmpMapper {
/** /**
* 分页查询 * 分页查询
*
* @param * @param
* @param * @param
* @param name
* @param gender
* @param begin
* @param end
* @return * @return
*/ */
/*@Select("select * from emp limit #{startIndex},#{pageSize}") /*@Select("select * from emp limit #{startIndex},#{pageSize}")
List<Emp> getPageList(int startIndex,int pageSize);*/ List<Emp> getPageList(int startIndex,int pageSize);*/
@Select("select * from emp") // @Select("select * from emp")
public List<Emp> list(); public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
} }

View File

@@ -2,12 +2,19 @@ package com.inmind.service;
import com.inmind.pojo.PageBean; import com.inmind.pojo.PageBean;
import java.time.LocalDate;
public interface EmpService { public interface EmpService {
/** /**
* 分页查询 * 分页查询
*
* @param page * @param page
* @param pageSize * @param pageSize
* @param name
* @param gender
* @param begin
* @param end
* @return * @return
*/ */
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.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.List; import java.util.List;
@Service @Service
@@ -46,16 +47,21 @@ public class EmpServiceImpl implements EmpService {
/** /**
* 分页查询 * 分页查询
* @param page 页码 *
* @param page 页码
* @param pageSize 每页显示条数 * @param pageSize 每页显示条数
* @param name
* @param gender
* @param begin
* @param end
* @return * @return
*/ */
@Override @Override
public PageBean page(Integer page, Integer pageSize) { public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
//使用分页插件,开始分页,传入页码和每页显示条数即可 //使用分页插件,开始分页,传入页码和每页显示条数即可
PageHelper.startPage(page,pageSize); PageHelper.startPage(page,pageSize);
//调用mapper的普通的查询语句即可(此时list方法已经被分页插件修改了底层sql它返回一个包含分页查询数据的page对象) //调用mapper的普通的查询语句即可(此时list方法已经被分页插件修改了底层sql它返回一个包含分页查询数据的page对象)
List<Emp> list = empMapper.list(); List<Emp> list = empMapper.list(name, gender, begin, end);
Page<Emp> p = (Page<Emp>) list; Page<Emp> p = (Page<Emp>) list;
//封装分页查询结果 //封装分页查询结果

View File

@@ -0,0 +1,23 @@
<?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"><!--注意resultType定义的是单条记录的java封装类型-->
select * from emp
<where>
<if test="name != null">
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>