springboot集合mybatis
时间: 2023-06-05 09:48:11 浏览: 80
Spring Boot集成MyBatis可以让我们更方便地使用MyBatis进行数据库操作。通过使用Spring Boot的自动配置和MyBatis的注解,我们可以快速地创建一个可靠的、高效的数据库访问层。同时,Spring Boot还提供了很多有用的特性,如自动配置、自动扫描、自动装配等,使得我们的开发更加简单、快速、高效。
相关问题
springboot集合mybatis,mysql的依赖
在pom.xml文件中添加以下依赖:
```
<dependencies>
<!-- SpringBoot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<!-- MySQL 依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
</dependencies>
```
其中,`spring-boot-starter-web`为SpringBoot Web的依赖,`mybatis-spring-boot-starter`为MyBatis的依赖,`mysql-connector-java`为MySQL的依赖。请根据自己的需求选择版本号。
springboot集合mybatis需要的依赖和yml配置
一、依赖
在pom.xml文件中添加如下依赖:
```
<!-- springboot集成mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
```
二、yml配置
在application.yml文件中添加如下配置:
```
# 数据源配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
# mybatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml # mapper文件的位置,可以是classpath下的任意目录或jar包里的文件
configuration:
map-underscore-to-camel-case: true # 开启驼峰命名规则,将下划线命名的数据库字段自动转换为驼峰命名的Java属性
```
其中,datasource中的url、username和password需要根据实际情况修改。mapper-locations用于指定mapper文件的位置,可以是classpath下的任意目录或jar包里的文件。configuration中的map-underscore-to-camel-case用于开启驼峰命名规则,将下划线命名的数据库字段自动转换为驼峰命名的Java属性。