一. 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. Servlet 笔记-Cookie 处理

    Cookie 是存储在客户端计算机上的文本文件,并保留了各种跟踪信息. 识别返回用户包括三个步骤: 服务器脚本向浏览器发送一组 Cookie.例如:姓名.年龄或识别号码等. 浏览器将这些信息存储在本地 ...

  2. Entity Framework Core 2.0 使用代码进行自动迁移

    一.前言 我们在使用EF进行开发的时候,肯定会遇到将迁移更新到生产数据库这个问题,前面写了一篇文章介绍了Entity Framework Core 2.0的入门使用,这里面介绍了使用命令生成迁移所需的 ...

  3. LeetCode 205. Isomorphic Strings (同构字符串)

    Given two strings s and t, determine if they are isomorphic. Two strings are isomorphic if the chara ...

  4. Spring Cloud官方文档中文版-Spring Cloud Config(下)-客户端等

    官方文档地址为:http://cloud.spring.io/spring-cloud-static/Dalston.SR2/#_serving_alternative_formats 文中例子我做了 ...

  5. RestServer 2.0 正式版发布

    RestServer 2.0 正式版发布 使用许可&版权说明 在保持本软件完整的情况下可以将本软件用于任何商业用途. 本软件可以自由传播,但是请保持软件相关文件和说明文档完整. 未经许可不得将 ...

  6. 使用bower init创建bower.json文件

    使用bower init 可以快速创建bower.json文件 bower init 回答一系列问题后就可以了,其中大部分问题可以按enter跳过.

  7. js判断值为null

    今天在做项目的时候,犯了一个着实不应该的错误,拿到data为null,然后判断如果为null执行A,否则执行B 我错误的代码是 if(data===null){ A; }else{ B; } 怎么调试 ...

  8. mysql开启慢查询日志以及查看(转载自网络)

    转载自http://database.51cto.com/art/201309/410314_1.htm

  9. Windows 安装 python2.7

    Windows 安装 python2.7 python2.7下载地址: https://www.python.org/downloads/release/python-2714/ 安装过程: 设置系统 ...

  10. .NET在VS2008中生成DLL并调用

    1.生成DLL 打开VS2008 - >新建->项目->类库->ClassLibrary1,在ClassLibrary1中会自动创建一个Class1类 class1中加入代码如 ...