当前位置: 首页 > news >正文

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

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;

}

相关文章:

  • SQLserver 数据库的索引,存储过程和触发器的使用与概念
  • 2018 年,去百度面试Java后端的一次面试经历
  • Python 随机数 random
  • Nginx源码包需要的翻译软件有哪些
  • CentOS Linux搭建SVN服务器
  • 提醒我喝水chrome插件开发指南
  • 进度二
  • mysql数据库主从复制
  • Python_OOP
  • 获取ip地址
  • ajax引用检测用户名是否存在
  • IDEA快捷键笔记
  • JSP第五篇【JSTL的介绍、core标签库、fn方法库、fmt标签库】
  • 用Metaclass实现一个精简的ORM框架
  • 看不到短期回报,首席财务官们表示对AI并不感冒
  • [rust! #004] [译] Rust 的内置 Traits, 使用场景, 方式, 和原因
  • 【MySQL经典案例分析】 Waiting for table metadata lock
  • 【划重点】MySQL技术内幕:InnoDB存储引擎
  • CAP 一致性协议及应用解析
  • extract-text-webpack-plugin用法
  • js如何打印object对象
  • Laravel核心解读--Facades
  • Nodejs和JavaWeb协助开发
  • Object.assign方法不能实现深复制
  • Redis 懒删除(lazy free)简史
  • RxJS: 简单入门
  • 案例分享〡三拾众筹持续交付开发流程支撑创新业务
  • 第13期 DApp 榜单 :来,吃我这波安利
  • 第十八天-企业应用架构模式-基本模式
  • 解析 Webpack中import、require、按需加载的执行过程
  • 那些被忽略的 JavaScript 数组方法细节
  • 前端性能优化——回流与重绘
  • 什么软件可以剪辑音乐?
  • 使用 Xcode 的 Target 区分开发和生产环境
  • 通过几道题目学习二叉搜索树
  • 突破自己的技术思维
  • 用quicker-worker.js轻松跑一个大数据遍历
  • 远离DoS攻击 Windows Server 2016发布DNS政策
  • FaaS 的简单实践
  • postgresql行列转换函数
  • ​Linux Ubuntu环境下使用docker构建spark运行环境(超级详细)
  • #[Composer学习笔记]Part1:安装composer并通过composer创建一个项目
  • #Linux(权限管理)
  • $.ajax()
  • (2015)JS ES6 必知的十个 特性
  • (Redis使用系列) Springboot 实现Redis 同数据源动态切换db 八
  • (安卓)跳转应用市场APP详情页的方式
  • (更新)A股上市公司华证ESG评级得分稳健性校验ESG得分年均值中位数(2009-2023年.12)
  • (六)c52学习之旅-独立按键
  • (三) prometheus + grafana + alertmanager 配置Redis监控
  • (五)Python 垃圾回收机制
  • (转)从零实现3D图像引擎:(8)参数化直线与3D平面函数库
  • .NET CORE Aws S3 使用
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .NET Remoting学习笔记(三)信道