title: Spring boot Mybatis multiple datasource
body: <p>I am using <code>Spring boot 2.0.3</code> and <code>mybatis</code> with <code>PostgreSql</code>.</p>
  
  <p>I am trying to set up multiple data source connection as follows by following <a href="https://programmer.help/blogs/spring-boot-integrates-mybatis-multiple-data-sources.html" rel="nofollow noreferrer">https://programmer.help/blogs/spring-boot-integrates-mybatis-multiple-data-sources.html</a>.</p>
  
  <h1>Datasource1</h1>
  
  <pre><code>@Configuration
  @MapperScan(basePackages = "com.repositories.StaRepository", sqlSessionFactoryRef = "sqlPromptSessionFactory", annotationClass = Mapper.class)
  //SqlSessionFactory is created from DB1 and then a SqlSessionTemplate is created from the created SqlSessionFactory.
  public class MyBatisConfigPrompt {
  
      @Bean(name = "DB1")
      @ConfigurationProperties(prefix = "spring.datasource.pro")
      public DruidDataSource DB1() {
          return DruidDataSourceBuilder.create().build();
      }
  
      @Bean(name = "sqlProSessionFactory")
      SqlSessionFactory sqlProSessionFactory() {
          SqlSessionFactory sessionFactory = null;
          try {
              SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
              bean.setDataSource(DB1());
              sessionFactory = bean.getObject();
          } catch (Exception e) {
              e.printStackTrace();
          }
          return sessionFactory;
      }
  
      @Bean
      public MapperScannerConfigurer proMapperScannerConfigurer() {
          MapperScannerConfigurer configurer = new MapperScannerConfigurer();
          configurer.setBasePackage("com.repositories.StaRepository");
          configurer.setSqlSessionFactoryBeanName("sqlProSessionFactory");
          return configurer;
      }
  
  }
  </code></pre>
  
  <h1>Datasource2</h1>
  
  <pre><code>@Configuration
  @MapperScan(basePackages = "com.repositories.ContDBRepository", sqlSessionFactoryRef = "sqlContSessionFactory", annotationClass = Mapper.class)
  //SqlSessionFactory is created from contDB and then a SqlSessionTemplate is created from the created SqlSessionFactory.
  public class MyBatisConfigCont {
      @Bean(name = "contDB")
      @ConfigurationProperties(prefix = "spring.datasource.cont")
      public DruidDataSource contDB() {
          return DruidDataSourceBuilder.create().build();
      }
  
      @Bean(name = "sqlContSessionFactory")
      SqlSessionFactory sqlContSessionFactory() {
          SqlSessionFactory sessionFactory = null;
          try {
              SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
              bean.setDataSource(contDB());
              sessionFactory = bean.getObject();
          } catch (Exception e) {
              e.printStackTrace();
          }
          return sessionFactory;
      }
  
      @Bean
      public MapperScannerConfigurer contMapperScannerConfigurer() {
          MapperScannerConfigurer configurer = new MapperScannerConfigurer();
          configurer.setBasePackage("com.repositories.ContDBRepository");
          configurer.setSqlSessionFactoryBeanName("sqlContSessionFactory");
          return configurer;
      }
  }
  </code></pre>
  
  <p>I have also a <code>ContDBRepository.class</code> with <code>@Mapper</code> Annotation and <code>ContDBRepository.xml</code> and same as <code>StaRepository.class</code> with <code>@Mapper</code> Annotation and <code>StaRepository.xml</code> in same package.</p>
  
  <p>With the above configuration i am getting <strong>ERROR</strong></p>
  
  <pre><code>No qualifying bean of type 'org.apache.ibatis.session.SqlSessionFactory' available: expected single matching bean but found 2: sqlContSessionFactory,sqlProSessionFactory
  </code></pre>
  
  <p>As a fix to the above error i set <code>@Primary</code> to one of the <code>SqlSessionFactory</code> but other <code>SqlSessionFactory</code> is never called when i want to use second datasource.</p>
  
  <p>Can anyone help what i am missing. </p>