myBatis进行高级分页查询

进行如下的高级分页查询
查询xx部门的员工的工资从xxx到xxx,并规定每一页显示的个数,进行分页
在这里插入图片描述

首先这是目录结构
在这里插入图片描述
首先是对mybatis的映射类的编写
EmployeeMapper.xml
其中,queryForList 是进行分页查找,queryFountCount 是进行返回符合条件的数目
先从queryFountCount 中返回符合条件的数目,供前台显示具体可以翻到的页数,然后才执行
queryForList 进行限制性的分页查找,传入一个javaBean来限制查询的启始页以及一次查询的条数

<?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.mybatis.com.hellow.Mapper.EmployeeMapper">

    <!--多个查询共同使用的sql-->
    <sql id ="base_where">
     <where>
            <if test="keyword!=null">
                and name LIKE concat('%',#{keyword},'%') or sn LIKE concat('%',#{keyword},'%')

            </if>
            <if test="minSalary!=null">
                and salsry >= #{minSalary}
            </if>
            <if test="maxSalary!=null">
                and salsry &lt;= #{maxSalary}
            </if>
            <if test="depId>0">
                and deptid = #{depId}
            </if>

        </where>

    </sql>


   <!--查询结果集-->
    <select id="queryForList"  resultMap="change">
        select id, name , sn , salsry , deptId from employee
        <include refid="base_where"></include>

        <!--页数大于0才做分页查询-->
        <if test="pageSize>0">
            limit #{start},#{pageSize};

        </if>

        <!--<where>
            <if test="keyword!=null">
                and name LIKE concat('%',#{keyword},'%') or sn LIKE concat('%',#{keyword},'%')

            </if>
            <if test="minSalary!=null">
                and salsry >= #{minSalary}
            </if>
            <if test="maxSalary!=null">
                and salsry &lt;= #{maxSalary}
            </if>
            <if test="depId>0">
                and deptid = #{depId}
            </if>

        </where>-->



    </select>

    <!--查询结果总数-->
    <select id="queryFountCount"  resultType="int">
        select  count(id) from  employee
        <include refid="base_where"></include>

    </select>




    <resultMap id="change" type="Employee">
        <result column="salsry" property="salary"></result>
        <result column="deptId" property="depId"></result>
    </resultMap>


</mapper>




有两个分装类(javabean),一个是对员工的高级查询信息的封装(EmployQueryObject),另一个是对分页内容进行封装(pageResult)

EmployQueryObject:

package cn.mybatis.com.hellow.query;

import lombok.Data;

import java.math.BigDecimal;

/**
 * 封装员工的高级查询信息
 */
@Data
public class EmployQueryObject {

    private String keyword;//关键字,员工的名字或员工编号
    private BigDecimal minSalary;//最低工资
    private  BigDecimal maxSalary;//最高工资
    private  Long depId = -1L;//部门ID,缺省为-1表示所有部门

    private int currentPage = 1;//当前页
    private int pageSize = 3;//每页的条数


    //分页查询: limit  start,pageSize,查询开始的数据
    public  int getStart(){
        return (currentPage-1)*pageSize;
    }

    //重写get方法,防止空字符串
    public String getKeyword(String str){
        return emptyStr(str);

    }

    //判空字符串
    private String  emptyStr(String str){
        return hasLength(str)? str:null;
    }

    //判空字符串
    private boolean hasLength(String str){
        return  str !=null &&  !"".equals(str.trim());

    }
}

PageResult :

package cn.mybatis.com.hellow.query;

import lombok.Getter;

import java.util.List;

/**
 * 对分页的内容进行封装
 */

@Getter
public class PageResult {
    private List<?> result;//每一页的结果集
    private int totalCount;//结果总数


    private int currentPage  = 1;//当前页
    private int pageSize = 3; //每一页最多有多少条数据


    //下面三个需要计算出来
    private int prevPage;//上一页
    private int nextPage;//下一页
    private int totalPage;//最后一页(一共多少页)


    public PageResult(List<?> result, int totalCount, int currentPage, int pageSize) {
        this.result = result;
        this.totalCount = totalCount;
        this.currentPage = currentPage;
        this.pageSize = pageSize;


        //最后一页
        this.totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize+1;

        //上一页
        this.prevPage = currentPage-1 >= 1 ? currentPage-1: 1;

        //下一页
        this.nextPage = currentPage+1 <= totalPage ? currentPage + 1 : totalPage;

        //安全设置当前页,最多只能到最后一页
        this.currentPage = currentPage > totalPage ? totalPage : currentPage;




    }
}

最后时service层

import cn.mybatis.com.hellow.domain.Employee;
import cn.mybatis.com.hellow.query.EmployQueryObject;
import cn.mybatis.com.hellow.query.PageResult;
import cn.mybatis.com.util.myBatisUtil;

import java.util.Collections;
import java.util.List;

public class EmployeeServiceImpl implements IEmployeeService {

    private EmployeeMapper employeeMapper = myBatisUtil.getMapper(EmployeeMapper.class);

    @Override
    public PageResult query(EmployQueryObject qo) {
        //查出符合条件的结果集个数
        int rows = employeeMapper.queryFountCount(qo);

        //结果总数每页
        if(rows == 0){
           //如果结果集为0条数目,则不进行分页查询
            return new PageResult(Collections.EMPTY_LIST,0,1,qo.getPageSize());
        }

        //如果结果集条数大于0,则进行分页查询
        //queryForList 取出 EmployQueryObject 封装好的提前设定的开始查询的位置,以及需要查询的个数
        List<Employee> result = employeeMapper.queryForList(qo);
        return new PageResult(result,rows,qo.getCurrentPage(),qo.getPageSize());

    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值