zoukankan      html  css  js  c++  java
  • springboot mybatis 多数据源配置支持切换以及一些坑

    一 添加每个数据源的config配置,单个直接默认,多个需要显示写出来

    @Configuration
    @MapperScan(basePackages ="com.zhuzher.*.mapper", sqlSessionTemplateRef  = "crmSqlSessionTemplate")
    public class CrmDataSourceConfig {
        @Bean(name = "crmData")
        @ConfigurationProperties(prefix = "spring.datasource.crm") // application.properteis中对应属性的前缀
        @Primary
        public DataSource crmDataSourceConfig() {
            return DataSourceBuilder.create().build();
        }
    
    
        @Bean(name = "crmSqlSessionFactory")
        @Primary
        public SqlSessionFactory crmSqlSessionFactory() throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(crmDataSourceConfig());
            //这段代码必须要,否则不会识别xml
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappings/*.xml"));
            return bean.getObject();
        }
    
        @Bean(name = "crmSqlSessionTemplate")
        @Primary
        public SqlSessionTemplate crmSqlSessionTemplate() throws Exception {
            return new SqlSessionTemplate(crmSqlSessionFactory());
        }
    }
    @Primary 表示默认的,必须且指定一个,mapper位置也要对应放在不同位置,利用不同的mapper请求不同的数据源数据
    二 属性文件配置多个数据源,对应config文件中前缀

    三 接口引用不同mapper请求不同数据源数据

    四 坑

    1 url需要使用jdbc-url

    2 xml位置需要在每个config显示置顶位置

    3 一定要指定一个默认的数据源,用注解

    @Primary
  • 相关阅读:
    树状数组&线段树
    8月7日小练
    8月6日小练
    LID&LDS 的另外一种算法
    LCS,LIS,LCIS
    8-11-Exercise
    8-10-Exercise
    线段树
    8-7-Exercise
    8-6-Exercise
  • 原文地址:https://www.cnblogs.com/houzheng/p/11767191.html
Copyright © 2011-2022 走看看