springboot 第十九节 starter and muti_datasource 多数据源

1、

spring.datasource.spring.driverClassName=com.mysql.jdbc.Driver
spring.datasource.spring.jdbcUrl=jdbc:mysql://127.0.0.1:3306/springboot?serverTimezone=GMT%2B8
spring.datasource.spring.username=root
spring.datasource.spring.password=rootspring.datasource.spring2.driverClassName=com.mysql.jdbc.Driver
spring.datasource.spring2.jdbcUrl=jdbc:mysql://127.0.0.1:3306/springcloud?serverTimezone=GMT%2B8
spring.datasource.spring2.username=root
spring.datasource.spring2.password=root

2、

package com.tx.config;import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;import javax.sql.DataSource;@Configuration
@MapperScan(basePackages = "com.tx.dao.orders", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataSource2Config {@Bean(name = "test2DataSource")/*  主要是这里,配置了数据源2*/@ConfigurationProperties(prefix = "spring.datasource.spring2")public DataSource testDataSource() {return DataSourceBuilder.create().build();}@Bean(name = "test2SqlSessionFactory")public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource)throws Exception {SqlSessionFactoryBean bean = new SqlSessionFactoryBean();bean.setDataSource(dataSource);bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapping/orders/*.xml"));return bean.getObject();}@Bean(name = "test2TransactionManager")public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {return new DataSourceTransactionManager(dataSource);}@Bean(name = "test2SqlSessionTemplate")public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {return new SqlSessionTemplate(sqlSessionFactory);}}

3、

package com.tx.service.impl;import com.tx.dao.orders.OrdersMapper;
import com.tx.dao.users.UsersMapper;
import com.tx.model.Orders;
import com.tx.model.Users;
import com.tx.service.IOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;@Service
public class OrderServiceImpl  implements IOrderService {@Resourceprivate UsersMapper usersMapper;@Resourceprivate OrdersMapper ordersMapper;@Override@Transactionalpublic void addOrder(Orders orders, Users users) {usersMapper.insertSelective(users);ordersMapper.insertSelective(orders);}
}

4、最后测试:


import com.tx.App;
import com.tx.model.Orders;
import com.tx.model.Users;
import com.tx.service.IOrderService;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;@SpringBootTest(classes = {App.class})
@RunWith(SpringRunner.class)
public class Test {@Resourceprivate IOrderService iOrderService;@org.junit.Testpublic void test1() {Users users = new Users();users.setUsername("enjoy34");users.setPasswd("haha34");users.setId(34);Orders orders = new Orders();orders.setAccount(34);orders.setName("娃娃34");orders.setUserId(34);iOrderService.addOrder(orders,users);}
}

代码示例:https://gitee.com/dgx555/springboot/tree/master/springboot_enjoy_03_starter_andmuti_datasource

springboot 第十九节 starter and muti_datasource 多数据源相关推荐

  1. Python编程基础:第五十九节 守护线程Daemon Threading

    第五十九节 守护线程Daemon Threading 前言 实践 前言 守护线程是在后台运行的线程,对程序的运行并不重要,你的程序在退出前不会等待守护线程的完成,此类线程的特点是,当程序中主线程及所有 ...

  2. Python编程基础:第四十九节 鸭子类型Duck Typing

    第四十九节 鸭子类型Duck Typing 前言 实践 前言 本节我们一起学习一个非常有趣的知识点:鸭子类型.有这么一句话:If it walks like a duck, and it quacks ...

  3. Python编程基础:第三十九节 面向对象编程Object Oriented Programming

    第三十九节 面向对象编程Object Oriented Programming 前言 实践 前言 到目前为止我们都是函数式编程,也即将每一个功能块写为一个函数.其实还有一种更常用的编程方式被称为面向对 ...

  4. Python编程基础:第二十九节 异常Exception

    第二十九节 异常Exception 前言 实践 前言 我们在写代码时不可避免地会出错,这时候编译器便会抛出异常并中断程序的执行.针对这种情况,我们可以采用异常处理的方式捕捉程序中的异常信息,并将异常信 ...

  5. Python编程基础:第十九节 索引Index Operator

    第十九节 索引Index Operator 前言 实践 前言 我们在字符串学习过程中已经接触过索引的相关内容,我们在这里做一个小结,Python中的索引分为两部分,一种是从左向右,从0开始依次递增,例 ...

  6. 第三百八十九节,Django+Xadmin打造上线标准的在线教育平台—列表筛选结合分页...

    第三百八十九节,Django+Xadmin打造上线标准的在线教育平台-列表筛选结合分页 根据用户的筛选条件来结合分页 实现原理就是,当用户点击一个筛选条件时,通过get请求方式传参将筛选的id或者值, ...

  7. 第三百一十九节,Django框架,文件上传

    第三百一十九节,Django框架,文件上传 1.自定义上传[推荐] 请求对象.FILES.get()获取上传文件的对象 上传对象.name获取上传文件名称 上传对象.chunks()获取上传数据包,字 ...

  8. 大白话5分钟带你走进人工智能-第十九节逻辑回归之优化点(4)

                                                                                          第十九节逻辑回归之优化点(4 ...

  9. 火云开发课堂 - 《使用Cocos2d-x 开发3D游戏》系列 第十九节:雾

    <使用Cocos2d-x 开发3D游戏>系列在线课程 第十九节:雾 视频地址:http://edu.csdn.net/course/detail/1330/20819?auto_start ...

最新文章

  1. Java:如何更优雅的处理空值?
  2. python类库的查找
  3. MR8M CANCEL INVOICE后为什么要手工去FI清帐
  4. 玩转Numpy——linspace()函数使用详解
  5. Newbe.Claptrap 框架入门,第二步 —— 创建项目
  6. Linux禁止ip拒绝访问80,Linux iptables 设置允许(禁止)IP范围
  7. 活体检测python_活体检测很复杂?仅使用opencv就能实现!(附源码)!
  8. Windows 8 系统安装
  9. 很好看的source insight配色方案
  10. 我写了一本操作系统词典送你了
  11. Hexo博客摘要生成方法
  12. 认识PV/PVC/StorageClass
  13. 华硕支持2003服务器主板,驱动天空 - 品牌主板 - 服务器主板 SERVER - 华硕服务器主板...
  14. java毕业设计选题基于JavaWeb实现疫情环境下校园宿舍|寝室管理系统
  15. 最近计算机速度测试情况,WiFi速度测试测试测试互联网速度多少兆字节
  16. thinkphp3.1.3框架手册
  17. G.652光纤各个子类的主要区别及应用
  18. Linux内核2.6.34.14添加系统调用及编译方法(CentOS-6.4-x86_64)
  19. 【我的OpenGL学习进阶之旅】介绍一下 绘制图元
  20. 垃圾分类对生活的有什么好处

热门文章

  1. C语言学习(十)C语言中的小数
  2. app开屏广告实现——借助webview和原生fetch请求实现
  3. AIC,AIB,同德显卡五兄弟,
  4. html背景只向x轴扩散,地球化学(复习资料)
  5. openwrt系统理解
  6. LIN雨量传感器:吉利雨量感应器拆解 MLX75308开发与应用
  7. moment 计算日期差
  8. 月中工作总结_在全职工作的9个月中,我是如何从新手转到软件工程师的
  9. 谷歌是如何以简洁赢取用户的
  10. PostgreSQL 分区表一点也不差