application.propertice配置mybatis数据源
时间: 2023-09-30 14:03:52 浏览: 43
对于application.properties文件配置MyBatis数据源,可以使用以下格式:
spring.datasource.url=jdbc:mysql://localhost:3306/mydatabase
spring.datasource.username=myusername
spring.datasource.password=mypassword
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
其中,url是数据库的连接地址,username和password是数据库的用户名和密码,driver-class-name是数据库驱动程序的名称。这些属性可以根据实际情况修改。
相关问题
application.properties配置mybatis
下面是一个典型的 application.properties 文件中的 MyBatis 配置示例:
```
# 数据库连接配置
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=123456
# MyBatis 配置
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.demo.model
mybatis.configuration.map-underscore-to-camel-case=true
```
其中,`spring.datasource` 部分是数据库连接配置,这里使用的是 MySQL 数据库;`mybatis.mapper-locations` 配置了 MyBatis 的 Mapper 所在位置,这里指定为 `classpath:mapper/*.xml`,表示在类路径下的 `mapper` 目录下,所有以 `.xml` 结尾的文件都是 Mapper 文件;`mybatis.type-aliases-package` 配置了 MyBatis 的别名包名,这里指定为 `com.example.demo.model`,表示在该包下的所有类都可以被 MyBatis 自动扫描并注册为别名;`mybatis.configuration.map-underscore-to-camel-case` 配置了 MyBatis 的命名规则,将下划线命名转为驼峰命名。
application.yml配置mybatis
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 包中。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)