springboot3.4.1版本整合mybatisplus流程
时间: 2025-01-24 19:53:02 浏览: 50
### Spring Boot 3.4.1 中集成 MyBatis Plus 的逐步指南
#### 添加依赖项
为了在项目中使用 MyBatis Plus,需修改 `pom.xml` 文件来添加必要的 Maven 依赖。具体来说,应该引入 MyBatis-Spring-Boot-Starter 和 MyBatis-Plus 的依赖。
```xml
<dependencies>
<!-- MyBatis Spring Boot Starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- MyBatis Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
</dependencies>
```
上述代码展示了如何向项目的构建文件中添加所需的库[^4]。
#### 创建分页配置类
创建名为 `MybatisPlusConfig.java` 的 Java 类用于自定义 MyBatis Plus 行为,特别是设置分页插件:
```java
package com.springboot.springbootmybatisplus.config;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
```
这段代码实现了分页拦截器的注册,从而允许应用程序执行带有限制条件的数据查询操作[^1]。
#### 数据源配置
编辑 application.properties 或者 application.yml 来指定数据库连接参数和其他属性。例如,在 YAML 格式的配置文件里可以这样写:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
mapper-locations: classpath*:mapper/*.xml
typeAliasesPackage: com.example.demo.entity
```
这里指定了 MySQL 数据库的位置以及访问凭证,并告知框架去哪里寻找映射 XML 文件和实体类所在位置[^2]。
完成以上步骤之后,重启应用服务器即可让新的更改生效并开始利用 MyBatis Plus 提供的功能增强业务逻辑层。
阅读全文
相关推荐


















