mybatis分页插件pageHelper实例详解

分页插件pagehelper也是一个很重要的插件,本文主要和大家介绍mybatis分页插件pagehelper详解及简单实例的相关资料,需要的朋友可以参考下,希望能帮助到大家。

mybatis分页插件pageHelper详解及简单实例

工作的框架spring springmvc mybatis3

首先使用分页插件必须先引入maven依赖,在pom.xml中添加如下

<!-- 分页助手 -->  <dependency><groupid>com.github.pagehelper</groupid><artifactid>pagehelper</artifactid><version>3.7.5</version></dependency>

其次需要在配置文件中添加配置,有两种方式

1,新建mybatis-config.xml内容如下

<?xml  version="1.0" encoding="UTF-8"?>nbsp;configuration   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"   "http://mybatis.org/dtd/mybatis-3-config.dtd"&gt;     <configuration>   <!-- 分页助手 -->   <plugins>    <!-- com.github.pagehelper为PageHelper类所在包名 -->    <plugin>    <!-- 数据库方言 -->      <property></property>      <!-- 设置为true时,使用RowBounds分页会进行count查询 会去查询出总数 -->      <property></property>    </plugin></plugins>   </configuration>

在spring-mybatis.xml中添加一个bean属性

<bean><property></property></bean>

加载全局的配置文件

<property></property>

配置mapper的扫描,找到所有的mapper.xml映射文件。

<property></property>

备注:如果你的mybatis-config.xml配置文件开启了如下别名配置:

<typealiases>      <!-- javabean 的首字母小写的非限定类名来作为它的别名(其实别名是不去分大小写的)。也可在javabean 加上注解@Alias 来自定义别名, 例如: @Alias(student) -->      <package></package>    </typealiases>

那么你的spring和mybatis整合文件就得加上相应的属性,否则会造成mybatis配置文件加载不成功报异常,如下:

       <property></property>            <property></property>            <property></property>            <property></property>    

相比于上面的配置我们这里多了一步

    <property></property>

配置的时候要注意mybatis配置文件和spring-mybatis整合文件的属性要统一。

2.如上操作配置完成,下面第二种方法

直接在spring-mybatis.xml中配置如下属性

<property></property><property></property>                              dialect=mysql        rowBoundsWithCount=true                      

配置文件加载好之后,就可以直接使用,具体使用代码如下:

PageHelper.startPage(Integer.parseInt(currentPage), Integer.parseInt(pageSize));    List<lytbbstz> publishTz = bbsTzDao.getPublishTz(userId);    PageInfo<lytbbstz> info = new PageInfo<lytbbstz>(publishTz);    map.put("status", 1);    map.put("tzList", info.getList());    return map;</lytbbstz></lytbbstz></lytbbstz>

前台需要传入的参数是当前页和页面显示数目,当然页面显示数目也可以后台规定,一般在接收参数时最好加上默认配置如下:

@RequestParam(defaultValue="1",value="currentPage")String currentPage, @RequestParam(defaultValue="10",value="pageSize")String pageSize

这是如果接收参数为空字符串时它自身默认显示的页面和条数,这个可以自己规定

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享