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内容如下

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

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

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" />

加载全局的配置文件

<property name="configLocation" value="classpath:mybatis-config.xml"></property>

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

<property name="mapperLocations" value="classpath:com/lyitong/mapping/*.xml"></property>

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

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

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

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">        <property name="dataSource" ref="dataSource" />        <!-- 加载全局的配置文件 -->        <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>        <!-- 配置mapper的扫描,找到所有的mapper.xml映射文件。 -->        <property name="mapperLocations" value="classpath:com/lyt/usermanage/mapper/*.xml"></property>        <!-- 配置类型别名 -->        <property name="typeAliasesPackage" value="classpath:com/lyt/usermanage/pojo/*"></property>    </bean>

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

<property name="typeAliasesPackage" value="classpath:com/lyt/usermanage/pojo/*"></property>

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

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

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

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="mapperLocations" value="classpath:com/lyitong/mapping/*.xml"></property><!-- pageHelper 分页插件 --><property name="plugins">   <array>     <bean class="com.github.pagehelper.PageHelper">       <property name="properties">         <value>           dialect=mysql           rowBoundsWithCount=true         </value>       </property>     </bean>   </array></property></bean>

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

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

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

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

这是如果接收参数为空字符串时它自身默认显示的页面和条数,这个可以自己规定 <br />以上就是pageHelper的简单应用,如有不足请留言一起探讨,谢谢!!!!

相关推荐