常用的ORM框架:MyBatis
常用的分页插件:PageHelper
https://pagehelper.github.io/
Spring Boot 2.x 整合 PageHelper 实现分页
1、maven依赖
https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>5.1.2version>
dependency>
2、添加配置文件
方式一:【推荐,已测】
https://github.com/qidasheng2012/ssm_simple
1)添加 mybatis-config.xml文件
<configuration>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<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"/>
<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演示
【PageBean】
package com.ssm.common.page;
import lombok.Data;
/**
* 分页bean
* @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;
/**
* @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();
}
}