mybatis pageHelper分页

在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 &gt;=#{hiredate1}
		</if>
		<if test="hiredate2!=null and hiredate2!=''">
			and hiredate &lt;=#{hiredate2}
		</if>
		<if test="sal1!=null and sal1!=''">
			and sal &gt;=#{sal1}
		</if>
		<if test="sal2!=null and sal2!=''">
			and sal &lt;=#{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";
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值