PageHelper的使用

1 修改 sqlMapConfig.xmljava

<!-- 配置pageHelp分页插件 -->
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageHelper">
			<!-- 设置数据库方言oracle,mysql,mariadb,sqlite,hsqldb,postgresql -->
			<property name="dialect" value="mysql"/>
		</plugin>
	</plugins>

2 编写测试代码mysql

package com.shi.page;

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.shi.entity.TbItem;
import com.shi.mapper.TbItemMapper;

/**
 * 
 * @author: SHF
 * @date: 2017年11月30日 上午11:43:27
 * @Description: 分页插件PageHelper 的测试
 */
public class PageHelperTest {
	
	@Test
	public void testPageHelper() throws Exception{
		
		//初始化spring容器
		ApplicationContext applicationContext=new  ClassPathXmlApplicationContext("classpath*:spring/applicationContext-dao.xml");
		//从容器中获取咱们的代理对象
		TbItemMapper tbItemMapper=applicationContext.getBean(TbItemMapper.class);
		
		//1 执行sql语句以前要先摄者分页信息,使用pageHelperde startPage方法
		PageHelper.startPage(1, 10);
		//2 执行查询
		List<TbItem> list=(List<TbItem>) tbItemMapper.selectAllByPage();
		//3 取分页信息,pageInfo, 1.总记录数,2.总页码,3当前页码,4.当前页的全部信息
		PageInfo<TbItem> pageInfo=new PageInfo<>(list);
		System.out.println("共有多少条记录"+pageInfo.getTotal());
		System.out.println("共有多少页"+pageInfo.getPages());
		System.out.println("当前记录数"+list.size());
	}
}

必须使用该jar包git

<dependency>
	<groupId>com.github.pagehelper</groupId>
	<artifactId>pagehelper</artifactId>
	 <version>3.4.2-fix</version>
</dependency>
相关文章
相关标签/搜索