mybatis分页插件pageHelper的使用

来源:互联网 发布:局域网控制桌面软件 编辑:程序博客网 时间:2024/06/05 21:16

在使用mybatis时,无法进行自动分页,因此采用pageHelper插件进行分页处理。

步骤如下:

1、配置dbConfig.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>    <!--翻页拦截器-->    <plugins>            <plugin interceptor="com.github.pagehelper.PageInterceptor" >           <!-- 方言设置,如果不设置,则默认为 postgresql;支持postgresql、oracle、mysql-->           <property name="helperDialect" value="postgresql"/>         </plugin>     </plugins>     </configuration>


2、在spring中配置引入dbConfig.xml

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource" ref="dataSource" /><property name="mapperLocations" value="classpath*:xxx/*Mapper.xml"/><property name="configLocation" value="classpath:dbConfig.xml"/></bean>


3、在service中设置分页参数

public PageInfo<Role> getAll() {    PageHelper.startPage(2, 3);    List<Role> list = roleMapper.getRoleList(new Role());    PageInfo<Role> page = new PageInfo<>(list);    return page;}


4、单元测试

@Testpublic void testGetAll() {   PageInfo<Role> page = userRoleService.getAll();   List<Role> list = page.getList();   System.out.println(list.size());}

其中pageInfo中包含了分页信息。

在此记录,以备查询。

 
原创粉丝点击