application.yml配置mybatis
时间: 2023-04-25 12:05:09 浏览: 975
Mybatis 配置可以在 application.yml 文件中进行。下面是一个示例配置:
```
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mapper/*.xml
type-aliases-package: com.example.domain
```
- `config-location`: Mybatis 配置文件的位置。
- `mapper-locations`: Mybatis 映射文件的位置。
- `type-aliases-package`: Mybatis 类型别名的包路径。
在上面的示例配置中,Mybatis 配置文件位于 classpath 下的 mybatis 目录中,名称为 mybatis-config.xml。Mybatis 映射文件位于 classpath 下的 mybatis/mapper 目录中,所有以 .xml 结尾的文件都会被加载。Mybatis 类型别名位于 com.example.domain 包中。
相关问题
application.yml配置tkmybatis
在Spring Boot项目中使用tk.mybatis时,需要在application.yml中配置相关信息。
示例配置:
```yaml
mybatis:
mapper-locations: classpath:mapper/*.xml # mapper文件路径
configuration:
map-underscore-to-camel-case: true # 下划线转驼峰
# tk.mybatis配置
mapper:
mappers:
- tk.mybatis.mapper.common.Mapper # 通用Mapper
not-empty: true # insert语句不插入null值
identity: MYSQL # 主键自增类型
```
说明:
- `mybatis.mapper-locations`:mapper文件路径,可以使用通配符,例如`classpath:mapper/**/*.xml`。
- `mybatis.configuration.map-underscore-to-camel-case`:是否开启下划线转驼峰功能。
- `mybatis.mapper.mappers`:需要使用的通用Mapper接口,可以配置多个。
- `mybatis.mapper.not-empty`:insert语句是否插入null值。
- `mybatis.mapper.identity`:主键自增类型,可选值为`MYSQL`、`ORACLE`等。
注意:
- 在使用tk.mybatis时,需要在mapper接口上加上`@Mapper`注解。
- 在mapper.xml文件中,需要使用`tk.mybatis.mapper.common.Mapper`接口中的方法,例如`insertSelective`、`selectByPrimaryKey`等。
application.yml mybatis配置mysql
在Spring Boot项目中,如果你想要通过`application.yml`文件配置MyBatis连接MySQL数据库,你需要做以下几个步骤:
1. **创建application.yml文件**:这个文件通常位于项目的`src/main/resources`目录下。在这个文件中,你可以设置数据源和MyBatis的相关配置。
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/your_database_name?serverTimezone=UTC
username: your_username
password: your_password
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
type-aliases-package: com.example.your_package.name # 自定义别名包名
mapper-locations: classpath:mapper/*.xml # 映射器XML文件路径
```
- `url`: 数据库地址,包括主机、端口和数据库名称。
- `username` 和 `password`: 数据库用户名和密码。
- `driver-class-name`: 指定用于连接MySQL的JDBC驱动,如上述示例中的`com.mysql.cj.jdbc.Driver`。
- `type-aliases-package`: 设置自定义的实体别名包。
- `mapper-locations`: 定义Mapper XML文件的位置,`classpath:`前缀表示查找类路径下的资源。
2. **添加依赖**:确保在`pom.xml`或`build.gradle`中包含了Spring Boot Data JPA和MyBatis的依赖。
```xml
<!-- Maven -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
// Gradle (Kotlin)
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.mybatis.spring.boot:mybatis-spring-boot-starter")
```
完成以上配置后,Spring Boot会自动扫描配置并初始化MyBatis与数据库的连接。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)