概述
(1)MyBatis-Plus有分页扩展插件,配置即可使用
(2)依赖版本
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.3.2</version>
</dependency>
注入插件
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MyBatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
编写查询接口
@GetMapping("/person/query")
public Page<Person> queryPerson(@RequestParam("name") String name, @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
//pageNum当前第几页,pageSize每页多少条数据
Page<Person> personPage = new Page<>(pageNum, pageSize);
//条件构造器和查询接口都是逆向工程生成的
QueryWrapper<Person> personQueryWrapper = new QueryWrapper<>();
//根据姓名模糊查询
personQueryWrapper.like("name", name);
return iPersonService.page(personPage, personQueryWrapper);
}
查看结果
访问http://localhost:8080/person/query?name=张三&pageNum=1&pageSize=5
{
"records": [
{
"id": 2,
"name": "张三",
"age": 1,
"createTime": "2021-07-29 16:22:52"
},
{
"id": 3,
"name": "张三",
"age": 2,
"createTime": "2021-07-29 16:22:52"
},
{
"id": 4,
"name": "张三",
"age": 3,
"createTime": "2021-07-29 16:22:52"
}
],
"total": 3,
"size": 5,
"current": 1,
"orders": [],
"optimizeCountSql": true,
"hitCount": false,
"searchCount": true,
"pages": 1
}
扩展内容
可以配置MyBatis-Plus日志
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
typeAliasesPackage: com.baomidou.springboot.entity
typeEnumsPackage: com.baomidou.springboot.entity.enums
global-config:
id-type: auto
table-underline: true
configuration:
map-underscore-to-camel-case: true
cache-enabled: false
## 这一行是MyBatis-Plus控制台日志
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
其实分页主要是在Sql后家里LIMIT