因此,我已经从事了Spring Boot项目,现在正在从事数据库工作。我认为最好为数据库设置两个用户:一个可以访问用户表以进行login/register/information更新,另一个可以访问其他表。我的想法是为两个DataSource创建两个单独的bean,每个bean都有一个单独的用户,并且当用户要登录时,控制器将相应地更改JDBCtemplate DataSource。但是我不确定那是否行得通,因为JDBCtemplate已经被定义为一个Spring Boot项目,而且我不知道它的范围(我假设如果它不是会话bean,那么更改DataSource的对象将是所有用户,而不仅仅是一个用户)有谁知道我该如何解决这个问题?请告诉我!
2 回答
Helenr
TA贡献1780条经验 获得超4个赞
您可以创建2个JdbcTemplate bean:
// 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);
}
并在自动装配时指定bean的名称:
// use jdbcTemplate1 for login/register/information
@Autowired
@Qualifier("jdbc1")
protected JdbcTemplate jdbcTemplate1;
// use jdbcTemplate2 for other
@Autowired
@Qualifier("jdbc2")
protected JdbcTemplate jdbcTemplate2;
添加回答
举报
0/150
提交
取消