I am using Spring boot 2.0.3 and mybatis with PostgreSql.
I am trying to set up multiple data source connection as follows by following https://programmer.help/blogs/spring-boot-integrates-mybatis-multiple-data-sources.html.
Datasource1
@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;
}
}
Datasource2
@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;
}
}
I have also a ContDBRepository.class with @Mapper Annotation and ContDBRepository.xml and same as StaRepository.class with @Mapper Annotation and StaRepository.xml in same package.
With the above configuration i am getting ERROR
No qualifying bean of type 'org.apache.ibatis.session.SqlSessionFactory' available: expected single matching bean but found 2: sqlContSessionFactory,sqlProSessionFactory
As a fix to the above error i set @Primary to one of the SqlSessionFactory but other SqlSessionFactory is never called when i want to use second datasource.
Can anyone help what i am missing.
@Primaryto the@Beanmethods in the primary configuration. You also need to remove the methods returningMapperScannerConfigureras there is@MapperScanalready (this might be the reason why adding@Primarydidn't work).MapperScannerConfigurerbasePackages. Have you verified if one data source setup works as you expect? Anyway, here is a portable demo project.