如何使用Mybatis第三方插件--PageHelper实现分页操作

本文介绍如何在Mybatis项目中使用PageHelper插件实现分页功能。主要内容包括添加PageHelper依赖、配置Mybatis拦截器、在项目和服务层使用PageHelper等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.概述

最近在做宜立方商城项目时,后台管理系统要求实现分页显示,由于项目使用了Mybatis逆向生成映射文件,所以在此使用了mybatis第三方插件--PageHelper来实现分页这一功能,下面就如何在项目使用这一插件进行说明。


2.使用方法

  1. 添加依赖
    把PageHelper依赖的jar包添加到工程中。官方提供的代码对逆向工程支持的不好,使用参考资料中的pagehelper-fix。首先将下列链接中的pagehelper-fix的maven工程导入myeclipse中,点击run 选择maven install,这步操作便可以把pagehelper-fix安装到本地仓库,从而可以将其当做一个jar包来使用。

pagehelper-fix下载链接:链接:https://pan.baidu.com/s/1kXb1OF1 密码:tgk5

2.修改mybatis配置文件

在Mybatis配置xml中配置拦截器插件:
   <plugins>
         <!-- com.github.pagehelper为PageHelper类所在包名 -->
         <plugin interceptor="com.github.pagehelper.PageHelper">
             <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->        
              <property name="dialect" value="mysql"/>
         </plugin>
    </plugins>
    
    

3. 如何在项目中使用PageHelper

import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;   
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; 
import cn.e3mall.mapper.TbItemMapper;
import cn.e3mall.pojo.TbItem;
import cn.e3mall.pojo.TbItemExample;  
/**
 * @author 熊涛
 *分页测试用例
 */
public class PageHelperTest {

    @Test
    public void testPageHelper() throws Exception
    {
        //初始化spring容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");
        //获得Mapper的代理对象
        TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);
        //执行sql语句前设置分页信息使用PageHelper的startPage方法
        PageHelper.startPage(1,30);
        //执行查询
        TbItemExample example = new TbItemExample();
        List<TbItem> list = itemMapper.selectByExample(example);
        //取分页信息,PageInfo:1.总记录数   2.总页数  3.当前页码
        PageInfo<TbItem> pageInfo = new PageInfo<>(list);
        System.out.println(pageInfo.getTotal());
        System.out.println(pageInfo.getPages());
        System.out.println(pageInfo.getPageNum());
        System.out.println(pageInfo.getPageSize());

    }
}

4. 在服务层使用PageHelper

@Override
    public EasyUIDataGridResult getItemList(int page, int rows) {
        //设置分页信息
                PageHelper.startPage(page, rows);
                //执行查询
                TbItemExample example = new TbItemExample();
                List<TbItem> list = itemMapper.selectByExample(example);
                //取分页信息
                PageInfo<TbItem> pageInfo = new PageInfo<>(list);
                //创建返回结果对象
                EasyUIDataGridResult result = new EasyUIDataGridResult();
                result.setTotal(pageInfo.getTotal());
                result.setRows(list);
                
                return result;
    }
    
    

5. 在控制层使用service

@RequestMapping("/item/list")
@ResponseBody
public EasyUIDataGridResult getItemList(Integer page, Integer rows) {

EasyUIDataGridResult result = itemService.getItemList(page, rows);
return result;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值