springboot数据源不正确_SpringBoot配置多数据源最简单的解决方案

一个最简单的SpringBoot+Mybatis的多数据源解决方案,基于Mysql数据库。不能用你打我,需要指导可以留言。喜欢可以关注我,不扯淡,还是谢谢你。

603a144f7225ff3e358ffff6ea904730.png

1.第一步先配置多个数据源信息,在application.properties文件里面。配置文件里含有数据库链接等待时候等配置,防止断开链接后报错:No operations allowed after connection closed ,也可以根据实际情况修改。

#多数据源配置–Mysql shop库spring.datasource.shop.driver-class-name=com.mysql.jdbc.Driverspring.datasource.shop.url=jdbc:mysql://xxx?useUnicode=true&characterEncoding=UTF-8spring.datasource.shop.username=xxxspring.datasource.shop.password= xxxspring.datasource.shop.max-idle=10spring.datasource.shop.max-wait=10000spring.datasource.shop.min-idle=5spring.datasource.shop.initial-size=5spring.datasource.shop.validation-query=SELECT 1spring.datasource.shop.test-on-borrow=falsespring.datasource.shop.test-while-idle=truespring.datasource.shop.time-between-eviction-runs-millis=18800#多数据源配置-Mysql 账单数据库–bill库spring.datasource.bill.driver-class-name=com.mysql.jdbc.Driverspring.datasource.bill.url= xxxxxxxxspring.datasource.bill.username=xxxxxxxxspring.datasource.bill.password= xxxxxxxx#最大的空闲连接数量.spring.datasource.bill.max-idle=10#等待连接返回的最大等待时间,毫秒单位.spring.datasource.bill.max-wait=10000#最小的空闲连接数量.spring.datasource.bill.min-idle=5#初始数量spring.datasource.bill.initial-size=5#指定获取连接时连接校验的sql查询语句spring.datasource.bill.validation-query=SELECT 1#当从连接池借用连接时,是否测试该连接.spring.datasource.bill.test-on-borrow=false#创建时,是否测试连接spring.datasource.bill.test-while-idle=true#指定空闲连接检查、废弃连接清理、空闲连接池大小调整之间的操作时间间隔spring.datasource.bill.time-between-eviction-runs-millis=18800

注意:一个shop库和一个bill库,其中bill为主库,在使用的过程中必须指定主库,不然会报错。

2.主库数据源 配置

/*** Created by wll on 2018/10/18.*/@Configuration@MapperScan(basePackages = “com.wll.mapper.bill”, sqlSessionTemplateRef = “billSqlSessionTemplate”)public class BillDataSourceConfig {@Bean(name = “billDataSource”)@ConfigurationProperties(prefix = “spring.datasource.bill”)@Primarypublic DataSource testDataSource() {return DataSourceBuilder.create().build();}@Bean(name = “billSqlSessionFactory”)@Primarypublic SqlSessionFactory testSqlSessionFactory(@Qualifier(“billDataSource”) DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(“classpath:mapping/bill/*.xml”));return bean.getObject();}@Bean(name = “billTransactionManager”)@Primarypublic DataSourceTransactionManager testTransactionManager(@Qualifier(“billDataSource”) DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean(name = “billSqlSessionTemplate”)@Primarypublic SqlSessionTemplate testSqlSessionTemplate(@Qualifier(“billSqlSessionFactory”) SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}}

红色部分意思的为com.wll.mapper.bill目录下的mapper文件指定数据源信息是billSqlSessionTemplate,也指定了这个mapper类对应的xml路径是classpath:mapping/bill/*.xml

3.另一个数据源信息配置(其实和上面的一样的,只是改了一些东西,复制粘贴代码切忘修改) 注意红色部分,都要修改

@Configuration@MapperScan(basePackages = ” com.wll.mapper.shop”, sqlSessionTemplateRef = “shopSqlSessionTemplate”)public class ShopDataSourceConfig {@Bean(name = “shopDataSource”)@ConfigurationProperties(prefix = “spring.datasource.shop”)public DataSource testDataSource() {return DataSourceBuilder.create().build();}@Bean(name = “shopSqlSessionFactory”)public SqlSessionFactory testSqlSessionFactory(@Qualifier(“shopDataSource”) DataSource dataSource) throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(“classpath:mapping/shop/*.xml”));return bean.getObject();}@Bean(name = “shopTransactionManager”)public DataSourceTransactionManager testTransactionManager(@Qualifier(“shopDataSource”) DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean(name = “shopSqlSessionTemplate”)public SqlSessionTemplate testSqlSessionTemplate(@Qualifier(“shopSqlSessionFactory”) SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}}

4.Mapper里面和平时一样,xml也一样,这里不再演示。

我个人博客有更多相技术文章,都是工作中用到的,喜欢的经常去看看:百度一下 “vsalw”就能找到。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值