Configuring Multiple Data Sources in Spring Boot with MyBatis
This article explains the underlying principles and step‑by‑step configuration for integrating Spring Boot with MyBatis to manage multiple data sources, covering BeanFactory, FactoryBean, SqlSessionFactoryBean setup, datasource properties, mapper scanning, and sample controller code for accessing distinct databases.
Starting from BeanFactory, the article explains how Spring integrates MyBatis by injecting MyBatis beans via Spring's FactoryBean, with SqlSessionFactoryBean being the key component.
It then describes how to configure Spring Boot to use multiple data sources, defining separate datasource properties (primary and slave) and replacing the default datasource configuration.
Key dependencies include spring-boot-starter-web, mybatis-spring-boot-starter, mysql-connector-java, and druid-spring-boot-starter.
The project structure contains separate DAO packages for each datasource and a configuration class that creates DataSource, SqlSessionFactory, and SqlSessionTemplate beans for each source.
@Configuration
@MapperScan(basePackages = "com.example.dao1", sqlSessionFactoryRef = "sqlSessionFactory1")
public class DataSource1Config {
@Bean
@ConfigurationProperties(prefix = "spring.primary")
public DataSource dataSourcePrimary() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory sqlSessionFactory1() throws Exception {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSourcePrimary());
String locationPattern = "classpath*:/mapperPrimary/*.xml";
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
sessionFactoryBean.setMapperLocations(resolver.getResources(locationPattern));
return sessionFactoryBean.getObject();
}
@Bean(name = "sqlSessionTemplate1")
public SqlSessionTemplate sqlSessionTemplate1(@Qualifier("sqlSessionFactory1") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}A similar configuration is created for the secondary datasource (slave).
@RestController
@RequestMapping("/data")
public class DataSourceController {
@Autowired
private User1Mapper userMapper;
@Autowired
private User2Mapper user2Mapper;
@GetMapping("/get-user1/{name}")
public User getUserFromTestDb(@PathVariable("name") String userName) {
return userMapper.selectUser(userName);
}
@GetMapping("/get-user2/{name}")
public User getUserFromTestDb2(@PathVariable("name") String userName) {
return user2Mapper.selectUser(userName);
}
}The article concludes with a summary of the main components: DataSource, SqlSessionFactory, @MapperScan, and the overall flow for integrating Spring Boot with MyBatis to handle multiple databases.
Rare Earth Juejin Tech Community
Juejin, a tech community that helps developers grow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.