zoukankan      html  css  js  c++  java
  • 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文件。

  • 相关阅读:
    背包问题
    计蒜客lev3
    线段树BIT操作总结
    图论题收集
    Codeforces Round #607 (Div. 2) 训练总结及A-F题解
    2-sat 学习笔记
    洛谷 P3338 【ZJOI2014】力/BZOJ 3527 力 题解
    $noi.ac$ #51 array 题解
    洛谷 P3292 【SCOI2016】幸运数字/BZOJ 4568 幸运数字 题解
    洛谷 P5283 【十二省联考2019】异或粽子 题解
  • 原文地址:https://www.cnblogs.com/sonofelice/p/6435317.html
Copyright © 2011-2022 走看看