(1)在pom.xml中添加依賴包;
(2)編寫啟動(dòng)類App.java這里特別要注意@Import({DynamicDataSourceRegister.class})否則啟動(dòng)不了會(huì)報(bào)找找不到數(shù)據(jù)源硫惕,這里注意一下這個(gè)DynamicDataSourceRegister需要自己到我源碼找配置文件,把所有配置文件復(fù)制一下就行啦惠猿,其他不用改厘擂。
(3)編寫配置文件application.properties;
(4)測(cè)試類測(cè)試指定數(shù)據(jù)源;(這里我節(jié)省了很多配置文件,會(huì)留下源碼)
(5)訪問頁面http://127.0.0.1:8080/test3
????????????????????????? http://127.0.0.1:8080/test1
(1)在pom.xml中添加依賴包;
(2)啟動(dòng)類
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
//注冊(cè)動(dòng)態(tài)多數(shù)據(jù)源
@Import({DynamicDataSourceRegister.class})
public classApp {
public static voidmain(String[] args) {
SpringApplication.run(App.class,args);
}
}
(3)編寫配置文件application.properties;
########################################################
###配置文件包括1個(gè)主數(shù)據(jù)源和多個(gè)數(shù)據(jù)源,
###其中主數(shù)據(jù)源在Spring中的beanName默認(rèn)為dataSource,
###另外幾個(gè)數(shù)據(jù)源的beanName分包為:ds1、ds2寇甸、ab
###其中datasource的type屬性可以具體指定到我們需要的數(shù)據(jù)源上面,
###不指定情況下默認(rèn)為:org.apache.tomcat.jdbc.pool.DataSource
###當(dāng)然你也可以把這些數(shù)據(jù)源配置到主dataSource數(shù)據(jù)庫中疗涉,然后讀取數(shù)據(jù)庫生成多數(shù)據(jù)源拿霉。當(dāng)然這樣做的必要性并不大,難不成數(shù)據(jù)源還會(huì)經(jīng)常變嗎博敬。
########################################################
# 主數(shù)據(jù)源友浸,默認(rèn)的
#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
# 更多數(shù)據(jù)源
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
# 下面為連接池的補(bǔ)充設(shè)置峰尝,應(yīng)用到上面所有數(shù)據(jù)源中
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)測(cè)試類測(cè)試指定數(shù)據(jù)源偏窝,我這里剩了很多配置文件,我會(huì)留下源碼武学;
(5)測(cè)試啟動(dòng)并測(cè)試結(jié)果
這里特別提示:多數(shù)據(jù)源是沒有事務(wù)的? ? ? ?
數(shù)據(jù)源A和B都進(jìn)行了增加或刪除
?A如果出錯(cuò)祭往,B是不會(huì)回滾的
?反過來也一樣
?同一個(gè)數(shù)據(jù)源有事務(wù)
源碼鏈接可以直接測(cè)試不過這里沒提供數(shù)據(jù)庫自己創(chuàng)建一個(gè),
里面就兩個(gè)屬性 id 和name就行 :http://pan.baidu.com/s/1eR2rx2y
####指導(dǎo)qq:179061434