一共两个数据源,需要实现两个配置类,另一个去除@Primary即可。
@Configuration @MapperScan(basePackages = {"com.example.demo.mapper.test1"}, sqlSessionTemplateRef = "test1SqlSessionTemplate") public class DataSource1Config { @Bean("test1DataSource") @ConfigurationProperties(prefix = "spring.datasource.test1") public DataSource test1DataSource() { return DataSourceBuilder.create().build(); } @Bean("test1SqlSessionFactory") @Primary public SqlSessionFactory test1SqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/test1/*.xml")); return factoryBean.getObject(); } @Bean("test1TransactionManager") @Primary public DataSourceTransactionManager test1TM(@Qualifier("test1DataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean("test1SqlSessionTemplate") @Primary public SqlSessionTemplate test1SqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory factory) { return new SqlSessionTemplate(factory); } }