SSM 整合 分页插件 PageHelper
一、简介
现在开发做常用的ORM框架就是MyBatis了,在实际开发中少不了做分页处理,这里提供最常用的分页插件PageHelper
PageHelper官网:https://pagehelper.github.io/
二、springboot2.0 + PageHelper
注意: springboot2.0 集成时要使用1.2.x以后版本
1、maven依赖
https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
2、添加配置文件
方式一:application.yml 【推荐,已测】
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
params: count=countSql
方式二:application.properties 【未测】
logging.level.com.example.demo.dao=DEBUG
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
pagehelper.page-size-zero=true
3、demo演示
PageBean
package com.ssm.common.page;
import lombok.Data;
/**
* 分页bean
* @author qp
* @date 2019/4/26 15:03
*/
@Data
public class PageBean {
// 当前页数,默认为第一页
private Integer page = 1;
// 要查询的记录数,默认一次查询10条记录
private Integer size = 10;
}
UserServiceImpl
package com.ssm.service.impl;
import com.github.pagehelper.PageHelper;
import com.ssm.dao.UserDao;
import com.ssm.model.User;
import com.ssm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* @author qp
* @date 2019/4/12 10:04
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public List<User> listUser(User user) {
// 开启分页插件,注意必须放在查询语句上面
// 底层实现原理:将下面的查询方法中sql语句获取到之后利用AOP拼接 limit生成分页语句
PageHelper.startPage(user.getPage(), user.getSize());
return userDao.listUser();
}
}
三、spring4 + PageHelper 5
1、maven依赖
https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
2、添加配置文件
方式一:【推荐,已测】
https://github.com/qidasheng2012/ssm_simple
1)添加 mybatis-config.xml文件
<?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>
<!-- 配置分页插件 -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
2)修改applicationContext.xml
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
方式二:【已测】
1)修改applicationContext.xml
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=mysql
reasonable=true
supportMethodsArguments=true
params=count=countSql
autoRuntimeDialect=true
</value>
</property>
</bean>
</array>
</property>
</bean>
3、demo演示
同上