springboot mybatisplus 集成不同数据库
时间: 2023-07-25 17:44:15 浏览: 61
Spring Boot 集成 Mybatis-plus 同样支持集成不同类型的数据库。需要在配置文件中配置数据源和 Mybatis-plus 相关的配置,具体示例代码如下:
MySQL:
```properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db_name?characterEncoding=utf-8&useSSL=false
spring.datasource.username=username
spring.datasource.password=password
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.type-aliases-package=com.example.demo.entity
```
Oracle:
```properties
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
spring.datasource.username=username
spring.datasource.password=password
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.type-aliases-package=com.example.demo.entity
```
SQLServer:
```properties
spring.datasource.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://localhost:1433;DatabaseName=db_name
spring.datasource.username=username
spring.datasource.password=password
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.type-aliases-package=com.example.demo.entity
```
PostgreSQL:
```properties
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/db_name
spring.datasource.username=username
spring.datasource.password=password
mybatis-plus.mapper-locations=classpath:mapper/*.xml
mybatis-plus.type-aliases-package=com.example.demo.entity
```
需要注意的是,除了配置不同类型的数据库连接信息外,还需要配置 Mybatis-plus 的 mapper-locations 和 type-aliases-package。其中 mapper-locations 是指定 Mapper 文件的存放位置,type-aliases-package 是指定实体类的包路径,这两个配置项需要根据项目实际情况进行配置。