在pom.xml中导入pagehelper的依赖
<!--pagehelper分页的依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
在mybatis分页中配置相关信息
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
</configuration>
在数据访问层编写多条件查询相关方法
public List<Employee> getAllEmployee(@Param("deptno") String deptno, @Param("ename")String ename, @Param("hiredate1")String hiredate1, @Param("hiredate2")String hiredate2, @Param("sal1")String sal1, @Param("sal2")String sal2);
<resultMap id="employeeWithDepartment" type="employee">
<id property="empno" column="empno"/>
<association property="department" column="deptno" select="com.ruanyuan.springboot.dao.DepartmentDao.getDepartmentById"/>
</resultMap>
<select id="getAllEmployee" resultMap="employeeWithDepartment">
select * from employee
<where>
<if test="deptno!='' and deptno!=-1">
and deptno=#{deptno}
</if>
<if test="ename!=null and ename!=''">
and ename like concat('%',#{ename},'%')
</if>
<if test="hiredate1!=null and hiredate1!=''">
and hiredate >=#{hiredate1}
</if>
<if test="hiredate2!=null and hiredate2!=''">
and hiredate <=#{hiredate2}
</if>
<if test="sal1!=null and sal1!=''">
and sal >=#{sal1}
</if>
<if test="sal2!=null and sal2!=''">
and sal <=#{sal2}
</if>
</where>
</select>
编写业务逻辑层相关方法
public PageInfo<Employee> getAllEmpoyee(int pageNo, int pageSize, @Param("deptno") String deptno, @Param("ename")String ename, @Param("hiredate1")String hiredate1, @Param("hiredate2")String hiredate2, @Param("sal1")String sal1, @Param("sal2")String sal2);
@Override
public PageInfo<Employee> getAllEmpoyee(int pageNo, int pageSize, @Param("deptno") String deptno, @Param("ename")String ename, @Param("hiredate1")String hiredate1, @Param("hiredate2")String hiredate2, @Param("sal1")String sal1, @Param("sal2")String sal2) {
PageHelper.startPage(pageNo,pageSize);
List<Employee> employees=employeeDao.getAllEmployee(deptno,ename,hiredate1,hiredate2,sal1,sal2);
System.out.println("员工集合"+employees);
PageInfo<Employee> pageInfo=new PageInfo<Employee>(employees);
return pageInfo;
}
编写控制层相关方法
@RequestMapping("/getAllEmployee")
public String getAllEmployee(Model model, @RequestParam(required=true,defaultValue="1")int pageNo, @RequestParam(required=false,defaultValue="4") int pageSize,@RequestParam(required=false,defaultValue="-1") String deptno,
@RequestParam(required=false,defaultValue="") String ename,@RequestParam(required=false,defaultValue="") String hiredate1,@RequestParam(required=false,defaultValue="") String hiredate2,
@RequestParam(required=false,defaultValue="") String sal1,@RequestParam(required=false,defaultValue="") String sal2){
PageInfo<Employee> pageInfo=employeeService.getAllEmpoyee(pageNo,pageSize,deptno,ename,hiredate1,hiredate2,sal1,sal2);
System.out.println(pageInfo);
String url="/employee/getAllEmployee?deptno="+deptno+"&ename="+ename+"&hiredate1="+hiredate1+"&hiredate2="+hiredate2+"&sal1="+sal1+"&sal2="+sal2;
List<Department> departments=departmentService.getAllDepartment();
model.addAttribute("departments",departments);
model.addAttribute("pageInfo",pageInfo);
model.addAttribute("url",url);
model.addAttribute("deptno2", Integer.valueOf(deptno));
System.out.println("部门"+deptno);
model.addAttribute("ename", ename);
model.addAttribute("hiredate1", hiredate1);
model.addAttribute("hiredate2", hiredate2);
model.addAttribute("sal1", sal1);
model.addAttribute("sal2", sal2);
return "EmployeeList";
}