【SSM 5】Mybatis分页插件的使用
一、添加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,发现一下子就成功了。我想,肯定是少配置文件了!
【SSM 5】Mybatis分页插件的使用的更多相关文章
- SSM 使用 mybatis 分页插件 pagehepler 实现分页
使用分页插件的原因,简化了sql代码的写法,实现较好的物理分页,比写一段完整的分页sql代码,也能减少了误差性. Mybatis分页插件 demo 项目地址:https://gitee.com/fre ...
- Java SSM框架之MyBatis3(三)Mybatis分页插件PageHelper
引言 对于使用Mybatis时,最头痛的就是写分页,需要先写一个查询count的select语句,然后再写一个真正分页查询的语句,当查询条件多了之后,会发现真不想花双倍的时间写count和select ...
- Mybatis学习---Mybatis分页插件 - PageHelper
1. Mybatis分页插件 - PageHelper说明 如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件. 该插件目前支持Oracle,Mysql,MariaDB,S ...
- MyBatis学习总结(17)——Mybatis分页插件PageHelper
如果你也在用Mybatis,建议尝试该分页插件,这一定是最方便使用的分页插件. 分页插件支持任何复杂的单表.多表分页,部分特殊情况请看重要提示. 想要使用分页插件?请看如何使用分页插件. 物理分页 该 ...
- Mybatis分页插件PageHelper的学习与使用
目录 中文教程 PageHelper使用 后端程序员都知道,在Web系统中,分页是一种常见的功能,我之前写的分页方法都比较麻烦,移植性也不高,这就很不乐观了.作为一个积极开朗的程序员,怎么能不去了解P ...
- 品优购商城项目(二)mybatis分页插件
品优购商城项目第二天,使用mybatis分页插件实现分页.主要实现的是 SSM整合mybatis分页. 一.引用mybatis分页插件 SqlMapConfig.xml <?xml versio ...
- Mybatis分页插件
mybatis配置 <!-- mybatis分页插件 --> <bean id="pagehelper" class="com.github.pageh ...
- mybatis分页插件以及懒加载
1. 延迟加载 延迟加载的意义在于,虽然是关联查询,但不是及时将关联的数据查询出来,而且在需要的时候进行查询. 开启延迟加载: <setting name="lazyLoading ...
- Mybatis分页插件PageHelper的配置和使用方法
Mybatis分页插件PageHelper的配置和使用方法 前言 在web开发过程中涉及到表格时,例如dataTable,就会产生分页的需求,通常我们将分页方式分为两种:前端分页和后端分页. 前端分 ...
- Mybatis分页插件PageHelper使用
一. Mybatis分页插件PageHelper使用 1.不使用插件如何分页: 使用mybatis实现: 1)接口: List<Student> selectStudent(Map< ...
随机推荐
- Get Jenkins job build queue length
Jenkins API doesn’t provide the job build queue length. Hence, it seems we have to parse the html to ...
- circular_buffer
编程有时需要使用定长的容器(fixed size container).实现旋转容器可以像下面这样: std::vector<T> vec(size); vec[i % size] = n ...
- 转:Android开发:使用JNI读取应用签名
博文转自http://www.tuicool.com/articles/UVjme2r,感谢博主的分享 为了防止被反编译,打算把关键代码写到so里(比如加解密),在so里加上判断APk包签名是否一致的 ...
- 转载:bootstrap, boosting, bagging 几种方法的联系
转:http://blog.csdn.net/jlei_apple/article/details/8168856 这两天在看关于boosting算法时,看到一篇不错的文章讲bootstrap, ja ...
- 分析案例:应用服务无响应,任务管理器中发现大量w3wp僵尸进程----等待异构系统WebService返回值
问题描述: 某二次开发的项目反馈,不定期出现应用服务器无响应的情况,登录服务器发现任务管理器中有大量的w3wp僵尸进程. 分析过程: 针对同一进程每隔15秒抓取dump,连续抓取3个,对比 ...
- Jeesite的cahche工具类
本CacheUtils主要是基于shiro的cache进行处理. 其他选择: 类似的我们可以选择java cache ,spring cahche等方案. 再进一步 ...
- PHP面向对象基础part.1
- SQLServer2005删除log文件和清空日志的方案
数据库在使用过程中会使日志文件不断增加,使得数据库的性能下降,并且占用大量的磁盘空间.SQL Server数据库都有log文件,log文件记录用户对数据库修改的操作.可以通过直接删除log文件和清空日 ...
- mySql事务_ _Java中怎样实现批量删除操作(Java对数据库进行事务处理)?
本文是记录Java中实现批量删除操作(Java对数据库进行事务处理),在开始之前先来看下面这样的一个页面图: 上面这张图片显示的是从数据库中查询出的出租信息,信息中进行了分页处理,然后每行的前面提 ...
- javascript网址收集
1.模块的写法http://www.ruanyifeng.com/blog/2012/10/javascript_module.html 2.模块规范 AMDhttp://www.ruanyifeng ...