淘淘商城06-mybatis分页插件PageHelper

PageHelper的官网 https://pagehelper.github.io/

如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件。该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页。

使用PageHelper我们不需要修改mapper.xml中的sql语句,直接使用PageHelper提供的api即可完成分页。

1.添加依赖

Mybatis 分页插件pagehelper不支持逆向工程Example条件查询,所以使用参考资料提供的pagehelper-fix

使用eclipse导入maven项目,并安装到本地仓库

在taotao-parent工程的pom.xml文件中修改pagehelper.version的版本号为

因为taotao项目所以jar包都依赖taotao-parent工程中pom.xml的版本

<pagehelper.version>3.4.2-fix</pagehelper.version>

如果不是逆向工程生成的代码,直接在dao层引入如下依赖即可

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

2.配置SqlMapConfig.xml

在Mybatis的全局文件中配置SqlMapConfig.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的API

3.1设置分页信息

//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);

//紧跟着的第一个select方法会被分页
List<Country> list = countryMapper.selectIf(1);
//第二个无法分页查询,正常查询
List<Country> list2 = countryMapper.selectIf(1);

3.2取出分页信息

使用pageInfo封装分页的信息,pageInfo是PageHelper插件提供的

//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectAll();
//用PageInfo对结果进行包装
PageInfo page = new PageInfo(list);
//测试PageInfo全部属性
//PageInfo包含了非常全面的分页属性
assertEquals(1, page.getPageNum());
assertEquals(10, page.getPageSize());
assertEquals(1, page.getStartRow());
assertEquals(10, page.getEndRow());
assertEquals(183, page.getTotal());
assertEquals(19, page.getPages());
assertEquals(1, page.getFirstPage());
assertEquals(8, page.getLastPage());
assertEquals(true, page.isFirstPage());
assertEquals(false, page.isLastPage());
assertEquals(false, page.isHasPreviousPage());
assertEquals(true, page.isHasNextPage());

4.编写测试类

我们在taotao-manager-service编写测试类,模拟service调用dao进行分页

package com.taotao.test.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 com.taotao.mapper.TbItemMapper;
import com.taotao.pojo.TbItem;
import com.taotao.pojo.TbItemExample;

public class TestPageHelper {
	
	@Test
	public void testhelper(){
		//初始化spring 容器
		ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
		//获取mapper的代理对象
		TbItemMapper itemMapper = context.getBean(TbItemMapper.class);
		
		//1.设置分页信息
		PageHelper.startPage(1, 3);//3行  紧跟着的第一个查询才会被分页
		//2.调用mapper的方法查询数据
		TbItemExample example = new TbItemExample();//设置查询条件使用
		List<TbItem> list = itemMapper.selectByExample(example);//select * from tb_item;
		List<TbItem> list2 = itemMapper.selectByExample(example);//select * from tb_item;
		
		//3.取分页信息
		PageInfo<TbItem> info = new PageInfo<>(list);
		
		System.out.println("第一个分页的list的集合长度"+list.size());
		System.out.println("第二个分页的list的集合长度"+list2.size());
		
		//4.遍历结果集  打印
		System.out.println("查询的总记录数数:"+info.getTotal());
		
		for (TbItem tbItem : list) {
			System.out.println(tbItem.getId()+"  "+tbItem.getTitle());
		}
	}
}

查询结果:

数据库中的结果:

猜你喜欢

转载自blog.csdn.net/pdsu161530247/article/details/81632713