mybatisplus 可以只开分页插件 不开count吗
时间: 2023-09-02 13:15:42 浏览: 46
是的,MyBatis Plus分页插件提供了两种分页方式:基于 count 查询和不基于 count 查询。默认情况下,分页插件开启了基于 count 的分页查询,即在执行分页查询时,会先执行一次查询获取总记录数,然后再执行一次带有分页参数的查询获取分页数据。
如果你只需要分页数据而不需要总记录数,可以通过配置来关闭基于 count 的分页查询,只开启不基于 count 的分页查询。具体来说,你可以在 MyBatis 配置文件中配置 `dialectType` 属性,将其设置为 `noCount`,即可只开启不基于 count 的分页查询。例如:
```xml
<!-- MyBatis Plus 分页插件配置 -->
<plugins>
<plugin interceptor="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
<property name="dialectType" value="noCount"/>
</plugin>
</plugins>
```
这样配置后,MyBatis Plus 将只执行一次带有分页参数的查询获取分页数据,而不会执行获取总记录数的查询。
相关问题
mybatisplus 不用分页插件为什么也会先count一次
MybatisPlus默认开启了分页插件,如果不需要分页功能,可以在MybatisPlus的配置文件中进行配置:
```
mybatis-plus:
configuration:
# 是否开启分页插件
page-helper:
enable: false
```
如果开启了分页插件,无论是否使用分页功能,都会先执行一次count查询来获取总记录数。这是因为分页插件需要知道总记录数才能计算出总页数,从而实现分页功能。如果不需要分页功能,可以禁用分页插件来避免这种情况。
springboot实现mybatisplus按照部门实现分页查询
1. 首先需要在pom.xml中添加mybatisplus和分页插件的依赖:
```
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
```
2. 在application.yml中配置mybatisplus和分页插件的参数:
```
mybatis-plus:
mapper-locations: classpath:/mapper/**/*.xml
global-config:
db-config:
id-type: auto
field-strategy: not_empty
table-prefix: sys_
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
```
3. 编写Mapper接口:
```
@Mapper
public interface DeptMapper extends BaseMapper<Dept> {
List<Dept> selectPage(Page<Dept> page, @Param("deptName") String deptName);
}
```
4. 编写Service实现类:
```
@Service
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {
@Override
public IPage<Dept> selectPage(Page<Dept> page, String deptName) {
QueryWrapper<Dept> queryWrapper = new QueryWrapper<>();
if (StringUtils.isNotBlank(deptName)) {
queryWrapper.like("dept_name", deptName);
}
return baseMapper.selectPage(page, queryWrapper);
}
}
```
5. 编写Controller:
```
@RestController
@RequestMapping("/dept")
public class DeptController {
@Autowired
private DeptService deptService;
@GetMapping("/list")
public Result list(Page<Dept> page, String deptName) {
IPage<Dept> data = deptService.selectPage(page, deptName);
return Result.success(data);
}
}
```
6. 在前端页面中,可以通过ajax请求/list接口,传入参数page和deptName实现分页查询。