pageHelper5 结合mybatis实现分页

2018-11-15  本文已影响0人  众生皆苦_3547

1. 引入pageHelper依赖

          <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>5.1.8</version>
            </dependency>

2. mybatis配置sqlMapConfig.xml文件中配置添加pageHelper的plugin

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

pageHelper 5以下的版本可能需要配置以下类(5中的Interceptor接口实现类在com.github.pagehelper.PageInterceptor中,之前版本是com.github.pagehelper.PageHelper就实现了Interceptor)

<configuration>
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>
</configuration>

3. 代码实现分页功能

//获取第1页,10条内容,紧跟的第一个查询才会被分页
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectAll();
//用PageInfo对结果进行包装
PageInfo page = new PageInfo(list);
上一篇下一篇

猜你喜欢

热点阅读