概述
进行如下的高级分页查询
查询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 <= #{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 <= #{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());
}
}
最后
以上就是愤怒樱桃为你收集整理的myBatis进行高级分页查询的全部内容,希望文章能够帮你解决myBatis进行高级分页查询所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复