There are many ways to configure multiple data sources to connect to the database. Two common ones are briefly introduced:
1. The first way
MybatisPlus comes with multiple data source functions:
Use @DS to switch data sources
@DS can be annotated on methods or classes, and there is a principle of proximity. Method annotations take precedence over class annotations .
annotation | result |
---|---|
No @DS | Default data source |
@DS("dsName") | dsName can be a group name or a specific library name |
@Service
@DS("slave")
public class UserServiceImpl implements UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
public List selectAll() {
return jdbcTemplate.queryForList("select * from user");
}
@Override
@DS("slave_1")
public List selectByCondition() {
return jdbcTemplate.queryForList("select * from user where age >10");
}
}
This method feels more cumbersome to match, and each class that needs to be sub-library needs to be configured.
2. The second way
Configure data source (mybatis configuration method)
@Configuration
@MapperScan(basePackages = "com.*.dao", sqlSessionTemplateRef = "serviceSqlSessionTemplate")
public class ServiceDataSourceConfig {
@Bean("serviceDataSource")
@ConfigurationProperties("spring.datasource.service")
public HikariDataSource dataSource() {
return new HikariDataSource();
}
/**
* 创建SessionFactory
*/
@Bean(name = "serviceSqlSessionFactory")
public SqlSessionFactory serviceSqlSessionFactory(@Qualifier("serviceDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//设置mapper配置文件
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:sqlmap/*.xml"));
return bean.getObject();
}
/**
* 创建事务管理器
*/
@Bean("serviceTransactionManger")
public DataSourceTransactionManager serviceTransactionManger(@Qualifier("serviceDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
/**
* 创建SqlSessionTemplate
*/
@Bean(name = "serviceSqlSessionTemplate")
public SqlSessionTemplate serviceSqlSessionTemplate(@Qualifier("serviceSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
Configure data source (MybatisPlus configuration method)
@Configuration
@MapperScan(basePackages = "com.*.dao", sqlSessionTemplateRef = "serviceSqlSessionTemplate")
public class ServiceDataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource.service")
public HikariDataSource serviceDataSource() {
return new HikariDataSource();
}
/**
* 创建SessionFactory
*/
@Bean
@Primary
public SqlSessionFactory serviceSqlSessionFactory(@Qualifier("serviceDataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
//设置mapper配置文件
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:sqlmap/*.xml"));
return bean.getObject();
}
/**
* 创建事务管理器
*/
@Bean
@Primary
public DataSourceTransactionManager serviceTransactionManger(@Qualifier("serviceDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
/**
* 创建SqlSessionTemplate
*/
@Bean
@Primary
public SqlSessionTemplate serviceSqlSessionTemplate(@Qualifier("serviceSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
The difference is that when creating the SessionFactory,
my shoe:
SqlSessionFactoryBean
MybatisPlus:
MybatisSqlSessionFactoryBean
If you use MybatisPlus, the data source is equipped with SqlSessionFactoryBean,
Then it will report Invalid bound statement (not found)