(1)使用 LIMIT 关键字
业务层将pageNo 和pageSize传过来,即可实现分页操作。
灵活性高,可优化空间大,但是对于新手比较复杂。
select * from user limit #{pageNo}, #{pageSize}
(2)RowBounds实现分页
Mybatis官方提供RowBounds类来实现逻辑分页。RowBounds中有2个字段offset和limit。这种方式获取所有的ResultSet,从ResultSet中的offset位置开始获取limit个记录。
只需要填充两个参数到RowBounds中,即可使用。
controller层:
RowBounds rowBounds = new RowBounds(page, size);
mapper层:
Listfindpage(RowBounds rowBounds);
mapper.xml:
三、PageHelper实现分页
PageHelper是一个第三方实现的分页拦截器插件。
添加pom依赖:
com.github.pagehelper pagehelper5.3.1
直接使用pagehelper方法,更加简便。
二、Mybatis-plus分页(1)加入pom依赖
com.baomidou
mybatis-plus-boot-starter
3.1.0
(2)在启动类同层写个Mybatis-plus配置类:
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableTransactionManagement
@Configuration
@MapperScan("com.exam.service.*.mapper*")
public class MybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
(3)传入参数,构建Page<>对象
最后,实现完整的查询操作即可。



