一、添加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分页插件的使用的更多相关文章

  1. SSM 使用 mybatis 分页插件 pagehepler 实现分页

    使用分页插件的原因,简化了sql代码的写法,实现较好的物理分页,比写一段完整的分页sql代码,也能减少了误差性. Mybatis分页插件 demo 项目地址:https://gitee.com/fre ...

  2. Java SSM框架之MyBatis3(三)Mybatis分页插件PageHelper

    引言 对于使用Mybatis时,最头痛的就是写分页,需要先写一个查询count的select语句,然后再写一个真正分页查询的语句,当查询条件多了之后,会发现真不想花双倍的时间写count和select ...

  3. Mybatis学习---Mybatis分页插件 - PageHelper

    1. Mybatis分页插件 - PageHelper说明 如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件. 该插件目前支持Oracle,Mysql,MariaDB,S ...

  4. MyBatis学习总结(17)——Mybatis分页插件PageHelper

    如果你也在用Mybatis,建议尝试该分页插件,这一定是最方便使用的分页插件. 分页插件支持任何复杂的单表.多表分页,部分特殊情况请看重要提示. 想要使用分页插件?请看如何使用分页插件. 物理分页 该 ...

  5. Mybatis分页插件PageHelper的学习与使用

    目录 中文教程 PageHelper使用 后端程序员都知道,在Web系统中,分页是一种常见的功能,我之前写的分页方法都比较麻烦,移植性也不高,这就很不乐观了.作为一个积极开朗的程序员,怎么能不去了解P ...

  6. 品优购商城项目(二)mybatis分页插件

    品优购商城项目第二天,使用mybatis分页插件实现分页.主要实现的是 SSM整合mybatis分页. 一.引用mybatis分页插件 SqlMapConfig.xml <?xml versio ...

  7. Mybatis分页插件

    mybatis配置 <!-- mybatis分页插件 --> <bean id="pagehelper" class="com.github.pageh ...

  8. mybatis分页插件以及懒加载

    1.   延迟加载 延迟加载的意义在于,虽然是关联查询,但不是及时将关联的数据查询出来,而且在需要的时候进行查询. 开启延迟加载: <setting name="lazyLoading ...

  9. Mybatis分页插件PageHelper的配置和使用方法

     Mybatis分页插件PageHelper的配置和使用方法 前言 在web开发过程中涉及到表格时,例如dataTable,就会产生分页的需求,通常我们将分页方式分为两种:前端分页和后端分页. 前端分 ...

  10. Mybatis分页插件PageHelper使用

    一. Mybatis分页插件PageHelper使用  1.不使用插件如何分页: 使用mybatis实现: 1)接口: List<Student> selectStudent(Map< ...

随机推荐

  1. python之环境搭建windows版

    1.先到python官网下载属于自己的的python版本,有linux版,有mac版,有windows版:https://www.python.org/downloads/windows/ 2.下载完 ...

  2. 初学Laravel

    之前一直用开tp和ot,本来觉得学会一个tp便可走遍天下,tp的确强大.但后来听到很多同行的同学说他们的公司都开始转型往lv走了,我的同学没有学过lv,然而公司给足时间去让他们去学.当然,缺人可能是占 ...

  3. Python之路,day7-Python基础

    os.system 输出命令结果到屏幕,返回命令执行状态os.popen("dir").read()#会保存命令的执行结果输出py2.7commandscommands.getst ...

  4. PowerBuilder 简介及应用 - 数据库系统原理

    PowerBuilder 是一种企业级数据库前端应用和多层体系结构开发工具,友好的用户界面,功能强大的数据窗口,是一个集成开发环境. PB 的特点 采用面向对象的编程方法和事件驱动的工作原理. 支持跨 ...

  5. oracle数据库从入门到精通

    oracle产品线围绕企业开发平台的企业开发平台四大组件:unix,weblogic中间件,java编程语言,oracle数据库oracle 开发主要分两类数据库管理:dba数据库编程:分两部分   ...

  6. 一个简易的反射类库NMSReflector

    转自:http://blog.csdn.net/lanx_fly/article/details/53914338 背景简介 以前看过一些代码,是简单的读取SqlReader然后赋值给Model,我不 ...

  7. Elasticsearch【JAVA REST Client】客户端操作

    ES系统作为集群,环境搭建非常方便简单. 现在在我们的应用中,如何对这个集群进行操作呢? 我们利用ES系统,通常都是下面的架构: 在这里,客户端的请求通过LB进行负载均衡,因为操作任何一个ES的实例, ...

  8. css3 em rem等单位的区别

    px:绝对单位,页面按精确像素展示 em:相对单位,基准点为父节点字体的大小,如果自身定义了font-size按自身来计算(浏览器默认字体是16px),整个页面内1em不是一个固定的值. rem:相对 ...

  9. springmvc--json--返回json的日期格式问题

    (一)输出json数据 springmvc中使用jackson-mapper-asl即可进行json输出,在配置上有几点: 1.使用mvc:annotation-driven 2.在依赖管理中添加ja ...

  10. MVC4 +EasyUI Tabs 使用

    Tabs 右键菜单功能实现 前端 <div id="tabs" class="easyui-tabs" fit="true" bord ...