转载大神

https://blog.csdn.net/qq_33624284/article/details/72828977

  1. 把插件jar包导入项目(具体上篇有介绍http://blog.csdn.net/qq_33624284/article/details/72821811
  2. spring-mybatis.xml文件中配置
<!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations" value="classpath:com/wl/goldatg/mapping/*.xml"></property>
<!-- 配置分页插件 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageHelper">
<property name="properties">
<value>
dialect=mysql
reasonable=true
</value>
</property>
</bean>
</array>
</property>
</bean>

## 下面我们就可以使用 ##

  • 新建sql语句,返回list结果集
<select id="selectByList" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user_t
</select>
  • service层
public List<User> selectByList() {
return this.userDao.selectByList();
}
  • 1
  • 2
  • 3
  • Controller类
@RequestMapping("/userList")
public String userList(@RequestParam(required=true,defaultValue="1") Integer page,HttpServletRequest request,Model model){
//PageHelper.startPage(page, pageSize);这段代码表示,程序开始分页了,page默认值是1,pageSize默认是10,意思是从第1页开始,每页显示10条记录。
PageHelper.startPage(page, 3);
List<User> userList = userService.selectByList();
PageInfo<User> p=new PageInfo<User>(userList);
model.addAttribute("page", p);
model.addAttribute("userList",userList);
return "showUser";
}

PageInfo这个类是插件里的类,非常方便的调用,分页再次提高性能:

    //当前页
private int pageNum;
//每页的数量
private int pageSize;
//当前页的数量
private int size;
//排序
private String orderBy; //由于startRow和endRow不常用,这里说个具体的用法
//可以在页面中"显示startRow到endRow 共size条数据" //当前页面第一个元素在数据库中的行号
private int startRow;
//当前页面最后一个元素在数据库中的行号
private int endRow;
//总记录数
private long total;
//总页数
private int pages;
//结果集
private List<T> list; //第一页
private int firstPage;
//前一页
private int prePage;
//下一页
private int nextPage;
//最后一页
private int lastPage; //是否为第一页
private boolean isFirstPage = false;
//是否为最后一页
private boolean isLastPage = false;
//是否有前一页
private boolean hasPreviousPage = false;
//是否有下一页
private boolean hasNextPage = false;
//导航页码数
private int navigatePages;
//所有导航页号
private int[] navigatepageNums;

使用PageInfo这个类,你需要将查询出来的list放进去:

List<User> userList = userService.selectByList();
PageInfo<User> p=new PageInfo<User>(userList);
  • 1
  • 2

页面使用非常方便,直接贴代码:

  1. 控制层
model.addAttribute("page", p);
  • 1
  1. 前台页面
<body>
<center>
<table width="200" border="1">
<tr>
<th scope="col">序号</th>
<th scope="col">ID</th>
<th scope="col">姓名</th>
<th scope="col">密码</th>
<th scope="col">年龄</th>
</tr>
<c:forEach begin="0" step="1" items="${userList}" var="list" varStatus="userlist">
<tr>
<td>${userlist.count}</td>
<td>${list.id}</td>
<td>${list.userName}</td>
<td>${list.password}</td>
<td>${list.age}</td>
</tr>
</c:forEach>
</table>
<p>一共${page.pages}页</p>
<a href="userList?page=${page.firstPage}">第一页</a>
<a href="userList?page=${page.nextPage}">下一页</a>
<a href="userList?page=${page.prePage}">上一页</a>
<a href="userList?page=${page.lastPage}">最后页</a>
</center>
</body>

最后结果显示:(关键点${page.prePage}简单又快捷) 

mybatis pageHelper 分页插件使用的更多相关文章

  1. mybatis pagehelper分页插件使用

    使用过mybatis的人都知道,mybatis本身就很小且简单,sql写在xml里,统一管理和优化.缺点当然也有,比如我们使用过程中,要使用到分页,如果用最原始的方式的话,1.查询分页数据,2.获取分 ...

  2. 关于Spring+mybatis+PageHelper分页插件PageHelper的使用策略

    把插件jar包导入项目(具体上篇有介绍http://blog.csdn.net/qq_33624284/article/details/72821811) spring-mybatis.xml文件中配 ...

  3. mybatis PageHelper分页插件 和 LRU算法缓存读取数据

    分页: PageHelper的优点是,分页和Mapper.xml完全解耦.实现方式是以插件的形式,对Mybatis执行的流程进行了强化,添加了总数count和limit查询.属于物理分页. 一.首先注 ...

  4. Springboot 系列(十二)使用 Mybatis 集成 pagehelper 分页插件和 mapper 插件

    前言 在 Springboot 系列文章第十一篇里(使用 Mybatis(自动生成插件) 访问数据库),实验了 Springboot 结合 Mybatis 以及 Mybatis-generator 生 ...

  5. Mybatis的分页插件PageHelper

    Mybatis的分页插件PageHelper 项目地址:http://git.oschina.net/free/Mybatis_PageHelper  文档地址:http://git.oschina. ...

  6. SpringBoot集成MyBatis的分页插件 PageHelper

    首先说说MyBatis框架的PageHelper插件吧,它是一个非常好用的分页插件,通常我们的项目中如果集成了MyBatis的话,几乎都会用到它,因为分页的业务逻辑说复杂也不复杂,但是有插件我们何乐而 ...

  7. SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页

    SpringBoot+Mybatis配置Pagehelper分页插件实现自动分页 **SpringBoot+Mybatis使用Pagehelper分页插件自动分页,非常好用,不用在自己去计算和组装了. ...

  8. Mybatis之分页插件pagehelper的简单使用

    最近从家里回来之后一直在想着减肥的事情,一个月都没更新博客了,今天下午没睡午觉就想着把mybatis的分页插件了解一下,由于上个月重新恢复了系统,之前创建的项目都没了,又重新创建了一个项目. 一.创建 ...

  9. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

随机推荐

  1. hdu 6109 数据分割

    /** * 题目描述有点坑,勉强能读懂,大致意思,有多组约束条件.原本每组数据之间是有分界符号的 * 现在分界符号没了,让你找出原来每组数据多少个条件,并且告诉,每组的最后一个条件会使得与前面的 * ...

  2. hashlib加密

    一.hashlib的基本组成: 1.hashlib库是python3的标准库,主要用于数据的加密,以下是hashlib的方法及属性. >>> import hashlib>&g ...

  3. sed根据关键字注释crontab的计划任务

    [root@linux06 ~]# crontab -e*/5 * * * * /root/time_test.sh ----------------------------------------- ...

  4. ACM学习历程—Hihocoder 1177 顺子(模拟 && 排序 && gcd)(hihoCoder挑战赛12)

      时间限制:6000ms 单点时限:1000ms 内存限制:256MB   描述 你在赌场里玩梭哈,已经被发了4张牌,现在你想要知道发下一张牌后你得到顺子的概率是多少? 假定赌场使用的是一副牌,四种 ...

  5. ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)

    DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so carel ...

  6. JSP介绍(2)--- 九大隐式对象

    request对象 每当客户端请求一个JSP页面时,JSP引擎就会制造一个新的request对象来代表这个请求. request对象提供了一系列方法来获取HTTP头信息,cookies,HTTP方法等 ...

  7. Maven项目实战(1)

    一.maven的好处? 同样的项目使用maven工程来实现,它的项目源码很小: 1.依赖管理 就是对jar 包管理的过程 2.项目的一键构建 (编译-----测试----- 运行 --打包------ ...

  8. David Malan teaching CS75 lecture 9, Scalability

    https://youtu.be/-W9F__D3oY4 Storage PATA, SATA, SAS (15,000 rpm), SSD, RAID0 : striping, double thr ...

  9. Spring入门第十六课

    接上一次讲课 先看代码: package logan.spring.study.annotation.repository; public interface UserRepository { voi ...

  10. 洛谷P3382 【模板】三分法(三分找凹凸点)

    P3382 [模板]三分法 题目描述 如题,给出一个N次函数,保证在范围[l,r]内存在一点x,使得[l,x]上单调增,[x,r]上单调减.试求出x的值. 输入输出格式 输入格式: 第一行一次包含一个 ...