一. Mybatis分页插件PageHelper使用

 1、不使用插件如何分页:

使用mybatis实现:

1)接口:

List<Student> selectStudent(Map<String, Object> map);

2)mapper.xml:

<select id="selectStudent" resultMap="BaseResultMap" parameterType="java.util.Map" >
select
<include refid="Base_Column_List" />
from student limit #{pageNum},#{pageSize}
</select>

3)测试:

@Test
public void TestGetStudent() throws IOException {
try {
StudentMapper mapper=session.getMapper(StudentMapper.class);
Map<String,Object> map=new HashMap<String,Object>();
map.put("pageNum", 0);
map.put("pageSize", 3);
List<Student> students=mapper.selectStudent(map); for(Student student :students)
System.out.println(student.gettId() + " " + student.gettName() + " "+student.gettAge()+" "+student.gettEnterdate()+" "+student.gettSid()); }finally {
session.close();
} }

2 使用PageHelper插件如何分页:

下载地址:

https://github.com/pagehelper/Mybatis-PageHelper

https://github.com/JSQLParser/JSqlParser

另外一个地址:

Pagehelper下载地址:

http://repo1.maven.org/maven2/com/github/pagehelper/pagehelper/

jsqlparser 下载地址:

http://repo1.maven.org/maven2/com/github/jsqlparser/jsqlparser/

使用步骤:

1、导入相关包pagehelper-x.x.x.jar 和  jsqlparser-x.x.x.jar。

2、在MyBatis全局配置文件中配置分页插件。

   <plugins>

<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>

</plugins>

3、使用PageHelper提供的方法进行分页

  StudentMapper mapper = session.getMapper(StudentMapper.class);
//放在查询之前
Page<Object> page = PageHelper.startPage(1, 3); StudentExample example=null;
List<Student> students=mapper.selectByExample(example); for(Student student :students)
System.out.println(student.gettId() + " " + student.gettName() + " "+student.gettAge()+" "+student.gettEnterdate()+" "+student.gettSid());
System.out.println("当前页码:"+page.getPageNum());
System.out.println("总记录数:"+page.getTotal());
System.out.println("每页的记录数:"+page.getPageSize());
System.out.println("总页码:"+page.getPages());

4、可以使用更强大的PageInfo封装返回结果

           StudentMapper mapper = session.getMapper(StudentMapper.class);
Page<Object> page = PageHelper.startPage(1, 3); StudentExample example=null;
List<Student> students=mapper.selectByExample(example);
for(Student student :students)
System.out.println(student.gettId() + " " + student.gettName() + " "+student.gettAge()+" "+student.gettEnterdate()+" "+student.gettSid());
PageInfo<Student> info = new PageInfo<Student>(students, 3);
System.out.println("当前页码:"+info.getPageNum());
System.out.println("总记录数:"+info.getTotal());
System.out.println("每页的记录数:"+info.getPageSize());
System.out.println("总页码:"+info.getPages());
System.out.println("是否第一页:"+info.isIsFirstPage());
System.out.println("连续显示的页码:");
int[] nums = info.getNavigatepageNums();
for (int i = 0; i < nums.length; i++) {
System.out.println(nums[i]);
}

Mybatis分页插件PageHelper使用的更多相关文章

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

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

  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分页插件PageHelper的实现

    Mybatis分页插件PageHelper的实现 前言 分页这个概念在做web网站的时候很多都会碰到 说它简单吧 其实也简单 小型的网站,完全可以自己写一个,首先查出数据库总条数,然后按照分页大小分为 ...

  5. 基于Mybatis分页插件PageHelper

    基于Mybatis分页插件PageHelper 1.分页插件使用 1.POM依赖 PageHelper的依赖如下.需要新的版本可以去maven上自行选择 <!-- PageHelper 插件分页 ...

  6. Mybatis分页插件-PageHelper的使用

    转载:http://blog.csdn.net/u012728960/article/details/50791343 Mybatis分页插件-PageHelper的使用 怎样配置mybatis这里就 ...

  7. (转)淘淘商城系列——MyBatis分页插件(PageHelper)的使用以及商品列表展示

    http://blog.csdn.net/yerenyuan_pku/article/details/72774381 上文我们实现了展示后台页面的功能,而本文我们实现的主要功能是展示商品列表,大家要 ...

  8. springmvc mybatis 分页插件 pagehelper

    springmvc mybatis 分页插件 pagehelper 下载地址:pagehelper 4.2.1 , jsqlparser 0.9.5 https://github.com/pagehe ...

  9. MyBatis 分页插件PageHelper 后台报错

    今天遇到一个问题,使用MyBatis 分页插件PageHelper 进行排序分页后,能正常返回正确的结果,但后台却一直在报错 net.sf.jsqlparser.parser.ParseExcepti ...

随机推荐

  1. java加密解密

    java加密解密 public class MD5Util { /** * @param args */ public static void main(String[] args) { System ...

  2. iOS之 NSTimer(二)

    1. Stopping a Timer  关闭定时器 if you create a non-repeating timer, there is no need to take any further ...

  3. JS实现移动端购物车左滑删除功能

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...

  4. 两个HTML地址栏传中文参数乱码

    这个不叫乱码,我非专业.这个是url编码,js本身就是读取url编码的.对于js获取url的中文你可以尝试用escape() encodeURI() encodeURIComponent() deco ...

  5. SQLAlchemy复杂查询

    最近个人用python + flask搞了一个小项目,ORM用到的是SQLAlchemy.   SQLAlchemy的查询方式非常灵活,你所能想像到的复杂SQL 语句,基本上都可以实现.这里简单的总结 ...

  6. 五种js判断是否为整数(转)

    五种js判断是否为整数类型方式 作者:snandy 这篇文章主要介绍了五种JavaScript判断是否为整数类型方式,需要的朋友可以参考下   这篇看看如何判断为整数类型(Integer),JavaS ...

  7. Python 字典和json的本质区别(个人理解)

    个人理解:字典和json显示的时候差不多,但是数据类型不同(如下图): 字典的类型是字典dict json的类型是字符串str 接口测试是传参数payload时有时候是传的字符串,应该将payload ...

  8. Spring IOC容器分析(2) -- BeanDefinition

    上文对Spring IOC容器的核心BeanFactory接口分析发现:在默认Bean工厂DefaultListableBeanFactory中对象不是以Object形成存储,而是以BeanDefin ...

  9. 通过geotools读写shp文件

    依赖jar包 读取shp public static void main(String[] path) { DbaseFileReader reader = null; try { reader = ...

  10. Anaconda快捷搭建Python2和Python3环境

    我们在使用Pycharm编辑Python程序经常会因为不熟悉Python2和Python3的一些代码区别而导致错误,我们知道他们之间很多代码是必须运行在对应版本中的,否则是会报错的.因此,本文介绍一个 ...