如何在springboot中创建两个数据源,其中辅助数据库依赖于主数据库(最初辅助数据库未知)
我想创建一个具有两个db连接的应用程序,其中在主db连接的基础上选择辅助db连接 我已经创建了两个数据源,一个是@primary,另一个是secondary,我在加载主页后创建的,但不起作用如何在springboot中创建两个数据源,其中辅助数据库依赖于主数据库(最初辅助数据库未知),spring,spring-boot,Spring,Spring Boot,我想创建一个具有两个db连接的应用程序,其中在主db连接的基础上选择辅助db连接 我已经创建了两个数据源,一个是@primary,另一个是secondary,我在加载主页后创建的,但不起作用 i have done something like this but not able to create second one package com.example.edunext.Configuration; import com.example.edunext.model.
i have done something like this but not able to create second one
package com.example.edunext.Configuration;
import com.example.edunext.model.User;
import com.example.edunext.model.UserDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.DependsOn;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@Component
public class Registry {
private static final Map<String, Object> registry = new HashMap<String, Object>();
@Autowired
@Qualifier("jdbcTemplate1")
private JdbcTemplate jdbcTemplate1;
public String getdb()
{
String sql1 = "select email from user1";
List<User> list1 = jdbcTemplate1.query(sql1, new UserDaoImpl.UserRowMapper());
return list1.get(0).getEmail();
}
@Bean(name = "db2")
@DependsOn("db1")
@ConfigurationProperties(prefix = "spring.second-db")
public DataSource dataSource2() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
try {
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
String jdbcurl="jdbc:mysql://localhost:3306/"+getdb();
dataSource.setUrl(jdbcurl);
dataSource.setUsername("root");
dataSource.setPassword("hrhk");
return dataSource;
}
catch(Exception ex)
{
return dataSource;
}
}
@Bean(name = "jdbcTemplate2")
@DependsOn("jdbcTemplate1")
public JdbcTemplate jdbcTemplate2(@Qualifier("db2") DataSource ds) {
if(ds==null)
{
return null;
}
else {
return new JdbcTemplate(ds);
}
}
}
包com.example.edunext.Configuration;
导入com.example.edunext.model.User;
导入com.example.edunext.model.UserDaoImpl;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.boot.context.properties.ConfigurationProperties;
导入org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.ComponentScan;
导入org.springframework.context.annotation.DependsOn;
导入org.springframework.jdbc.core.jdbc模板;
导入org.springframework.jdbc.datasource.driverManager数据源;
导入org.springframework.stereotype.Component;
导入javax.sql.DataSource;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入java.util.stream.collector;
导入java.util.stream.stream;
@组成部分
公共类注册表{
私有静态最终映射注册表=new HashMap();
@自动连线
@限定符(“jdbcTemplate1”)
私有JdbcTemplate jdbcTemplate1;
公共字符串getdb()
{
String sql1=“选择来自user1的电子邮件”;
List list1=jdbcTemplate1.query(sql1,新的UserDaoImpl.UserRowMapper());
返回list1.get(0.getEmail();
}
@Bean(name=“db2”)
@德彭森(“db1”)
@配置属性(前缀=“spring.second db”)
公共数据源数据源2(){
DriverManager数据源dataSource=新的DriverManager数据源();
试一试{
setDriverClassName(“com.mysql.jdbc.Driver”);
String jdbcurl=“jdbc:mysql://localhost:3306/“+getdb();
setUrl(jdbcurl);
dataSource.setUsername(“根”);
dataSource.setPassword(“hrhk”);
返回数据源;
}
捕获(例外情况除外)
{
返回数据源;
}
}
@Bean(name=“jdbcTemplate2”)
@DependsOn(“jdbcTemplate1”)
公共JdbcTemplate jdbcTemplate2(@Qualifier(“db2”)数据源ds){
如果(ds==null)
{
返回null;
}
否则{
返回新的JdbcTemplate(ds);
}
}
}
这可能会有帮助。您能否澄清“它不起作用”的含义?你有什么错误吗?你在使用什么代码?我想根据requesturl(我已经用数据库名存储在我的主数据库中)创建一个辅助数据源@EssexBoy谢谢,但我的第二个数据库依赖于第一个数据库(根据url,我将选择第二个数据库)