文章目录
Spring Boot 配置文件 之 application.properties
(一)数据源配置
0、引入依赖包(pom.xml)
在pom.xml文件中引入 JDBC依赖包 和 相关数据库驱动包。
(1)JDBC依赖包
使用 Spring 的
JdbcTemplate
进行数据库操作,必须引入的 JDBC 依赖包。如下所示:
pom.xml<!--Spring Boot 集成 JdbcTemplate--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
备注:
spring-boot-starter-jdbc
依赖包,是在使用 Spring Boot 的JdbcTemplate
操作数据库时才需要添加。如果直接使用原始的 JDBC 连接操作数据库
,是不需要添加这个依赖包的。下面的例子中使用了Spring Boot 的JdbcTemplate
操作数据库,所以需要添加这个依赖。
关于使用“原始的 JDBC 连接操作数据库”,可参考文章:使用原始的 JDBC 操作数据库_Shipley_Leo的博客-CSDN博客
(2)数据库驱动包
① MySQL 驱动包
使用 MySQL 数据库,需要引入 MySQL 驱动包。如下所示:
pom.xml<!--MySQL 驱动包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
② Oracle 驱动包
使用 Oracle 数据库,需要引入 Oracle 驱动包。如下所示:
pom.xml<!-- Oracle 驱动包 --> <dependency> <groupId>com.oracle.database.jdbc</groupId> <artifactId>ojdbc8</artifactId> <version>21.5.0.0</version> <scope>runtime</scope> </dependency> <!-- Additional library required to support Internationalization --> <dependency> <groupId>com.oracle.database.nls</groupId> <artifactId>orai18n</artifactId> <version>21.5.0.0</version> <scope>provided</scope> </dependency>
1、单数据源配置(application.properties)
(1)MySQL 数据源配置
application.properties
# MySQL数据源配置 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/jdbctest?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true spring.datasource.username=root spring.datasource.password=******
备注:使用 MySQL 数据库,需要引入 MySQL 驱动包。
(2)Oracle 数据源配置
application.properties
## Oracle数据源配置 ##spring.profiles=oracleDb #spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver #spring.datasource.url=jdbc:oracle:thin:@localhost:1521:orcl #spring.datasource.username=system #spring.datasource.password=*********
备注:使用 Oracle 数据库,需要引入 Oracle 驱动包。
单数据源
的具体使用案例可参考文章:7.1 JdbcTemplate 入门_Shipley_Leo的博客-CSDN博客
2、多数据源配置(application.properties)
application.properties
# 配置 JdbcTemplate 多数据源 # 数据源1(首选/主要的):MySQL spring.datasource.primary.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.primary.jdbc-url=jdbc:mysql://localhost:3306/jdbc_test spring.datasource.primary.username=root spring.datasource.primary.password=****** ## 数据源2(次选/次要的):MySQL spring.datasource.secondary.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.secondary.jdbc-url=jdbc:mysql://localhost:3306/jdbc_test2 spring.datasource.secondary.username=root spring.datasource.secondary.password=****** # 数据源3(第三的):Oracle spring.datasource.tertiary.driver-class-name=oracle.jdbc.driver.OracleDriver spring.datasource.tertiary.jdbc-url=jdbc:oracle:thin:@localhost:1521:orcl spring.datasource.tertiary.username=system spring.datasource.tertiary.password=*********
配置 JDBC 初始化
DataSourceConfig.java
package com.example.jdbctemplateproject.config; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.jdbc.DataSourceBuilder; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; /** * 配置 JDBC 初始化 * * @author: shipleyleo * @create: 2023-04-04 20:35:30 */ @Configuration public class DataSourceConfig { // @Primary @Bean(name = "primaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.primary") public DataSource primaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "secondaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.secondary") public DataSource secondaryDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "tertiaryDataSource") @ConfigurationProperties(prefix = "spring.datasource.tertiary") public DataSource tertiaryDataSource() { return DataSourceBuilder.create().build(); } @Primary @Bean(name = "primaryJdbcTemplate") public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "secondaryJdbcTemplate") public JdbcTemplate secondaryJdbcTemplate(@Qualifier("secondaryDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "tertiaryJdbcTemplate") public JdbcTemplate tertiaryJdbcTemplate(@Qualifier("tertiaryDataSource") DataSource dataSource) { return new JdbcTemplate(dataSource); } } // 通过使用 @Qualifier 注解,我们可以消除需要注入哪个 bean 的问题。参考文章:https://zhuanlan.zhihu.com/p/100371910 // primaryJdbcTemplate缺少@Primary注解导致报错问题。参考文章:https://blog.csdn.net/Shipley_Leo/article/details/129994588
多数据源
的具体使用案例可参考文章:7.3 实战:实现 JdbcTemplate 多数据源_Shipley_Leo的博客-CSDN博客