【Spring】SpringMVC Mybatis 结合 PageHelper 实现分页功能

一、引入pom依赖

<properties>
    <mybatis.version>3.2.8</mybatis.version>
    <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
    <pagehelper.version>5.0.0</pagehelper.version>
</properties>

<dependencies>
    <dependency>
        <groupId>com.github.miemiedev</groupId>
        <artifactId>mybatis-paginator</artifactId>
        <version>${mybatis.paginator.version}</version>
    </dependency>
    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper</artifactId>
        <version>${pagehelper.version}</version>
    </dependency>
</dependencies>

 

二、在springmvc配置文件中加入插件

sqlMapConfig.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>
    <!--配置开启自动匹配驼峰-->
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <!--配置PageHelper分页插件拦截器-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <!--            <property name="offsetAsPageNum" value="true"/>-->
            <!--            <property name="helperDialect" value="mysql"/>-->
            <property name="helperDialect" value="mysql"/>
            <!--            <property name="rowBoundsWithCount" value="true"/>-->
            <property name="reasonable" value="false"/>
        </plugin>
    </plugins>
</configuration>

 

三、在代码中使用

@Override
public EUDataGridResult getContentList(Long id, int page, int rows) {
    TbContentExample example = new TbContentExample();
    TbContentExample.Criteria criteria = example.createCriteria();
    criteria.andCategoryIdEqualTo(id);

    // 分页处理
    PageHelper.startPage(page, rows);
    // 查询分类列表
    List<TbContent> lists = tbContentMapper.selectByExample(example);
    
    // 创建返回值对象
    EUDataGridResult result = new EUDataGridResult();
    result.setRows(lists);

    // 取记录总数
    PageInfo pageInfo = new PageInfo(lists);
    result.setTotal(pageInfo.getTotal());
    result.setPages(pageInfo.getPages());

    return result;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值