ssm框架中pagehelper的用法

博主使用PageHelper进行信息分页时遇到问题,现将问题及解决办法总结。介绍了使用步骤,包括在pom中导入依赖、配置mybatis配置文件、在spring配置文件中加载mybatis配置文件、编写测试方法,还指出PageHelper.startPage只对其后第一条查询语句分页。

今天在使用pagehelper将信息进行分页的时候遇到了很多的问题,始终不能分页成功,现在将问题以及解决办法总结。

使用步骤

1、在pom中导入依赖

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

2、配置mybatis的配置文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
		PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
		"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 引入分页插件 -->
  <!-- 要求3.3版本以上的mybatis -->
	<plugins>
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
      <!-- 分页参数合理化,即不可能到达-1页之类不合理的页数 -->
      <property name="reasonable" value="true"/>
    </plugin>
  </plugins>
</configuration>

3、不要忘记在你的spring的配置文件中加载mybatis 的配置文件

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource"></property>
       <!-- 加载mybatis的全局配置文件 -->
		<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml" />
   </bean>

4、编写测试方法

PageHelper.startPage(start,rows);只对他之后的第一条查询语句进行分页

@Test
	public void testPageHelper() throws Exception {
		//初始化spring容器
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
		//从容器中获得Mapper代理对象
		BArticlesMapper bArticlesMapper = (BArticlesMapper) applicationContext.getBean(BArticlesMapper.class);	
		
		//执行sql语句之前设置分页信息使用PageHelper的startPage方法。	
		
		//设置现在的页数为2,显示的条数为5条
		PageHelper.startPage(2, 5);
		List<BArticles> list = bArticlesMapper.selectByExampleWithBLOBs(null);
		System.out.println(list.size());
		//取分页信息,PageInfo。
		   PageInfo<BArticles> pageInfo = new PageInfo<>(list);

		System.out.println("当前页码"+pageInfo.getPageNum());
		System.out.println("总页码"+pageInfo.getPages());
		System.out.println("总记录数"+pageInfo.getTotal());
		//取得分页之后的单页的信息
		List<BArticles> list2 = pageInfo.getList();
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值