mabatis的分页,使用第三方的pageHelper

4 篇文章 0 订阅

环境:maven+SSM 2018年几乎最新

1、pom添加依赖

<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.4</version>
</dependency>

在这里尤其注意的是一定要在用到pageHelper项目的pom.xml文件里面都要添加这个依赖

否则在执行分页查询的时候出现警告

警告: Hessian/Burlap: 'com.github.pagehelper.Page' is an unknown class in WebappClassLoader

  context:

  delegate: false

  repositories:

----------> Parent Classloader:

ClassRealm[plugin>org.apache.tomcat.maven:tomcat7-maven-plugin:2.2, parent: sun.misc.Launcher$AppClassLoader@55f96302]


参考别人的博客地址:https://blog.csdn.net/qq_38765404/article/details/78108780


2、在sqlMapConfig.xml文件里,每个人的文件名可能不一样,凑活看,记得灵活变动

<plugins>
	<plugin interceptor="com.github.pagehelper.PageInterceptor" />
</plugins>

是不是没有看到配置方言的property?因为在pageHelper 5X后就自动识别数据库了

3、java测试类

package com.shun.test;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shun.mapper.TbItemMapper;
import com.shun.pojo.TbItem;
import com.shun.pojo.TbItemExample;

/**
* @author czs
* @version 创建时间:2018年5月10日 上午9:45:55 
*/
public class PageHelperTest {
	public static void main(String[] args) {
		// 获取spring容器
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
				
		// 从容器中获得mapper代理对象
		TbItemMapper itemMapper = applicationContext.getBean(TbItemMapper.class);
		
		// 在执行查询之前获得设置分页的条件
		PageHelper.startPage(1, 20);
		
		// 执行查询
		List<TbItem> items = itemMapper.selectByExample(new TbItemExample());
		
		// 获取分页信息PageInfo  总记录数  总页数  当前页码等待
		PageInfo<TbItem> pageInfo = new PageInfo<TbItem>(items);
		
		System.out.println("总记录数:"+pageInfo.getTotal());
		System.out.println("总页数:"+pageInfo.getPages());
		System.out.println("当前页码:"+pageInfo.getPageNum());
	}
}

4、运行测试类结果



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值