【ssm框架】使用PageHelper插件进行分页

通常我们要实现分页,都需要传入一个当前页,要读的数据个数,经过计算之后,将需要的值填入接在sql语句limit关键字的后面。然而使用PageHelper之后我们并不需要对sql添加什么limit,直接按查询全部去写sql语句即可。只要在查询之前调用PageHelper.startPage(page,rows),它会帮我们把limit及后面的内容自动注入到我们的sql语句中进行查询,大大方便了我们的开发以及sql语句的管理。下面就来看看如何使用这个插件吧。

 

一、首先在pom.xml添加依赖

<dependency>

<groupId>com.github.pagehelper</groupId>

<artifactId>pagehelper</artifactId>

<version>4.1.6</version>

</dependency>

二、在mybatis-config.xml中添加插件

<?xml version="1.0"encoding="UTF-8" ?>

<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<plugins>

<!-- 分页查询插件 -->

<plugininterceptor="com.github.pagehelper.PageHelper">

<propertyname="dialect" value="mysql" />

<propertyname="reasonable" value="true" />

</plugin>

</plugins>

</configuration>

三、在spring-mybatis.xml中引入该配置文件

<bean id="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">

<description>spring和MyBatis完美整合,不需要mybatis的配置映射文件</description>

<propertyname="dataSource" ref="dataSource" />

<propertyname="configLocation"value="classpath:mybatis/mybatis-config.xml" />

<!-- 自动扫描mapping.xml文件 -->

<propertyname="mapperLocations"value="classpath:mybatis/mappers/**/*.xml" />

</bean>

 

 

实际使用代码如下:

public PageInfo<User> listByPage(intpage, int rows, String keywords, UserType type) throws Exception{

PageHelper.startPage(page,rows);

List<User> list =userMapper.search(keywords, type, null);

PageInfo<User> pageInfo = newPageInfo<>(list);

return pageInfo;

}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值