开发者

Spring and Mybatis multiple data sources setup

开发者 https://www.devze.com 2023-02-06 05:56 出处:网络
My applications uses Spring3+MyBatis3. I\'m trying to setup multiple data source for it. Setup looks like:

My applications uses Spring3+MyBatis3. I'm trying to setup multiple data source for it. Setup looks like:

<!-- db1 setup-->
<bean id="db1SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
    p:configLocation="WEB-INF/mybatis/sqlMapConfig.xml"
    p:dataSource-ref="db1DataSource" />
<bean id="db1SqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg ref="db1SqlSessionFactory"/>
</bean>
<!-- db2 setup -->
<bean id="db2SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
    p:configLocation="WEB-INF/mybatis/sqlMapConfig.xml"
    p:dataSource-ref="db2DataSource" />
<bean id="db2SqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg ref="db2SqlSessionFactory"/>
</bean>

In the logs, I've found this message:

No unique bean of type [org.apache.ibatis.session.SqlSessionFactory] is defined: expected single matching bean but found 2: [db1SqlSessionFactory, db2SqlSessionFactory]

I googled and looked into mybatis manuals but couldn't find way how to setup multiple da开发者_如何学编程ta sources with mybatis. Any ideas?


also solved ! just reference your factory bean in MapperScannerConfigurer : sqlSessionFactoryBeanName

First data source >>>>>>>

<bean id="dataSource1" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">

<bean id="sqlSessionFactory1" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource1"/>
</bean>

<bean id="MapperScannerConfigurer1" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.package.p1"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory1"/>
    </bean>

Second data source >>>>>>

<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
<bean id="sqlSessionFactory2" class="org.mybatis.spring.SqlSessionFactoryBean">
            <property name="dataSource" ref="dataSource2"/>
    </bean>
<bean id="MapperScannerConfigurer1" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <property name="basePackage" value="com.package.p2"/>
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory2"/>
</bean>


solved, the problem was that I must specify directly reference to sqlSessionFactory

<bean id="myDao" class="org.mybatis.spring.mapper.MapperFactoryBean"
    p:sqlSessionTemplate-ref="db1SqlSessionTemplate"
    p:mapperInterface="my.project.domain.dao.MyDao"
    p:sqlSessionFactory-ref="db1SqlSessionFactory"/>


In a DAO implementation use SqlSessionTemplate instead of SqlSessionDaoSupport. Inject bean db1SqlSessionTemplate or db2SqlSessionTemplate.

@Repository
public class TestDaoImpl implements TestDao{
    @Autowired
    private SqlSession db1SqlSessionTemplate;
    ...
    db1SqlSessionTemplate.selectList("testSelect");
    ...
}

When extending SqlSessionDaoSupport the context Spring does not know that you use SqlSession.

0

精彩评论

暂无评论...
验证码 换一张
取 消