Spring 和 Mybatis 多数据源设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4746766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 04:17:03  来源:igfitidea点击:

Spring and Mybatis multiple data sources setup

springdatasourcemybatis

提问by vkolodrevskiy

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

我的应用程序使用 Spring3+MyBatis3。我正在尝试为其设置多个数据源。设置看起来像:

<!-- 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 data sources with mybatis. Any ideas?

我用谷歌搜索并查看了 mybatis 手册,但找不到如何使用 mybatis 设置多个数据源的方法。有任何想法吗?

采纳答案by vkolodrevskiy

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

解决了,问题是我必须直接指定对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"/>

回答by user2144996

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

也解决了!只需在 MapperScannerConfigurer 中引用您的工厂 bean:s​​qlSessionFactoryBeanName

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>

回答by Shuster

In a DAO implementation use SqlSessionTemplateinstead of SqlSessionDaoSupport. Inject bean db1SqlSessionTemplateor db2SqlSessionTemplate.

在 DAO 实现中使用SqlSessionTemplate而不是SqlSessionDaoSupport. 注入 beandb1SqlSessionTemplatedb2SqlSessionTemplate.

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

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

当扩展SqlSessionDaoSupport上下文时,Spring 不知道您使用SqlSession.