spring3 ibatis2.x多个数据源

时间:2014-01-12 10:14:07

标签: java oracle spring-mvc datasource ibatis

我使用spring ibatis连接Oracle多用户空间。

好像是用户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中我想要操作其他数据。如何实施?

public class TaskJob {

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

}

1 个答案:

答案 0 :(得分: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/user-java@ibatis.apache.org/msg04432.html

无法自己测试代码。请试试。