Multiple Data Source Configuration
Project structure
Recent project uses multiple data sources, where the record about the specific configuration information.
Currently two databases, and PaaS robot, according to the database, the project is divided into the packet structure shown below, to facilitate the management and maintenance after.
Project Configuration
The main configuration file 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
# Connection pool configuration 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
@MapperScan (basePackages = "com.huayunworld.parser.flowChart.dao.robot" , sqlSessionFactoryRef = "robotSqlSessionFactory" )public class { 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
@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 { @Resource private AttrMapper attrMapper; @Resource private CallRecBotMapper callRecBotMapper; @Test public void allDataSourceSelectTest () { System.out.println (attrMapper.selectAllData ()); System.out.println (callRecBotMapper.selectAllData ()); }
Output:
Test can be seen, the two libraries are connected to success.
Original: Big Box springboot mybatis druid multi data source configuration