一、添加maven依赖项
<span style="font-family:KaiTi_GB2312;font-size:18px;"><dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
</dependency></span>
版本号:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><pagehelper.version>3.4.2-fix</pagehelper.version>
<mybatis.paginator.version>1.2.15</mybatis.paginator.version></span>
二、Mybatis配置文件(SqlMapConfig.xml)增加分页插件
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis3 config.dtd">
<configuration>
<!-- 配置分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型Oracle,MySQL,MarinDBName,SQLite,PostareSQL六种数据库 -->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration></span>
三、spring整合
<span style="font-family:KaiTi_GB2312;font-size:18px;"> </bean>
<!-- 让spring管理sqlsessionfactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
</bean>
<!-- 配置扫描包,加载mapper代理对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="Angel.mapper" />
</bean></span>
四、service分页实现
<span style="font-family:KaiTi_GB2312;font-size:18px;">@Override
public EUDataGridResult selectAll(int pageNum,int pageSize) {
//设置分页的参数
PageHelper.startPage(pageNum, pageSize);
//查询数据
List<TbUser> list=userMapper.selectAll();
//创建一个返回值对象
EUDataGridResult result=new EUDataGridResult();
result.setRows(list);
//取记录总条数
PageInfo<TbUser> pageInfo=new PageInfo<>(list);
result.setTotal(pageInfo.getTotal());
return result;
}</span>
附:EUDataGridResult 类
<span style="font-family:KaiTi_GB2312;font-size:18px;">package Angel.pojo;
import java.util.List;
public class EUDataGridResult {
private long total;
private List<?> rows;
public long getTotal() {
return total;
}
public void setTotal(long total) {
this.total = total;
}
public List<?> getRows() {
return rows;
}
public void setRows(List<?> rows) {
this.rows = rows;
}
}</span>
五、controller实现
<span style="font-family:KaiTi_GB2312;font-size:18px;">@RequestMapping("/user/findAll")
@ResponseBody
public EUDataGridResult getItemList(@RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="10")Integer rows,HttpServletResponse response) throws IOException{
EUDataGridResult result=userService.selectAll(page, rows);
return result;
}</span>
六、JSP页面
<span style="font-family:KaiTi_GB2312;font-size:18px;"><%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.4.1/themes/default/easyui.css" />
<link rel="stylesheet" type="text/css" href="js/jquery-easyui-1.4.1/themes/icon.css" />
<script type="text/javascript" src="js/jquery-easyui-1.4.1/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery-easyui-1.4.1/jquery.easyui.min.js"></script>
<script type="text/javascript" src="js/jquery-easyui-1.4.1/locale/easyui-lang-zh_CN.js"></script>
</head>
<body>
<table class="easyui-datagrid" id="itemList" title="用户列表"
data-options="url:'<%=basePath %>user/findAll',pageSize:10,pagination:true">
<thead>
<tr>
<th data-options="field:'id',width:70">ID</th>
<th data-options="field:'username',width:60">名称</th>
<th data-options="field:'phone',width:70">电话</th>
<th data-options="field:'email',width:70">邮箱</th>
<th data-options="field:'created',width:130,align:'center'">创建日期</th>
<th data-options="field:'updated',width:130,align:'center'">更新日期</th>
</tr>
</thead>
</table>
</body>
</html></span>
七、总结
在最开始的时候,想自己写一个分页实现,但是,后来就发现有Mybatis已经封装好的分页插件,就简单的配置一下就可以使用了。到现在还是感觉,自己对已有资源的使用还不够,每次都闹着自己创新创造,但是,先向别人学习,这一步也很重要。
然后在实现的时候,先是和王高高弄这个分页插件,不知道为什么,她那儿弄了好久都没有成功,后来自己回来写demon,发现一下子就成功了。我想,肯定是少配置文件了!