项目架构

1.导入相关依赖

 <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!--freemarker支持-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency> <!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!--整合mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>

2.application.properties

 ## test1 数据源配置
#spring.datasource.test1.driverClassName=com.mysql.jdbc.Driver
#spring.datasource.test1.jdbc-url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf8
#spring.datasource.test1.username=root
#spring.datasource.test1.password=123 ## test2 数据源配置
#spring.datasource.test2.driverClassName=com.mysql.jdbc.Driver
#spring.datasource.test2.jdbc-url=jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf8
#spring.datasource.test2.username=root
#spring.datasource.test2.password=123

3.创建datasource包  下TestMyBatisConfig1

 @Configuration
@MapperScan(basePackages = "cn.happy.test1", sqlSessionFactoryRef = "test1SqlSessionFactory")
public class DataSource1Config {
@Bean(name = "test1DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test1")
@Primary
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
} @Bean(name = "test1SqlSessionFactory")
@Primary
public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//读取mybatis小配置文件
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
return bean.getObject();
} @Bean(name = "test1TransactionManager")
@Primary
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
} @Bean(name = "test1SqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}

@Primary注解标识默认使用的数据源

如果不加启动会报如下错误:意思是有两个数据源  不知需要使用哪个数据源

3.创建datasource包  下TestMyBatisConfig2

 @Configuration
@MapperScan(basePackages = "cn.happy.test2", sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataSource2Config {
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test2")
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);
//读取mybatis小配置文件
// bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test2/*.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);
} }

4.entity层

 /**
* author: 刘涛
*
* @create 2018-04-04 11:18
*/
public class Book { private Integer bookid; private String bookname; private Integer bookprice; get set省略。。
}

5.test1下的dao中BookMapperTest1

 public interface BookMapperTest1 {

     @Select("select * from book where bookname=#{bookname}")
public Book findByName(@Param("bookname") String bookname); @Insert("insert into book(bookname,bookprice) values (#{bookname},#{bookprice})")
public int insertBook(@Param("bookname") String bookname, @Param("bookprice") Double bookprice); @Select("select * from book")
public Book findBook(@Param("Book") Book book);
}

6.test2下的dao中BookMapperTest2

 public interface BookMapperTest2 {

     @Select("select * from book where bookname=#{bookname}")
public Book findByName(@Param("bookname") String bookname); @Insert("insert into book(bookname,bookprice) values (#{bookname},#{bookprice})")
public int insertBook(@Param("bookname") String bookname, @Param("bookprice") Double bookprice);
}

7.controller层

@RestController
public class BookController {
@Autowired
private BookMapperTest1 bookMapperTest1; @Autowired
private BookMapperTest2 bookMapperTest2; @RequestMapping("insert001")
public String insert001(String bookname,Double bookprice){
bookMapperTest1.insertBook(bookname,bookprice);
return "success";
} @RequestMapping("insert002")
public String insert002(String bookname,Double bookprice){
bookMapperTest2.insertBook(bookname,bookprice);
return "success";
}
}

8.启动项目

test1数据库

test2数据库

SpringBoot整合多数据源实现的更多相关文章

  1. springBoot整合多数据源

    springBoot整合相关 1:springBoot整合多数据源: 应用场景:     项目需要同时连接两个不同的数据库A, B,并且它们都为主从架构,一台写库,多台读库. 工具/版本: jdk1. ...

  2. springboot整合多数据源解决分布式事务

    一.前言        springboot整合多数据源解决分布式事务.             1.多数据源采用分包策略              2.全局分布式事务管理:jta-atomikos. ...

  3. springboot整合多数据源及事物

    有两种方式:一种是分包的方式.一种是加注解的方式(@DataSource(ref="")). 分包方式:项目结构图如下: 分为com.itmayiedu.test01.com.it ...

  4. SpringBoot整合Druid数据源

    关于SpringBoot数据源请参考我上一篇文章:https://www.cnblogs.com/yueshutong/p/9409295.html 一:Druid介绍 1. Druid是什么? Dr ...

  5. SpringBoot整合Mybatis【非注解版】

    接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ​ 选择Spring Initializr,配置JDK版本 ​ 输入项目名 ​ 选择构建web项目所需的state ...

  6. SpringBoot整合Mybatis之项目结构、数据源

    已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...

  7. SpringBoot整合阿里Druid数据源及Spring-Data-Jpa

    SpringBoot整合阿里Druid数据源及Spring-Data-Jpa https://mp.weixin.qq.com/s?__biz=MzU0MDEwMjgwNA==&mid=224 ...

  8. SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)

    1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...

  9. SpringBoot整合Mybatis多数据源 (AOP+注解)

    SpringBoot整合Mybatis多数据源 (AOP+注解) 1.pom.xml文件(开发用的JDK 10) <?xml version="1.0" encoding=& ...

随机推荐

  1. C语言strcpy,strncpy和strlcpy讲解

    前言 C风格的字符串处理函数有很多,如strcpy().strcat()等等. strcpy与strcat char* strcpy (char* dest, const char* src); ch ...

  2. windows下搭建Kafka,并通过命令窗口收发消息

    参考网址: https://blog.csdn.net/ydc321/article/details/70154278 前提条件:windows环境需要安装jdk 1.下载Kafka,可以通过官网下载 ...

  3. JAVA进阶22

    1.接口默认方法的使用 ①接口的默认方法可以通过接口实现类对象直接调用. ②接口的默认方法也可以被接口实现类进行覆盖重写 package cn.intcast.demo17; public inter ...

  4. TextView设置不同字段不同点击事件

    转载自:http://www.apkbus.com/blog-160625-59265.html package com.example.fortextdemo;   import java.util ...

  5. Java基础 -- Java 抽象类 抽象方法

    总结: 1. 抽象类不能被实例化(初学者很容易犯的错),如果被实例化,就会报错,编译无法通过.只有抽象类的非抽象子类可以创建对象. 2. 抽象类中不一定包含抽象方法,但是有抽象方法的类必定是抽象类. ...

  6. 机器学习基石9-Linear Regression

    注: 文章中所有的图片均来自台湾大学林轩田<机器学习基石>课程. 笔记原作者:红色石头 微信公众号:AI有道 上节课,主要介绍了在有noise的情况下,VC Bound理论仍然是成立的.同 ...

  7. CentOS配代理服务器

    背景: 某云上有台Windows主机,为了省钱(...),购买的1M带宽... 然后日常只有我用,特别卡,嫌弃得不行. 最近接触到代理,琢磨代理连接到局域网内带宽大的主机,是否上网速度会蹭蹭得涨?实践 ...

  8. 收藏vue技术内幕

    http://hcysun.me/vue-design/art/7vue-reactive.html#observer-%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0

  9. Java中值传递和引用传递的区别

    在Java中参数的传递主要有两种:值传递和参数传递: 下面是对两种传递方式在内存上的分析: 一:值传递 解释:实参传递给形参的是值  形参和实参在内存上是两个独立的变量 对形参做任何修改不会影响实参 ...

  10. 关于PCB走线能不能走锐角的讨论

    (此文参考吴川斌的博客) 很多PCB工程师都知道Layout走线时忌走直角,那么锐角能走吗? 回答当然是否定的!为什么呢? 这里先不说锐角对高速信号走线会不会造成负面影响,单从PCB DFM(可制造性 ...