java—如何使用SpringJDBC模板通过Map进行迭代并将每个键保存到不同的数据库中?

fgw7neuy  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(272)

我有下面的Map
Map<整数,列表>

Key            Value
190 -> Account(1, "abc", 17998210, 190)
       Account(2, "hsj", 6786179, 190)
191 -> Account(4, "ioip", 246179, 191)
       Account(4, "ewrew", 90179, 191)

对于键190,我想写入oracle db,对于键191,我想写入postgres数据库。我使用的是jdbctemplate
我在做什么

class AccountServiceImpl
{

   @Autowired
   private AccountDaoImpl daoImpl;

   for(Map.Entry<String, List<Account>> entry : map.keySet()){
       daoImpl.saveEntry(entry.getKey(), entry.getValue()); // Is there a better way to iterate and call saveEntry for each key?
   }
}

class AccountDaoImpl{

public void saveEntry(String key, List<Account> list){
   int[][] result = getJdbcTemplate(key).batchUpdate(...///......)

}

}
问题:在accountserviceimpl类中,我有 daoImpl.saveEntry(entry.getKey(), entry.getValue()); 是否有更好的方法对每个键进行迭代并调用saveentry?

3ks5zfa0

3ks5zfa01#

您可以创建不同的数据源以用于存储数据
为两个数据源创建配置

@Configuration
public class ConfigDataSource {

    final public static String BEAN_JDBC_TEMPLATE_POSTGRES = "jdbcTemplatePostgres";
    final public static String BEAN_JDBC_TEMPLATE_ORACLE = "jdbcTemplateOracle";
    final public static String BEAN_DATASOURCE_POSTGRES = "dataSourcePostgres";
    final public static String BEAN_DATASOURCE_ORACLE = "dataSourceOracle";

    @Bean(BEAN_DATASOURCE_ORACLE)
    @ConfigurationProperties(prefix="spring.datasource.oracle")
    public DataSource dataSourceOracle() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = BEAN_JDBC_TEMPLATE_ORACLE)
    public JdbcTemplate jdbcTemplateOracle(@Qualifier(BEAN_DATASOURCE_ORACLE) DataSource dataSource) {
     return new JdbcTemplate(dataSource);
    }

    @Bean(BEAN_DATASOURCE_POSTGRES)
    @Primary
    @ConfigurationProperties(prefix="spring.datasource.postgres")
    public DataSource dataSourcePostgres() {
        return DataSourceBuilder.create().build();
    }

    @Bean(BEAN_JDBC_TEMPLATE_POSTGRES)
    public JdbcTemplate jdbcTemplatePostgres(@Qualifier(BEAN_DATASOURCE_POSTGRES) DataSource dataSource) {
     return new JdbcTemplate(dataSource);
    }

}

.properties上的设置


# spring.datasource.protgres is the same as ConfigurationProperties(prefix=

spring.datasource.postgres.url=jdbc:postgres://localhost:0000/database
spring.datasource.postgres.jdbcUrl=${spring.datasource.postgres.url}
spring.datasource.postgres.username=user
spring.datasource.postgres.password=pass
spring.datasource.postgres.driver-class-name=your.driverPostgres

# optional config to poll connection as you need

spring.datasource.postgres.max-lifetime=30000
spring.datasource.postgres.connection-timeout=30000
spring.datasource.postgres.maximum-pool-size=2
spring.datasource.postgres.pool-name=postgresds

# spring.datasource.oracle is the same as ConfigurationProperties(prefix=

spring.datasource.oracle.url=jdbc:oracle:thin:@localhost:0000:database
spring.datasource.oracle.jdbcUrl=${spring.datasource.oracle.url}
spring.datasource.oracle.username=user
spring.datasource.oracle.password=pass
spring.datasource.oracle.driver-class-name=oracle.jdbc.OracleDriver

# optional config to poll connection as you need

spring.datasource.oracle.max-lifetime=30000
spring.datasource.oracle.connection-timeout=30000
spring.datasource.oracle.maximum-pool-size=2
spring.datasource.oracle.pool-name=oracleds

在服务或dao上使用

@Qualifier(ConfigDataSource.BEAN_JDBC_TEMPLATE_POSTGRES)
    @Autowired(required=false)
    private JdbcTemplate jdbcTemplatePostgres;

    @Qualifier(ConfigDataSource.BEAN_JDBC_TEMPLATE_ORACLE)
    @Autowired(required=false)
    private JdbcTemplate jdbcTemplateOracle;

    public save(Object objToSave){

        if( /* postgres */ ){
            jdbcTemplatePostgres.execute(...);

        }else{
            jdbcTemplateOracle.execute(...);
        }

    }

相关问题