PageHelper的使用

来源:互联网 发布:网络广告数据 编辑:程序博客网 时间:2024/06/05 12:15

PageHelper 的实现原理


PageHelper 的使用方法:

1.安装pagehelper 插件包 ,

下载地址:https://github.com/pagehelper/Mybatis-PageHelper/tree/master/src/main/java/com/github/pagehelper

2.在sqlMapConfig.xml中引入插件配置

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><!-- 配置分页插件 --><plugins><plugin interceptor="com.github.pagehelper.PageHelper"><!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->                <property name="dialect" value="mysql"/></plugin></plugins></configuration>

3.在查询的sql语句执行之前,添加一行代码:

PageHelper.startPage(1, 10);

第一个参数是page,要显示第几页。

第二个参数是rows,没页显示的记录数。

4.取查询结果的总数量。

创建一个PageInfo类的对象,从对象中取分页信息。

public class TestPageHelper {@Testpublic void testPageHelper() {//创建一个spring容器ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-*.xml");//从spring容器中获得Mapper的代理对象TbItemMapper mapper = applicationContext.getBean(TbItemMapper.class);//执行查询,并分页TbItemExample example = new TbItemExample();//分页处理PageHelper.startPage(2, 10);List<TbItem> list = mapper.selectByExample(example);//取商品列表for (TbItem tbItem : list) {System.out.println(tbItem.getTitle());}//取分页信息PageInfo<TbItem> pageInfo = new PageInfo<>(list);long total = pageInfo.getTotal();System.out.println("共有商品:"+ total);}}


 


 

 

 


原创粉丝点击