java鬼混笔记:springboot 9、springboot整合mybatis加上分页功能

时间:2022-12-20 09:53:08

之前笔记是springboot+mybatis,但是漏了一个重要的功能 ,那就是分页。分页的话,用国人提供的一个好插件pagerhelper,简单明了。上代码!!!

首先在pom.xml中引入新的jar

/////
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
////

然后在 application.properties 中加入配置,来个10分简单的

///
#分页
#配置说明:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md
#表示用的是mysql数据库
pagehelper.helperDialect=mysql
#reasonable:分页合理化参数,默认值为false。当该参数设置为 true 时,pageNum<=0 时会查询第一页, pageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询
pagehelper.reasonable=true
///

然后看service层方法:

///
public List<A> findAll(Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);//在调用Dao层查询的方法的上一行代码必须是分页方法来着,比如这里return aMapper.findAll();的上一行代码是PageHelper.startPage(pageNum, pageSize);
return aMapper.findAll();
}
///



OK 运行一下,效果就出来了