mybatisplus 3.1分页查询 配置类如何使用
时间: 2024-12-24 08:37:40 浏览: 19
MyBatisPlus 3.1版本的分页查询配置通常是在全局配置类中设置的,这个配置类一般叫做`GlobalConfig`或者自定义的`BaseMapperConfig`。以下是基本步骤:
1. **引入依赖**:
确保你在项目中已经添加了MyBatis Plus的依赖。如果你使用的是Maven,可以在pom.xml中加入相关依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.x</version>
</dependency>
```
2. **创建配置类**:
创建一个全局配置类,如`GlobalConfig.java`,并继承`GlobalConfiguration`:
```java
import com.baomidou.mybatisplus.core.config.GlobalConfig;
public class GlobalConfig extends GlobalConfiguration {
// 这里配置分页插件
@Override
public void configGlobal(MybatisPlusGlobalConfig mybatisPlusGlobalConfig) {
super.configGlobal(mybatisPlusGlobalConfig);
mybatisPlusGlobalConfig.pageHelper().setPagehelper core(true); // 开启分页插件
mybatisPlusGlobalConfig.pageHelper().setreasonable(false); // 是否开启合理的分页策略,默认为true,可以根据需要调整
}
}
```
3. **注册配置**:
在Spring Boot的启动类或者扫描注解的地方,注册你的全局配置类:
```java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Autowired
private GlobalConfig globalConfig;
@Bean
public MybatisSqlSessionFactory sqlSessionFactory() {
return new SqlSessionFactoryBuilder().build(globalConfig.getDataSource());
}
}
```
4. **启用自动分页**:
在你的服务层或Mapper接口上添加`@TableField(pageable = true)`注解,这样在执行SQL时就会自动加上分页信息。
注意:上述示例中的配置可能会随着版本更新而略有差异,建议参考官方文档进行最新设置。
阅读全文
相关推荐


















