springboot3 整合mybatis
时间: 2023-09-30 09:06:46 浏览: 215
SpringBoot整合mybatis
在Spring Boot中整合MyBatis,可以按照以下步骤进行操作:
1. 在启动类中添加`@MapperScan`注解,用于扫描Mapper文件夹的路径。例如:
```java
@SpringBootApplication
@MapperScan(basePackages = "com.example.springbootmybatisplusdemo.mapper")
public class SpringBootMybatisplusDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootMybatisplusDemoApplication.class, args);
}
}
```
这样,Spring Boot会自动扫描并注册Mapper接口。
2. 在pom.xml文件中添加MyBatis相关的依赖,包括`mybatis-spring-boot-starter`、`mysql-connector-java`和`druid`等。例如:
```xml
<!-- 今天加上mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.4</version>
</dependency>
```
3. 配置数据库连接和MyBatis的相关配置。可以在application.properties或application.yml文件中添加相关配置,如数据库连接信息、MyBatis配置文件路径等。
以上是整合MyBatis的基本步骤,根据具体需求可能还需要进行其他配置和操作。希望对你有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* *3* [Spring Boot 3.x- MybatisPlus集成](https://blog.csdn.net/renpeng301/article/details/124665330)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* [Springboot整合3.整合mybatis](https://blog.csdn.net/zhangchen124/article/details/124208920)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文