目录
在最近的工作中,项目要求在一个工程中访问多个数据源的数据,这部分的配置比较简单,简单记录一下具体的配置。
一、配置
首先,在application配置文件中添加多个数据数据源,如下:
server:
port: 8088
spring:
datasource:
sys:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/spring-boot-demo?useSSL=false&useUnicode=true&characterEncoding=utf8&useAffectedRows=true
username: root
password:
fin:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/spring-boot-demo-1?useSSL=false&useUnicode=true&characterEncoding=utf8&useAffectedRows=true
username: root
password:
这里配置了sys和fin两个数据源,分别对应spring-boot-demo和spring-boot-demo-1数据库。
然后,对这两个数据源进行配置,配置类如下:
@Configuration
@MapperScan(basePackages = "com.greedystar.springbootdemo.modules.fin.dao", sqlSessionTemplateRef = "finSqlSessionTemplate")
public class FinDataSourceConfig {
@Bean(name = "finDataSource")
@ConfigurationProperties(prefix = "spring.datasource.fin")
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
@Bean(name = "finSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("finDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/fin/*Mapper.xml"));
return bean.getObject();
}
@Bean(name = "finTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("finDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "finSqlSessionTemplate")
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("finSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
@Configuration
@MapperScan(basePackages = "com.greedystar.springbootdemo.modules.sys.dao", sqlSessionTemplateRef = "sysSqlSessionTemplate")
public class SysDataSourceConfig {
@Bean(name = "sysDataSource")
@ConfigurationProperties(prefix = "spring.datasource.sys")
@Primary
public DataSource dataSource() {
DruidDataSource dataSource = new DruidDataSource();
return dataSource;
}
@Bean(name = "sysSqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("sysDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/sys/*Mapper.xml"));
return bean.getObject();
}
@Bean(name = "sysTransactionManager")
@Primary
public DataSourceTransactionManager transactionManager(@Qualifier("sysDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "sysSqlSessionTemplate")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sysSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
这里需要注意,在两个数据源的配置中要特别区分DAO接口的包路径和映射文件的路径,当我们使用不同的DAO对象时便会访问不同的数据源。
二、测试
其他代码都是使用工具 Generator 生成的,就不贴出来了,下面测试一下多数据源的使用:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = SpringBootDemoApplication.class)
public class SpringBootDemoApplicationTests {
@Autowired
private UserDao userDao; // spring-boot-demo
@Autowired
private RoleDao roleDao; // spring-boot-demo-1
@Test
public void test() {
System.out.println(JSON.toJSONString(userDao.findAllList()));
System.out.println(JSON.toJSONString(roleDao.findAllList()));
}
}
控制台打印如下:
可以看到初始化了两个数据源,并访问不同的数据源查询了数据。