springboot+mybatis多数据源配置,AOP注解动态切换数据源

转载至:https://blog.csdn.net/xiaosheng_papa/article/details/80218006

亲测有效。

注:有些系统中已经配置了单数据源,现在要转成多数据源,可能需要额外的配置。拿我自己当前项目来说:

项目在启动类中配置了单数据源:

        @Bean(name = "DataSource")//数据源配置
	@ConfigurationProperties(prefix = "spring.datasource.db1")//xxx要和server.context-path配置的名称一样
	@Primary
	public DataSource dataSource() {
		return DataSourceBuilder.create().build();
	}

	@Bean(name = "sqlSessionFactory")//SqlSessionFactory配置
	@Primary
	public SqlSessionFactory sqlSessionFactory(@Qualifier("DataSource") DataSource dataSource) throws Exception {
		SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
		bean.setDataSource(dataSource);
		bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:transconf/sql-map-config.xml"));//po对象别名的xml文件
		bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/**/*.xml"));//mapper的xml文件
		return bean.getObject();
	}

public SqlSessionFactory sqlSessionFactory(@Qualifier("DataSource") DataSource dataSource) throws Exception {

 需要改写成

public SqlSessionFactory sqlSessionFactory(@Qualifier("dynamicDataSource") DataSource dataSource) throws Exception {

 

完整原文: http://blog.maptoface.com/post/143

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值