我在linux服务器上有mysql服务器。它包含5个不同客户端的5个数据库。到目前为止,我创建了5个tomcat实例,基于客户端部署5个war,并从servelet.xml文件访问数据库,如下所示,
client1实例战:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/client1" />
<property name="username" value="rootuser" />
<property name="password" value="pswd" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" >
<constructor-arg ref="dataSource"/>
</bean>
client2实例战:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/client2" />
<property name="username" value="rootuser" />
<property name="password" value="pswd" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" >
<constructor-arg ref="dataSource"/>
</bean>
配置类:
public class JdbcRepository {
@Autowired
@Qualifier("jdbcTemplate")
protected JdbcTemplate jdbcTemplate;
@Autowired
@Qualifier("namedParameterJdbcTemplate")
protected NamedParameterJdbcTemplate namedParameterJdbcTemplate ;
@Autowired
@Qualifier("dataSource")
private DataSource dataSource; //mysqlSource;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() {
return namedParameterJdbcTemplate;
}
public void setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
jdbcTemplate = new JdbcTemplate(this.dataSource);
}
public Long findMemberIdByUserName(String username) {
try{
String sql = "SELECT userLoginId FROM user WHERE userName = ?";
Long id = jdbcTemplate.queryForObject(sql, new Object[]{username}, Long.class);
return id;
}
catch(Exception e){
return 0l;
}
}
但是现在我想创建单个实例并为所有客户端部署单一战争并根据用户登录访问数据库。如下所示,
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/{DBBasedOnLoginUser}" />
<property name="username" value="rootuser" />
<property name="password" value="pswd" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate" >
<constructor-arg ref="dataSource"/>
</bean>
如何实现这一目标?