如何在 spring-boot 中使用 2 个或更多 jdbcTemplate?
How to use 2 or more jdbcTemplate with spring-boot?
我想在我的项目中使用 2 个或更多 jdbcTemplate application.properties.I 尝试但出现运行时异常。
########## 我的 application.properties:-
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ccm_new
spring.datasource.username=test
spring.datasource.password=test
spring.oracledatasource.url=jdbc:oracle:thin:@localhost:1521:mastera
spring.oracledatasource.password=test
spring.oracledatasource.username=test
spring.oracledatasource.driver-class-name=oracle.jdbc.driver.OracleDriver
@Bean(name = "dsMaster") ############
@Primary
@ConfigurationProperties(prefix="spring.oracledatasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMaster") #############
public JdbcTemplate masterJdbcTemplate(@Qualifier("dsMaster") DataSource dsMaster)
{
return new JdbcTemplate(dsMaster);
}
################我正常使用 mysql 连接但是在使用 oracle 连接时我得到了
org.springframework.jdbc.CannotGetJdbcConnectionException:获取JDBC连接失败;嵌套异常是 java.sql.SQLException:无法为连接 URL 'null' 创建 class '' 的 JDBC 驱动程序
在 org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:81)
在 org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:371)
在 org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:446)
在 org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456)
在 enter code here
我弄错了,我想通过 application.properties 建立 mysql 连接而不需要 @bean configuration.If 你想建立 2 个或更多连接你只需要定义所有数据源的 @ConfigurationProperties(prefix="spring.mysqldatasource") 不同于 "spring.datasource".prifix " spring.datasource" 的不同前缀仅在我们只需要从一个 database.Here 建立连接时使用是最终的工作代码示例:-
application.properties
spring.mysqldatasource.driver-class-name=com.mysql.jdbc.Driver
spring.mysqldatasource.url=jdbc:mysql://localhost:3306/ccm_new
spring.mysqldatasource.username=test
spring.mysqldatasource.password=test
spring.mysqldatasource.dbcp2.initial-size=5
spring.mysqldatasource.dbcp2.max-total=15
spring.mysqldatasource.dbcp2.pool-prepared-statements=true
spring.oracledatasource.url=jdbc:oracle:thin:@localhost:1521:mastera
spring.oracledatasource.password=test
spring.oracledatasource.username=test
spring.oracledatasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.oracledatasource.dbcp2.initial-size=5
spring.oracledatasource.dbcp2.max-total=15
spring.oracledatasource.dbcp2.pool-prepared-statements=true
@Configuration
public class PrototypeUtility {
@Bean(name = "dsMaster")
@Primary
@ConfigurationProperties(prefix="spring.oracledatasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMaster")
public JdbcTemplate masterJdbcTemplate(@Qualifier("dsMaster") DataSource dsMaster) {
return new JdbcTemplate(dsMaster);
}
@Bean(name = "dsMasterMysql")
@ConfigurationProperties(prefix="spring.mysqldatasource")
public DataSource primaryDataSourceMysql() {
return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMasterMysql")
public JdbcTemplate masterMysqlJdbcTemplate(@Qualifier("dsMasterMysql") DataSource dsMasterMysql) {
return new JdbcTemplate(dsMasterMysql);
}
}
然后我自动连接了两个连接:-
@Autowired
private JdbcTemplate jdbcMasterMysql;
@Autowired
public JdbcTemplate jdbcMaster;
这段代码 运行 对我来说成功了。
如果有人有疑问,请不要犹豫。
我弄错了,我想通过 application.properties 建立 mysql 连接而不需要 @bean configuration.If 你想建立 2 个或更多连接你只需要定义所有数据源的 @ConfigurationProperties(prefix="spring.mysqldatasource") 不同于 "spring.datasource".prifix " spring.datasource" 的不同前缀仅在我们需要仅从一个 database.Here 建立连接时使用是最终的工作代码示例:-
application.properties
spring.mysqldatasource.driver-class-name=com.mysql.jdbc.Driver
spring.mysqldatasource.url=jdbc:mysql://localhost:3306/ccm_new
spring.mysqldatasource.username=test
spring.mysqldatasource.password=test
spring.mysqldatasource.dbcp2.initial-size=5
spring.mysqldatasource.dbcp2.max-total=15
spring.mysqldatasource.dbcp2.pool-prepared-statements=true
spring.oracledatasource.url=jdbc:oracle:thin:@localhost:1521:mastera
spring.oracledatasource.password=test
spring.oracledatasource.username=test
spring.oracledatasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.oracledatasource.dbcp2.initial-size=5
spring.oracledatasource.dbcp2.max-total=15
spring.oracledatasource.dbcp2.pool-prepared-statements=true
@Configuration
public class PrototypeUtility {
@Bean(name = "dsMaster")
@Primary
@ConfigurationProperties(prefix="spring.oracledatasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMaster")
public JdbcTemplate masterJdbcTemplate(@Qualifier("dsMaster") DataSource dsMaster) {
return new JdbcTemplate(dsMaster);
}
@Bean(name = "dsMasterMysql")
@ConfigurationProperties(prefix="spring.mysqldatasource")
public DataSource primaryDataSourceMysql() {
return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMasterMysql")
public JdbcTemplate masterMysqlJdbcTemplate(@Qualifier("dsMasterMysql") DataSource dsMasterMysql) {
return new JdbcTemplate(dsMasterMysql);
}
}
我想在我的项目中使用 2 个或更多 jdbcTemplate application.properties.I 尝试但出现运行时异常。
########## 我的 application.properties:- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ccm_new
spring.datasource.username=test
spring.datasource.password=test
spring.oracledatasource.url=jdbc:oracle:thin:@localhost:1521:mastera
spring.oracledatasource.password=test
spring.oracledatasource.username=test
spring.oracledatasource.driver-class-name=oracle.jdbc.driver.OracleDriver
@Bean(name = "dsMaster") ############
@Primary
@ConfigurationProperties(prefix="spring.oracledatasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMaster") #############
public JdbcTemplate masterJdbcTemplate(@Qualifier("dsMaster") DataSource dsMaster)
{
return new JdbcTemplate(dsMaster);
}
################我正常使用 mysql 连接但是在使用 oracle 连接时我得到了
org.springframework.jdbc.CannotGetJdbcConnectionException:获取JDBC连接失败;嵌套异常是 java.sql.SQLException:无法为连接 URL 'null' 创建 class '' 的 JDBC 驱动程序
在 org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:81)
在 org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:371)
在 org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:446)
在 org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:456)
在 enter code here
我弄错了,我想通过 application.properties 建立 mysql 连接而不需要 @bean configuration.If 你想建立 2 个或更多连接你只需要定义所有数据源的 @ConfigurationProperties(prefix="spring.mysqldatasource") 不同于 "spring.datasource".prifix " spring.datasource" 的不同前缀仅在我们只需要从一个 database.Here 建立连接时使用是最终的工作代码示例:-
application.properties
spring.mysqldatasource.driver-class-name=com.mysql.jdbc.Driver
spring.mysqldatasource.url=jdbc:mysql://localhost:3306/ccm_new
spring.mysqldatasource.username=test
spring.mysqldatasource.password=test
spring.mysqldatasource.dbcp2.initial-size=5
spring.mysqldatasource.dbcp2.max-total=15
spring.mysqldatasource.dbcp2.pool-prepared-statements=true
spring.oracledatasource.url=jdbc:oracle:thin:@localhost:1521:mastera
spring.oracledatasource.password=test
spring.oracledatasource.username=test
spring.oracledatasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.oracledatasource.dbcp2.initial-size=5
spring.oracledatasource.dbcp2.max-total=15
spring.oracledatasource.dbcp2.pool-prepared-statements=true
@Configuration
public class PrototypeUtility {
@Bean(name = "dsMaster")
@Primary
@ConfigurationProperties(prefix="spring.oracledatasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMaster")
public JdbcTemplate masterJdbcTemplate(@Qualifier("dsMaster") DataSource dsMaster) {
return new JdbcTemplate(dsMaster);
}
@Bean(name = "dsMasterMysql")
@ConfigurationProperties(prefix="spring.mysqldatasource")
public DataSource primaryDataSourceMysql() {
return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMasterMysql")
public JdbcTemplate masterMysqlJdbcTemplate(@Qualifier("dsMasterMysql") DataSource dsMasterMysql) {
return new JdbcTemplate(dsMasterMysql);
}
}
然后我自动连接了两个连接:-
@Autowired
private JdbcTemplate jdbcMasterMysql;
@Autowired
public JdbcTemplate jdbcMaster;
这段代码 运行 对我来说成功了。 如果有人有疑问,请不要犹豫。
我弄错了,我想通过 application.properties 建立 mysql 连接而不需要 @bean configuration.If 你想建立 2 个或更多连接你只需要定义所有数据源的 @ConfigurationProperties(prefix="spring.mysqldatasource") 不同于 "spring.datasource".prifix " spring.datasource" 的不同前缀仅在我们需要仅从一个 database.Here 建立连接时使用是最终的工作代码示例:-
application.properties
spring.mysqldatasource.driver-class-name=com.mysql.jdbc.Driver
spring.mysqldatasource.url=jdbc:mysql://localhost:3306/ccm_new
spring.mysqldatasource.username=test
spring.mysqldatasource.password=test
spring.mysqldatasource.dbcp2.initial-size=5
spring.mysqldatasource.dbcp2.max-total=15
spring.mysqldatasource.dbcp2.pool-prepared-statements=true
spring.oracledatasource.url=jdbc:oracle:thin:@localhost:1521:mastera
spring.oracledatasource.password=test
spring.oracledatasource.username=test
spring.oracledatasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.oracledatasource.dbcp2.initial-size=5
spring.oracledatasource.dbcp2.max-total=15
spring.oracledatasource.dbcp2.pool-prepared-statements=true
@Configuration
public class PrototypeUtility {
@Bean(name = "dsMaster")
@Primary
@ConfigurationProperties(prefix="spring.oracledatasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMaster")
public JdbcTemplate masterJdbcTemplate(@Qualifier("dsMaster") DataSource dsMaster) {
return new JdbcTemplate(dsMaster);
}
@Bean(name = "dsMasterMysql")
@ConfigurationProperties(prefix="spring.mysqldatasource")
public DataSource primaryDataSourceMysql() {
return DataSourceBuilder.create().build();
}
@Bean(name = "jdbcMasterMysql")
public JdbcTemplate masterMysqlJdbcTemplate(@Qualifier("dsMasterMysql") DataSource dsMasterMysql) {
return new JdbcTemplate(dsMasterMysql);
}
}