导包
<dependencies>
<!--Springboot-MVC -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.1</version>
</dependency>
<!--MySQL连接包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--工具包 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
在启动类中注入分页Bean
//Spring boot方式
@Configuration
@MapperScan("com.baomidou.cloud.service.*.mapper*")
public class MybatisPlusConfig {
/**
* 新的分页插件,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存出现问题(该属性会在旧插件移除后一同移除)
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
controller方法
注意包:import com.baomidou.mybatisplus.extension.plugins.pagination.Page;分页对象
这里注入service层就省略了
/**
* 分页查询
*/
@GetMapping("/findPage")
public List<User> fenye(@RequestParam(value = "page",defaultValue = "1") int page,@RequestParam(value = "size",defaultValue = "10") int size){
//创建分页对象
Page<User> paged = new Page<>(page,size);
List<User> jobInfos = service.selectUserPage(paged);
return jobInfos;
}
service方法
@Autowired
private UserMapper userMapper; //如果爆红也没事,假错误
/**
* 分页查询
*/
public List<User> selectUserPage(Page<User> page) {
// 调用Mapper接口返回一个page对象
Page<User> jobInfoPage = userMapper.selectPage(page, null);
//page对象转list对象
List<User> records = jobInfoPage.getRecords();
return records;
}