Use two datasources with jdbc template

前端 未结 2 717
情话喂你
情话喂你 2021-01-19 04:56

So, I\'ve worked on a spring boot project and now I am working on the database. I thought that it would be best to set two users for the database:

one that has acce

相关标签:
2条回答
  • 2021-01-19 05:11

    First you should config two data source. Connect different data sources according to different operations.

    0 讨论(0)
  • 2021-01-19 05:21

    You can create 2 JdbcTemplate beans:

    // declare
    @Bean("jdbc1")
    public JdbcTemplate createJdbcTemplate1(@Autowired @Qualifier("datasource1") DataSource dataSource1){
        return new JdbcTemplate(dataSource1);
    }
    
    @Bean("jdbc2")
    public JdbcTemplate createJdbcTemplate2(@Autowired @Qualifier("datasource2") DataSource dataSource2){
        return new JdbcTemplate(dataSource2);
    }
    

    and specify name of bean when autowiring:

    // use jdbcTemplate1 for login/register/information
    @Autowired
    @Qualifier("jdbc1")
    protected JdbcTemplate jdbcTemplate1;
    
    // use jdbcTemplate2 for other
    @Autowired
    @Qualifier("jdbc2")
    protected JdbcTemplate jdbcTemplate2;
    
    0 讨论(0)
提交回复
热议问题