准备条件
1 .引入pageHelper 依赖包
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.0.0</version>
</dependency>
2.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">
<!-- mybatis 的主配置文件 -->
<configuration>
<!-- <typeAliases>
<typeAlias type="User" alias="user"/>
</typeAliases> -->
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="mysql"/>
<!-- 该参数默认为false -->
<!-- 设置为true时,会将RowBounds第一个参数offset当成pageNum页码使用 -->
<!-- 和startPage中的pageNum效果一样-->
<property name="offsetAsPageNum" value="true"/>
<!-- 该参数默认为false -->
<!-- 设置为true时,使用RowBounds分页会进行count查询 -->
<property name="rowBoundsWithCount" value="true"/>
<!-- 设置为true时,如果pageSize=0或者RowBounds.limit = 0就会查询出全部的结果 -->
<!-- (相当于没执行分页查询,但是返回结果仍然是Page类型-->
<property name="pageSizeZero" value="true"/>
<!-- 3.3.0版本可用 - 分页参数合理化,默认false禁用 -->
<!-- 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 -->
<!-- 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据 -->
<property name="reasonable" value="false"/>
<!-- 3.5.0版本可用 - 为了支持startPage(Object params)方法 -->
<!-- 增加了一个`params`参数来配置参数映射,用于从Map或ServletRequest中取值 -->
<!-- 可以配置pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默认值 -->
<!-- 不理解该含义的前提下,不要随便复制该配置 -->
<!-- <property name="params" value="pageNum=start;pageSize=limit;"/> -->
</plugin>
</plugins>
</configuration>
3.spring 整合mybatis (加载mybatis 主配置文件)
<!-- Spring整合MyBatis框架 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:/mybatis/mybatis-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations">
<list>
<!-- classpath* 表示从依赖的项目打的jar包中进行扫描,加载配置,不加*表示从当前项目类路径进行加载配置文件。 -->
<value>classpath*:/mybatis/mapper-*.xml</value>
</list>
</property>
<property name="typeAliasesPackage" value="com.atguigu.collect.bean"></property>
</bean>
4.Controller 层实现方法
实例方法
//多条件查询的方法
@RequestMapping("getEmpInfoList")
@ResponseBody
public HashMap<String, Object> searchConditionsEmp(
String status,String orgId,
String id,String loginName,
@RequestParam("page") Integer pageNum,
@RequestParam("rows") Integer rows){
//防止 参数值为 空字符串 "" mybatis 中无法识别的囧境遇
Map<String,Object> map = new HashMap<String,Object>();
map.put("status", "".equals(status)?null:status);
map.put("orgId", "".equals(orgId)?null:orgId);
map.put("id", "".equals(id)?null:id);
map.put("loginName", "".equals(loginName)?null:loginName);
//分页所需的数据
map.put("rows", rows);
map.put("pageNum", pageNum);
//将 当前页码 每页显示条数 放入 到pageHelper 中
PageHelper.startPage(pageNum, rows);
//查询所记录的方法(此句 在mybaits 查询语句中不要在加入 limt pageHelper 会自动注入)
List<EmployeeInfo> empInfoList = employeeInfoService.getEmpInfoList(map);
//调用需要分页处理的 执行方法(会在SQL 语句中自动加入 limit )
PageInfo pageInfo = new PageInfo(empInfoList);
//获取总记录数
long total = pageInfo.getTotal();
//封装获取到的分页数据 封装成分页插件所需要的格式
HashMap<String, Object> jsonMap = new HashMap<String, Object>();
jsonMap.put("total", total);
jsonMap.put("rows", empInfoList);
return jsonMap;
}
5.前端easyui整合 分页
<!-- 列表部分 -->
<div id="tables">
<table id ="empInfodg" class="easyui-datagrid" style="width:100%;height:250px"
pagination='true' rownumbers='true'(加入两个属性 datagrid 会自动加入分页插件)
data-options="url:'getEmpInfoList.do',fitColumns:true,singleSelect:true">
<thead>
<tr>
<th data-options="field:'ck',checkbox:true"></th>
<th data-options="field:'status'">员工状态</th>
<th data-options="field:'id' ,width:60">账号</th>
<th data-options="field:'loginName',width:60">姓名</th>
<th data-options="field:'sex',width:60,align:'right'">性别</th>
<th data-options="field:'joinTime',width:60">入职时间</th>
<th data-options="field:'positionId',width:60">岗位</th>
<th data-options="field:'orgId',width:60,align:'right'">机构</th>
<th data-options="field:'createTime',width:60">录入时间</th>
<th data-options="field:'modifyTime',width:60">修改时间</th>
</tr>
</thead>
</table>
</div>