2014-01-12 82 views
0

我使用spring ibatis連接Oracle多用戶空間。spring3 ibatis2.x multiple datasource

看起來像用戶A,用戶B,用戶系統。

用戶系統始終使用。其他用戶,而在任務(線程)調度研究

spring.xml

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
destroy-method="close"> 
    <property name="driverClassName" value="${jdbc.driverClassName}" /> 
    <property name="url" value="${jdbc.url}" /> 
    <property name="username" value="System" /> 
    <property name="password" value="system" /> 
</bean> 

<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" 
destroy-method="close"> 
    <property name="driverClassName" value="${jdbc.driverClassName}" /> 
    <property name="url" value="${jdbc.url}" /> 
    <property name="username" value="A" /> 
    <property name="password" value="a" /> 
</bean> 

    <bean id="dataSource3" class="org.apache.commons.dbcp.BasicDataSource" 
destroy-method="close"> 
    <property name="driverClassName" value="${jdbc.driverClassName}" /> 
    <property name="url" value="${jdbc.url}" /> 
    <property name="username" value="B" /> 
    <property name="password" value="b" /> 
</bean> 

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> 
    <property name="configLocation" value="classpath:SqlMapConfig.xml" /> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 
<bean id="sqlMapClient1" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> 
    <property name="configLocation" value="classpath:SqlMapConfig.xml" /> 
    <property name="dataSource" ref="dataSource2" /> 
</bean> 

<bean id="sqlMapClient2" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> 
    <property name="configLocation" value="classpath:SqlMapConfig.xml" /> 
    <property name="dataSource" ref="dataSource3" /> 
</bean> 

在道:

public class BaseDao extends SqlMapClientDaoSupport { 
Logger log = Logger.getLogger(getClass()); 

@Resource(name = "sqlMapClient") 
private SqlMapClient sqlMapClient; 


@PostConstruct 
public void initSqlMapClient() { 
    super.setSqlMapClient(sqlMapClient); 
} 
    } 

    public class UserDao extends BaseDao { 
     public void test() 
     { 
      this.getSqlMapClientTemplate().queryForObject("...."); 
     } 
    } 

任務:在taskJob我想調度研究其他datasorce。如何實現它?

public class TaskJob { 

      //@Autowired 
      //UserDao userDao; 
      public void test(){ 
       //to get other sqlmapclient in UserDao 
       userDao.test();//for User A,User B,User .... 
      } 

} 

回答

1

你不能這樣做,使用SqlMapClientDaoSupport。如果你沒有使用SqlMapClientDaoSupport,那麼下面的解決方案就足夠了。

MapClientFactoryBean不應該有數據源

<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> 
    <property name="configLocation" value="classpath:SqlMapConfig.xml" /> 
</bean> 

有兩臺UserDAO的(每個數據源)

<bean id = "userDAO" class = "com.xxxx.UserDAO"> 
    <property name="dataSource" ref="dataSource" /> 
     <property name="sqlMapClient" ref = "sqlMapClient"> 
<bean id = "userDAO1" class = "com.xxxx.UserDAO"> 
    <property name="dataSource2" ref="dataSource" /> 
     <property name="sqlMapClient" ref = "sqlMapClient"> 
</bean> 

參見此處瞭解更多詳情http://www.mail-archive.com/[email protected]/msg04432.html

不可能對代碼進行測試自己。請嘗試。

+0

謝謝,但是這個解決方案太麻煩了。有更簡單的方法嗎?Oracle用戶很多。我需要循環這些用戶的線程 – zt9788

+0

SqlMapClientDaoSupport被設計爲只支持一個SqlMapClient ..所以我真的懷疑你可以用現有的代碼做任何事情。 – ArunM

+0

我可以自己創建DataSource不使用spring(aop),如:for(DataSource dt ...)(new UserDao().set(dt);) – zt9788