案例-员工管理-分页条件查询
package com.itheima.controller;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.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 java.time.LocalDate;
import java.time.Period;
import java.util.List;
@Slf4j
@RestController
public class EmpController {
@Autowired
EmpService empService;
/*分页查询*/
/* @GetMapping("/emps")
public Result count(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize) {
log.info("分页查询获取列表数据" + page+"," + pageSize);
PageBean PageBean = empService.count(page, pageSize);
return Result.success(PageBean);
}
*/
/*分页条件查询*/
@GetMapping("/emps")
public Result page(Integer page,Integer pageSize,String name,Short gender,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
log.info("分页条件查询:参数:{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
PageBean pageBean = empService.page(page,pageSize,name,gender,begin,end);
return Result.success(pageBean);
}
}
package com.itheima.service;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import java.time.LocalDate;
import java.util.List;
public interface EmpService {
/*分页查询*/
PageBean count(Integer page, Integer pageSize);
PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end);
}
package com.itheima.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.BeanNameAware;
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.获取分页查询结果列表
Integer start = (page - 1) * pageSize;
List<Emp> empList = empMapper.page(start, pageSize);
//3.封装pageBean对象
PageBean pageBean =new PageBean(count,empList);
return pageBean;
}*/
/*分页查询PageHelper插件*/
@Override
public PageBean count(Integer page, Integer pageSize) {
//1.设置分页参数
PageHelper.startPage(page, pageSize);
//2.执行查询
List<Emp> empList = empMapper.list();
Page<Emp> p = (Page<Emp>) empList;
//3.封装pageBean对象
PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
return pageBean;
}
/*分页条件查询*/
@Override
public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
PageHelper.startPage(page, pageSize);
List<Emp> empList = empMapper.empList(name, gender, begin, end);
Page<Emp> p = (Page<Emp>) empList;
PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
return pageBean;
}
}
package com.itheima.mapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDate;
import java.util.List;
@Mapper
public interface EmpMapper {
/*
*//*查询总记录数*//*
@Select("select count(*) from emp")
public Long count();
*//*分页查询获取列表数据*//*
@Select("select * from emp limit #{index},#{pageSize}")
public List<Emp> page(Integer index,Integer pageSize);*/
/*员工信息的查询*/
@Select("select * from emp")
public List<Emp> list();
/*员工分页条件查询*/
List<Emp> empList(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="com.itheima.mapper.EmpMapper">
<select id="empList" resultType="com.itheima.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>