springboot mybatis druid多数据源配置 | 张扎瓦的博客

springboot mybatis druid多数据源配置

多数据源配置


项目结构

最近项目用到了多数据源,在此处记录一下具体的配置信息。

目前是两个数据库,paas 和 robot,根据数据库,将项目的包结构分为如下图所示,方便管理及后期维护。

项目图片

项目配置

主配置文件 application.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#连接池配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driverClassName=com.mysql.jdbc.Driver

#robot库
spring.datasource.robot.url=jdbc:mysql://localhost:3306/robot?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.robot.username=root
spring.datasource.robot.password=
spring.datasource.robot.initialSize=1
spring.datasource.robot.maxActive=20
spring.datasource.robot.minIdle=1
spring.datasource.robot.maxWait=60000
spring.datasource.robot.timeBetweenEvictionRunsMillis=60000
spring.datasource.robot.minEvictableIdleTimeMillis=300000
spring.datasource.robot.validationQuery=select 1
spring.datasource.robot.testWhileIdle=true

#pass库
spring.datasource.paas.url=jdbc:mysql://localhost:3306/paas?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.paas.username=root
spring.datasource.paas.password=
spring.datasource.paas.initialSize=1
spring.datasource.paas.maxActive=20
spring.datasource.paas.minIdle=1
spring.datasource.paas.maxWait=60000
spring.datasource.paas.timeBetweenEvictionRunsMillis=60000
spring.datasource.paas.minEvictableIdleTimeMillis=300000
spring.datasource.paas.validationQuery=select 1
spring.datasource.paas.testWhileIdle=true


#实体类扫描路径
mybatis.type-aliases-package=com.huayunworld.parser.flowChart.model.db
#mybatis配置文件路径
mybatis.config-location=classpath:mybatis-config.xml

数据源robot配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Configuration
@MapperScan(basePackages = "com.huayunworld.parser.flowChart.dao.robot", sqlSessionFactoryRef = "robotSqlSessionFactory")
public class RobotDruidConfig {

private static final String MAPPER = "classpath:mapping/robot/*.xml";


@ConfigurationProperties("spring.datasource.robot")
@Primary
@Bean(name = "robotDataSource")
public DruidDataSource robotDataSource() {
return new DruidDataSource();
}


@Bean(name = "robotTransactionManager")
@Primary
public DataSourceTransactionManager robotTransactionManager() {
return new DataSourceTransactionManager(robotDataSource());
}


@Bean(name = "robotSqlSessionFactory")
@Primary
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("robotDataSource") DataSource dataSource) throws Exception {
final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER));
return sessionFactoryBean.getObject();
}

}

数据源paas配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
@Configuration
@MapperScan(basePackages = "com.huayunworld.parser.flowChart.dao.paas", sqlSessionFactoryRef = "paasSqlSessionFactory")
public class PaasDruidConfig {

private static final String MAPPER = "classpath:mapping/paas/*.xml";


@ConfigurationProperties("spring.datasource.paas")
@Bean(name = "paasDataSource")
public DruidDataSource paasDataSource() {
return new DruidDataSource();
}


@Bean(name = "paasTransactionManager")
public DataSourceTransactionManager masterTransactionManager() {
return new DataSourceTransactionManager(paasDataSource());
}


@Bean(name = "paasSqlSessionFactory")
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("paasDataSource") DataSource dataSource) throws Exception {
final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER));
return sessionFactoryBean.getObject();
}

}

测试

使用时,与之前的mybatis的使用方法一样,直接注入dao层对象即可。spring会根据不同包下的dao,连接不同的数据库。

测试代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

@RunWith(SpringRunner.class)
@SpringBootTest(classes = RbcParserApplication.class)
@Transactional
public class RbcParserApplicationTests {
// robot库
@Resource
private AttrMapper attrMapper;

// paas库
@Resource
private CallRecBotMapper callRecBotMapper;


/**
* 两个数据库同时测试 查询
*/
@Test
public void allDataSourceSelectTest() {
System.out.println(attrMapper.selectAllData());
System.out.println(callRecBotMapper.selectAllData());
}

输出结果:

测试代码

通过测试可以看出,两个库分别连接成功了。

如果我的文章对您有所帮助,不妨打赏一杯豆浆以资鼓励(○` 3′○)