![](https://img-blog.csdnimg.cn/direct/fcdd7ef1787340f2815ee68cbaee0356.png)
![](https://img-blog.csdnimg.cn/direct/b5c536d3e2f5443ab6baaf714e6751e7.png)
![](https://img-blog.csdnimg.cn/direct/fce615ab4e7944a38e6731fef3d43e65.png)
![](https://img-blog.csdnimg.cn/direct/085609e0ef3f488ca246399223eb2f37.png)
![](https://img-blog.csdnimg.cn/direct/283d76e2153648439bc8b52406cb2fc1.png)
@Slf4j
@RestController
public class EmpController {
@Autowired
private EmpService empService;
@GetMapping("/emps")
public Result page(@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer pageSize,
String name,
Short gender,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
// if (page == null) page = 1;
// if (pageSize == null) pageSize = 10;
log.info("分页查询: {}, {}, {}, {}, {}, {}", page, pageSize, name, gender, begin, end);
// 调用Service的方法进行分页查询
PageBean pageBean = empService.page(page, pageSize, name, gender, begin, end);
return Result.success(pageBean);
}
package cn.ensource.service;
import cn.ensource.pojo.PageBean;
import java.beans.JavaBean;
import java.time.LocalDate;
public interface EmpService {
/**
* 分页查询方法
*
*/
PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end);
}
package cn.ensource.service.impl;
import cn.ensource.mapper.EmpMapper;
import cn.ensource.pojo.Emp;
import cn.ensource.pojo.PageBean;
import cn.ensource.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
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 PageBean page(Integer page, Integer pageSize) {
// // 1. 获取总记录数
// long count = empMapper.count();
//
// // 2. 获取分页查询结果列表
// List<Emp> empList = empMapper.page((page - 1) * pageSize, pageSize);
//
// // 3. 封装PageBean对象
// return new PageBean(count, empList);
// }
@Override
public PageBean page(Integer page, Integer pageSize, String name, Short 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 PageBean(p.getTotal(), p.getResult());
}
}
package cn.ensource.mapper;
import cn.ensource.pojo.Emp;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDate;
import java.util.List;
@Mapper
public interface EmpMapper {
/**
* 查询总总记录数
* @return
*/
@Select("select count(*) from emp")
public Long count();
/**
* 分页查询获取列表数据
* @param start
* @param pageSize
* @return
*/
@Select("select * from emp limit #{start}, #{pageSize};")
public List<Emp> page(Integer start, Integer pageSize);
/**
* 员工信息的查询
*/
// @Select("select * from emp")
public List<Emp> list(String name, Short gender, LocalDate begin, LocalDate end);
}
<?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="cn.ensource.mapper.EmpMapper">
<!--条件查询-->
<select id="list" resultType="cn.ensource.pojo.Emp">
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>
![](https://img-blog.csdnimg.cn/direct/5953958900f1440da14690e0194e2740.png)
![](https://img-blog.csdnimg.cn/direct/97362123370c4ff6b7e787a8ef0e17a2.png)
![](https://img-blog.csdnimg.cn/direct/36b7424a908a4ba1bb850119c329d73b.png)