SpringBoot 多数据源搭建

(1)在pom.xml中添加依赖包;

(2)编写启动类App.java这里特别要注意@Import({DynamicDataSourceRegister.class})否则启动不了会报找找不到数据源,这里注意一下这个DynamicDataSourceRegister需要自己到我源码找配置文件,把所有配置文件复制一下就行啦,其他不用改。

(3)编写配置文件application.properties;

(4)测试类测试指定数据源;(这里我节省了很多配置文件,会留下源码)

(5)访问页面http://127.0.0.1:8080/test3

                          http://127.0.0.1:8080/test1


(1)在pom.xml中添加依赖包;

(2)启动类

importcom.neil.config.DynamicDataSourceRegister;

importorg.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

importorg.springframework.context.annotation.Import;

/ **

*

* @ authorAngel(QQ:412887952)

*@versionv.0.1

* /

@SpringBootApplication

//注册动态多数据源

@Import({DynamicDataSourceRegister.class})

public classApp {

public static voidmain(String[] args) {

SpringApplication.run(App.class,args);

}

}


(3)编写配置文件application.properties;

########################################################

###配置文件包括1个主数据源和多个数据源,

###其中主数据源在Spring中的beanName默认为dataSource,

###另外几个数据源的beanName分包为:ds1、ds2、ab

###其中datasource的type属性可以具体指定到我们需要的数据源上面,

###不指定情况下默认为:org.apache.tomcat.jdbc.pool.DataSource

###当然你也可以把这些数据源配置到主dataSource数据库中,然后读取数据库生成多数据源。当然这样做的必要性并不大,难不成数据源还会经常变吗。

########################################################

# 主数据源,默认的

#spring.datasource.type=com.zaxxer.hikari.HikariDataSource

spring.datasource.driverClassName=com.mysql.jdbc.Driver

spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=1234

# 更多数据源

custom.datasource.names=ds1,ds2,ab

#custom.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource

custom.datasource.ds1.driverClassName=com.mysql.jdbc.Driver

custom.datasource.ds1.url=jdbc:mysql://localhost:3306/test1

custom.datasource.ds1.username=root

custom.datasource.ds1.password=1234

#custom.datasource.ds2.type=com.zaxxer.hikari.HikariDataSource

custom.datasource.ds2.driverClassName=com.mysql.jdbc.Driver

custom.datasource.ds2.url=jdbc:mysql://localhost:3306/test

custom.datasource.ds2.username=root

custom.datasource.ds2.password=1234

#custom.datasource.ab.type=com.zaxxer.hikari.HikariDataSource

custom.datasource.ab.driverClassName=com.mysql.jdbc.Driver

custom.datasource.ab.url=jdbc:mysql://localhost:3306/test3

custom.datasource.ab.username=root

custom.datasource.ab.password=1234

# 下面为连接池的补充设置,应用到上面所有数据源中

spring.datasource.maximum-pool-size=100

spring.datasource.max-idle=10

spring.datasource.max-wait=10000

spring.datasource.min-idle=5

spring.datasource.initial-size=5

spring.datasource.validation-query=SELECT 1

spring.datasource.test-on-borrow=false

spring.datasource.test-while-idle=true

spring.datasource.time-between-eviction-runs-millis=18800

########################################################

### Java Persistence Api

########################################################

# Specify the DBMS

spring.jpa.database=MYSQL

# Show or not log for each sql query

spring.jpa.show-sql=true

# Hibernate ddl auto (create, create-drop, update)

spring.jpa.hibernate.ddl-auto=update

# Naming strategy

#[org.hibernate.cfg.ImprovedNamingStrategy  #org.hibernate.cfg.DefaultNamingStrategy]

spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.DefaultNamingStrategy

# stripped before adding them to the entity manager)

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect


(4)测试类测试指定数据源,我这里剩了很多配置文件,我会留下源码;


(5)测试启动并测试结果


这里特别提示:多数据源是没有事务的       

数据源A和B都进行了增加或删除

 A如果出错,B是不会回滚的

 反过来也一样

 同一个数据源有事务

源码链接可以直接测试不过这里没提供数据库自己创建一个,

里面就两个属性 id 和name就行 :http://pan.baidu.com/s/1eR2rx2y

####指导qq:179061434

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,974评论 6 342
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,014评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,780评论 18 399
  • 1. 啪 我一个弹跳起身,看见老婆把刚刚扇我巴掌的手收回去,大声嚷着“快起来,快起来,车快到了,在慢点赶不上火车了...
    林蒙LM阅读 291评论 2 2
  • 《望》 你推窗眺望 三月的春光 蝴蝶的翅膀 我驻足凝望 复古的轩窗 素美的容妆 他凭栏远望 滴翠的戎装 绯红的脸庞...
    木兰溪阅读 315评论 1 1