Spring-Mybatis配置多数据源

可以参考: http://www.cnblogs.com/ityouknow/p/6102399.html

需要一个DatabaseConfiguration类,实现 TransactionManagementConfigurer 接口。

针对每一个数据源的配置主要分为3步:

1. 配置DataSource

2.配置创建sqlSessionFactoryBean的方法,来生成对应该数据源的bean

3.配置mapper(dao)扫描的包

值得注意的是,该配置类需要制定一个主数据源,一般用@Primary注解

下面是代码概览:

/**
 * Created by sonofelice on 17/2/23.
 */
@Configuration
@Slf4j
public class DatabaseConfiguration implements TransactionManagementConfigurer {

    /*------------------------ test1 db -------------------------------*/
    @Value("${test1.isEmbedded:true}")
    Boolean test1DatabaseIsEmbedded;

    @Value("${test1.database.url:}")
    String test1DatabaseUrl;

    @Value("${test1.database.username:}")
    String test1DatabaseUsername;

    @Value("${test1.database.password:}")
    String test1DatabasePassword;

    @Bean(name = "test1DataSource")
    public DataSource test1DataSource() {
        DataSourceConfig config = new DataSourceConfig()
                                          .withIsEmbedded(test1DatabaseIsEmbedded)
                                          .withDatabaseUrl(test1DatabaseUrl)
                                          .withDatabaseUsername(test1DatabaseUsername)
                                          .withDatabasePassword(test1DatabasePassword);
        return DataSourceGenerator.getDataSource(config);
    }

    @Bean(name = "test1SqlSessionFactory")
    public SqlSessionFactoryBean test1SqlSessionFactory() throws Exception {
        return new CustomSqlSessionFactoryBean(test1DataSource());
    }

    @Configuration
    @MapperScan(basePackages = "com.test1.mapper.stat",
            sqlSessionFactoryRef = "test1SqlSessionFactory")
    @Primary
    public static class Test1MapperConfiguration {

    }

    /*------------------------ test2 db -------------------------------*/
    @Value("${test2.database.isEmbedded:true}")
    Boolean test2DatabaseIsEmbedded;

    @Value("${test2.database.url:}")
    String test2DatabaseUrl;

    @Value("${test2.database.username:}")
    String test2DatabaseUsername;

    @Value("${test2.database.password:}")
    String test2DatabasePassword;

    @Bean(name = "test2DataSource")
    public DataSource mktDataSource() {
        DataSourceConfig config = new DataSourceConfig()
                                          .withIsEmbedded(test2DatabaseIsEmbedded)
                                          .withDatabaseUrl(test2DatabaseUrl)
                                          .withDatabaseUsername(test2DatabaseUsername)
                                          .withDatabasePassword(test2DatabasePassword);
        return DataSourceGenerator.getDataSource(config);
    }

    @Bean(name = "test2SqlSessionFactoryBean")
    public SqlSessionFactoryBean test2SqlSessionFactory() throws Exception {
        return new CustomSqlSessionFactoryBean(test2DataSource());
    }

    @Configuration
    @MapperScan(basePackages = "com.test2.mapper.mkt",
            sqlSessionFactoryRef = "test2SqlSessionFactoryBean")
    public static class MktMapperConfiguration {

    }

/*------------------------ end --------------------------------*/

    @Override
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        return txManager();
    }

    public PlatformTransactionManager txManager() {
        return new DataSourceTransactionManager(test2StatDataSource());
    }
}

配置好了之后就可以用了。不需要配置xml文件。

 

posted @ 2017-02-23 20:35  SonoFelice  阅读(375)  评论(0编辑  收藏  举报