1 pom文件引入依赖 (注意:pagehelper版本不能太高,楼主之前用的5.0以上的版本,然后分页没有效果,浪费了两个小时才发现这个原因)
<!-- mybatis的分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
2 新建mybatis的config类
package com.boot.security.server.config;
import com.github.pagehelper.PageHelper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
@Configuration
public class MybatisConfig {
@Bean
public PageHelper pageHelper() {
System.out.println("MyBatisConfiguration.pageHelper()");
PageHelper pageHelper = new PageHelper();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "true");
p.setProperty("dialect","mysql");
pageHelper.setProperties(p);
return pageHelper;
}
}
3 使用分页 (注意:注意,下面的方法中总共有四行代码,前三行,编写顺序一定不能乱,不然会查询全部,分页无效,亲测)
@GetMapping("/roleList")
@ApiOperation(value = "获取角色类表")
@PreAuthorize("hasAuthority('sys:role:roleList')")
public String getRoleList(Integer page , Integer limit){
PageHelper.startPage(page,limit);
List<Role> list =roleDao.getRoleList();
PageInfo<Role> pageInfo=new PageInfo<>(list);
return JSON.toJSONString(pageInfo);
}