springboot mybatis druid多数据源配置

白昼怎懂夜的黑 提交于 2019-11-30 21:04:09

多数据源配置


项目结构

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

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

项目配置

主配置文件 application.properties

1234567891011121314151617181920212223242526272829303132333435
#连接池配置spring.datasource.type=com.alibaba.druid.pool.DruidDataSourcespring.datasource.driverClassName=com.mysql.jdbc.Driver#robot库spring.datasource.robot.url=jdbc:mysql://localhost:3306/robot?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=truespring.datasource.robot.username=rootspring.datasource.robot.password=spring.datasource.robot.initialSize=1spring.datasource.robot.maxActive=20spring.datasource.robot.minIdle=1spring.datasource.robot.maxWait=60000spring.datasource.robot.timeBetweenEvictionRunsMillis=60000spring.datasource.robot.minEvictableIdleTimeMillis=300000spring.datasource.robot.validationQuery=select 1spring.datasource.robot.testWhileIdle=true#pass库spring.datasource.paas.url=jdbc:mysql://localhost:3306/paas?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=truespring.datasource.paas.username=rootspring.datasource.paas.password=spring.datasource.paas.initialSize=1spring.datasource.paas.maxActive=20spring.datasource.paas.minIdle=1spring.datasource.paas.maxWait=60000spring.datasource.paas.timeBetweenEvictionRunsMillis=60000spring.datasource.paas.minEvictableIdleTimeMillis=300000spring.datasource.paas.validationQuery=select 1spring.datasource.paas.testWhileIdle=true#实体类扫描路径mybatis.type-aliases-package=com.huayunworld.parser.flowChart.model.db#mybatis配置文件路径mybatis.config-location=classpath:mybatis-config.xml

数据源robot配置

1234567891011121314151617181920212223242526272829303132
@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配置

1234567891011121314151617181920212223242526272829
@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,连接不同的数据库。

测试代码:

12345678910111213141516171819202122
@RunWith(SpringRunner.class)@SpringBootTest(classes = RbcParserApplication.class)@Transactionalpublic class RbcParserApplicationTests {	    @Resource	private AttrMapper attrMapper;        // paas库	@Resource	private CallRecBotMapper callRecBotMapper;	/**	 * 两个数据库同时测试 查询	 */	@Test	public void allDataSourceSelectTest() {		System.out.println(attrMapper.selectAllData());		System.out.println(callRecBotMapper.selectAllData());	}

输出结果:

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

原文:大专栏  springboot mybatis druid多数据源配置


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!