主數據源配置spring
@Configuration @MapperScan(basePackages = "com.somta.springboot.dao.master", sqlSessionTemplateRef = "masterSqlSessionTemplate") public class MasterDataSourceConfiguration { @Value("${spring.datasource.master.driver-class-name}") private String driverClassName; @Value("${spring.datasource.master.url}") private String url; @Value("${spring.datasource.master.username}") private String username; @Value("${spring.datasource.master.password}") private String password; @Bean(name = "masterDataSource") @Primary public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(this.driverClassName); dataSource.setUrl(this.url); dataSource.setUsername(this.username); dataSource.setPassword(this.password); return dataSource; } @Bean(name = "masterSqlSessionFactory") @Primary public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/master/**/Mysql_*Mapper.xml")); return bean.getObject(); } @Bean(name = "masterTransactionManager") @Primary public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "masterSqlSessionTemplate") @Primary public SqlSessionTemplate sqlSessionTemplate(@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
從數據源配置sql
@Configuration @MapperScan(basePackages = "com.somta.springboot.dao.slaver", sqlSessionTemplateRef = "slaverSqlSessionTemplate") public class SlaverDataSourceConfiguration { @Value("${spring.datasource.slaver.driver-class-name}") private String driverClassName; @Value("${spring.datasource.slaver.url}") private String url; @Value("${spring.datasource.slaver.username}") private String username; @Value("${spring.datasource.slaver.password}") private String password; @Bean(name = "slaverDataSource") public DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(this.driverClassName); dataSource.setUrl(this.url); dataSource.setUsername(this.username); dataSource.setPassword(this.password); return dataSource; } @Bean(name = "slaverSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("slaverDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mybatis/slaver/**/Mysql_*Mapper.xml")); return bean.getObject(); } @Bean(name = "slaverTransactionManager") public DataSourceTransactionManager transactionManager(@Qualifier("slaverDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "slaverSqlSessionTemplate") public SqlSessionTemplate sqlSessionTemplate(@Qualifier("slaverSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
測試類springboot
@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) public class MultiDatasourceTest { @Autowired private UserMasterDao masterUserDao; @Autowired private UserSlaverDao slaverUserDao; /** * 查詢用戶 * @throws Exception */ @Test public void testQueryUser() throws Exception { User masterUser = masterUserDao.queryUserById(1L); System.out.println("masterUser==>"+masterUser.getName()); User slaverUser = slaverUserDao.queryUserById(1L); System.out.println("slaverUser==>"+slaverUser.getName()); } }