The MyBatis annotation method for configuring multiple data sources is similar to the XML method except that the annotation method does not specify the location of the XML file.

Configuring Data Source Information

  • application.properties
# data source one spring. The datasource. Resource1. Driver - class - name = com. Mysql.. JDBC driver # distinguish spring. The datasource. Url with custom JDBC configuration jdbc-url spring.datasource.resource1.jdbc-url=jdbc:mysql://localhost:3306/game? useUnicode=true&characterEncoding=utf-8 spring.datasource.resource1.username=root Spring. The datasource. Resource1. Password = root # 2 spring data source. The datasource. Resource2. The driver - class - name = com. Mysql.. JDBC driver spring.datasource.resource2.jdbc-url=jdbc:mysql://localhost:3306/bookstore? useUnicode=true&characterEncoding=utf-8 spring.datasource.resource2.username=root spring.datasource.resource2.password=rootCopy the code

Configuring multiple Data Sources

The steps for integrating MyBatis configuration data source are as follows:

  • Creating a data sourceDatasource
  • Create a Session Session factorySqlSessionFactroy
  • Create a database transactionDataSourceTransactionManager
  • createSqlSessionTemplate

The key place this part is the data source configuration, need one layer injection, first create a DataSource, create SqlSessionFactory, then create a transaction manager DataSourceTransactionManager, Finally wrapped in the SqlSessionTemplate.

A data source

@Configuration
@MapperScan(basePackages = "com.example.mybatis.mapper.primary",
        sqlSessionFactoryRef = "PrimarySqlSessionFactory")
public class PrimaryDataSourceConfig {

    /** * Configure the data source **@return* /
    @Bean(name = "PrimaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DataSource dataSource(a) {
        return DataSourceBuilder.create().build();
    }

    /** * Configure the SQL session factory */
    @Bean(name = "PrimarySqlSessionFactory")
    public SqlSessionFactory sessionFactory(@Qualifier("PrimaryDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);

        return bean.getObject();
    }

    /** * transaction manager */
    @Bean(name = "PrimaryTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("PrimaryDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "PrimarySqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("PrimarySqlSessionFactory") SqlSessionFactory sessionFactory) {
        return newSqlSessionTemplate(sessionFactory); }}Copy the code

The data source 2

@Configuration
@MapperScan(basePackages = "com.example.mybatis.mapper.secondary",
        sqlSessionFactoryRef = "SecondarySqlSessionFactory")
public class SecondaryDataSourceConfig {

    /** * Configure the data source */
    @Bean(name = "SecondaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.secondary")
    public DataSource dataSource(a) {
        return DataSourceBuilder.create().build();
    }

    /** * Configure the SQL session factory */
    @Bean(name = "SecondarySqlSessionFactory")
    public SqlSessionFactory sessionFactory(@Qualifier("SecondaryDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);

        return bean.getObject();
    }

    /** * transaction manager */
    @Bean(name = "SecondaryTransactionManager")
    public DataSourceTransactionManager transactionManager(@Qualifier("SecondaryDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Bean(name = "SecondarySqlSessionTemplate")
    public SqlSessionTemplate sqlSessionTemplate(@Qualifier("SecondarySqlSessionFactory") SqlSessionFactory sessionFactory) {
        return newSqlSessionTemplate(sessionFactory); }}Copy the code

Note: Before SpringBoot 2.x, you need to specify the primary library with the @primary annotation, otherwise an error will be reported.

Finally, the detailed code can be seen in the Demo of this example.

The source address

springboot-multidatasource-mybatis-annotation