springboot mybatis 配置雙資料來源

2021-10-08 10:32:08 字數 3338 閱讀 4082

檔案放置的位置,對應的同乙個資料庫的dao要放在同乙個包裡

spring:

datasource:

driverclassname: com.mysql.jdbc.driver

test1:

#driver-

class

-name: com.mysql.jdbc.driver

jdbc-url: jdbc:mysql:

//***x:

3306

/tqsq? servertimezone=utc&useunicode=

true

&characterencoding=utf8&charactersetresults=utf8&usessl=

false

&verifyservercertificate=

false

&autoreconnct=

true

&autoreconnectforpools=

true

&allowmultiqueries=

true

username:

password:

test2:

#driver-

class

-name: com.mysql.jdbc.driver

jdbc-url: jdbc:mysql:

//***xx:

3306

/jiameng?servertimezone=utc&useunicode=

true

&characterencoding=utf8&charactersetresults=utf8&usessl=

false

&verifyservercertificate=

false

&autoreconnct=

true

&autoreconnectforpools=

true

&allowmultiqueries=

true

username:

password:

2.配置config—datasource1檔案

@configuration

(basepackages =

"com.zhy.dao.test1"

,sqlsessionfactoryref =

"db1sqlsessionfactory"

)public

class

datasource1config

/** * 建立sqlsessionfactory 工廠會話

* @param datasource

* @return

*/@bean

(name =

"db1sqlsessionfactory"

)@primary

public sqlsessionfactorybean testsqlsessionfactory

(@qualifier

("db1datasource"

) datasource datasource)

throws exception

/** * 配置事務管理

* @param datasource

* @return

*/@bean

(name =

"db1transactionmanager"

)@primary

public datasourcetransactionmanager testtransactionmanager

(@qualifier

("db1datasource"

)datasource datasource)

/** * 返回資料庫會話模板

* @param sqlsessionfactory

* @return

*/@bean

(name =

"db1sqlsessiontemplate"

)@primary

public sqlsessiontemplate testsqlsessiontemplate

(@qualifier

("db1sqlsessionfactory"

)sqlsessionfactory sqlsessionfactory)

}

3.配置config—datasource2檔案

@configuration

(basepackages =

"com.zhy.dao.test2"

,sqlsessionfactoryref =

"db2sqlsessionfactory"

)public

class

datasource2config

/** * 建立sqlsessionfactory

* @param datasource

* @return

*/@bean

(name =

"db2sqlsessionfactory"

)public sqlsessionfactorybean testsqlsessionfactory

(@qualifier

("db2datasource"

) datasource datasource)

throws exception

/** * 配置事務管理

* @param datasource

* @return

*/@bean

(name =

"db2transactionmanager"

)public datasourcetransactionmanager testtransactionmanager

(@qualifier

("db2datasource"

)datasource datasource)

@bean

(name =

"db2sqlsessiontemplate"

)public sqlsessiontemplate testsqlsessiontemplate

(@qualifier

("db2sqlsessionfactory"

)sqlsessionfactory sqlsessionfactory)

}

spring boot mybatis配置檔案開發

之前寫了乙個註解版開發的,現在在乙個配置檔案開發。我直接把 貼下面 根據id查詢單個資訊 public orders getorders integer id 新增單個資訊 mybatis config.xml的配置 insert into orders user id,number,oreatet...

springboot mybatis 多資料來源配置

首先 配置檔案 配置資料來源local,other為連線名稱 spring.datasource.local.url jdbc postgresql spring.datasource.local.username admin spring.datasource.local.password 666...

Spring雙資料庫配置

有時候我們可能在乙個專案中使用兩個資料庫,為了實現使用兩個或多個資料庫的功能,我們需要在spring中配置相關資訊。首先是新增配置檔案conf.properties www.cppcns.com.config.propertyplaceholderconfigurer classpath confi...