我使用 HikariCP 和 jdbc 模板从一个数据库进行查询。我使用的方法如下:
我的 Hikari CP 配置文件(hikari.properties
):
driverClassName=org.postgresql.Driver
jdbcUrl=jdbc:postgresql://10.1.19.99:5432/mvc_data_base
maximumPoolSize=60
dataSource.cachePrepStmts=true
dataSource.prepStmtCacheSize=250
dataSource.prepStmtCacheSqlLimit=2048
我是这样配置的
@Configuration
public class AppConfig {
@Bean(destroyMethod = "close")
public DataSource dataSource() throws SQLException {
HikariConfig config = new HikariConfig("D:/hikari.properties");
HikariDataSource dataSource = new HikariDataSource(config);
return dataSource;
}
}
我想通过JDBC模板查询我所做的
public class UserDetail {
@Autowired
private JdbcTemplate jtmUserDetail;
List<Map<String, Object>> row1=
jtmUserDetail.queryForList("select * from pubic.user");
}
现在我想要类似的东西
public class UserDetail {
@Autowired
private JdbcTemplate jtmUserDetail;
@Autowired
private JdbcTemplate jtmUserDetail2;
List<Map<String, Object>> row1=
jtmUserDetail.queryForList("select * from pubic.user");
List<Map<String, Object>> row2=
jtmUserDetail2.queryForList("select * from pubic.user");
}
其中 jtmUserDetail
从一个数据库 (mvc_data_base
) 查询,jtmUserDetail2
从另一个数据库 (test_data_base
) 查询。
我不知道如何指定要查询的数据库。
最佳答案
您需要有 2 个数据源属性和 2 个函数来获取每个数据源
public DataSource dataSourceSecond() throws SQLException {
HikariConfig config = new HikariConfig("D:/hikari2.properties");
然后添加逻辑来控制何时使用每个数据源。
您可以使用 2 个 jdbcTemplate 和 different @Qualifier
关于java - 如何在 hikari 属性中添加两个数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47385440/