SpringBoot2整合MyBatis
前言
在整合MyBatis之前,我们要准备哪些工作:
导入mysql场景:
```xml
<!--SpringBoot对mysql有版本仲裁,博主当前是SpringBoot2.6.6,mysql仲裁版本如下 -->
<mysql.version>8.0.28</mysql.version>
<!--大家也可自行查看自己的版本号 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<mysql.version>8.0.28</mysql.version> <!--使用默认版本,不需要配version -->
</dependency>
想要修改版本
1、直接依赖引入具体版本(maven的就近原则)
2、重新声明版本(maven的属性的就近原则)
<java.version>1.8</java.version>
<mysql.version>8.0.28</mysql.version>
```
配置文件修改:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/user
username: root
password: 123456
driver-class-name: com.mysql.jdbc.Driver
# 如果是mysql8以上版本,请使用下面配置:
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/hunnu?characterEncoding=utf-8&serverTimezone=UTC
username: root
password: 123456
```
1.导入Mybatis的starter(场景启动器)
<!--mybatis场景-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
注意:mybatis的场景包含了jdbc的依赖,所以不需要再次导入了
<!--jdbc场景 -->
<dependency>