4

With MyBatis-Spring-Boot-Starter, we can easily integrate MyBatis with Spring Boot, it works perfectly for one data source. However, now we'd like to add an extra data source in our project, unfortunately it seems not easy. In MyBatis official documentation, I see the following content:

MyBatis-Spring-Boot-Starter will:

  • Autodetect an existing DataSource.
  • Will create and register an instance of a SqlSessionFactoryBean passing that DataSource as an input.
  • Will create and register an instance of a SqlSessionTemplate got out of the SqlSessionFactoryBean.

It looks like MyBatis-Spring-Boot-Starter supports only one data source at this moment. So, the question is how to configure multiple MyBatis datasources in Sping Boot?

1

3 Answers 3

1

You outlined 3 beans that are needed for MyBatis+Spring integration. These are automatically created for single data source.

If you need two data sources, you need to create 3 beans for each data source explicitly. So you'll be creating 6 beans (2 of type DataSource, 2 of type SqlSessionFactoryBean and 2 of type SqlSessionFactoryBean).

To bind DAO with certain datasource, you will need to use sqlSessionTemplateRef or sqlSessionFactoryRef parameter of @MapperScan annotation.

Also I don't recommend to go down the XML hell. I was using it this way in PROD, with two data sources, without any ugly XML configs on various projects. Also SQL queries were annotated.

Shame is that MyBatis documentation is not great and most examples out there are in XML.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your prompt answer, it's a good idea to use sqlSessionTemplateRef and sqlSessionFactoryRef parameter of @MapperScan annotation, but I still hope I could stick to spring-boot's auto-configuration style
I believe it's not possible if you have two data sources. Spring Boot autoconfig works great with one data source. Spring Boot primarily targets microservice world, where having two data sources is anti-pattern.
0

Something this like this to your spring servlet.xml:

<bean id="db2dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName"><value>${db2.database.driver}</value></property>
            <property name="url"><value>${db2.database.url}</value></property>
            <property name="username"><value>${db2.database.username}</value></property>
            <property name="password"><value>${db2.database.password}</value></property>
            <property name="maxActive"><value>${db2.database.maxactiveconnections}</value></property>
            <property name="maxIdle"><value>${db2.database.idleconnections}</value></property>
            <property name="initialSize"><value>${db2.database.initialSize}</value></property>

    </bean>   

    <bean id="db2SqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="db2dataSource" />
        <property name="configLocation" value="/WEB-INF/mybatis-config.xml"/>
     </bean>

     <bean id="db2Dao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="db2SqlSessionFactory"/> 
        <property name="mapperInterface" value="com.dao.db2Dao" />
    </bean> 

        <bean id="oracledataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName"><value>${oracle.database.driver}</value></property>
            <property name="url"><value>${oracle.database.url}</value></property>
            <property name="username"><value>${oracle.database.username}</value></property>
            <property name="password"><value>${oracle.database.password}</value></property>
            <property name="maxActive"><value>${oracle.database.maxactiveconnections}</value></property>
            <property name="maxIdle"><value>${oracle.database.idleconnections}</value></property>
            <property name="initialSize"><value>${oracle.database.initialSize}</value></property>

    </bean> 
    <bean id="oracleSqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="oracledataSource" />
        <property name="configLocation" value="/WEB-INF/mybatis-config.xml"/>
     </bean>
     <bean id="oracleoardDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="oracleSqlSessionFactory"/>  
        <property name="mapperInterface" value="com.lodige.clcs.dao.oracleoardDao" />
    </bean> 

1 Comment

thanks for your timely response, but this is not spring-boot style, one of spring-boot's greatest magic is auto-configuration, I'm still trying to use minimum configuration to get it work.
0

Maybe this is what you need

@Configuration
@MapperScan(basePackages = "com.neo.mapper.test1", sqlSessionTemplateRef  = 
"test1SqlSessionTemplate")
public class DataSource1Config {

@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test1")
@Primary
public DataSource testDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
    SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    bean.setDataSource(dataSource);
    return bean.getObject();
}

@Bean(name = "test1TransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
    return new DataSourceTransactionManager(dataSource);
}

@Bean(name = "test1SqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    return new SqlSessionTemplate(sqlSessionFactory);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.