這篇文章給大家介紹在springboot中使用mybatis如何實(shí)現(xiàn)多數(shù)據(jù)源,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
說起多數(shù)據(jù)源,一般都來解決那些問題呢,主從模式或者業(yè)務(wù)比較復(fù)雜需要連接不同的分庫來支持業(yè)務(wù)。我們項(xiàng)目是后者的模式,網(wǎng)上找了很多,大都是根據(jù)jpa來做多數(shù)據(jù)源解決方案,要不就是老的spring多數(shù)據(jù)源解決方案,還有的是利用aop動(dòng)態(tài)切換
配置文件
pom包就不貼了比較簡單該依賴的就依賴,主要是數(shù)據(jù)庫這邊的配置:
mybatis.config-locations=classpath:mybatis/mybatis-config.xml spring.datasource.test1.driverClassName = com.mysql.jdbc.Driver spring.datasource.test1.url = jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8 spring.datasource.test1.username = root spring.datasource.test1.password = root spring.datasource.test2.driverClassName = com.mysql.jdbc.Driver spring.datasource.test2.url = jdbc:mysql://localhost:3306/test2?useUnicode=true&characterEncoding=utf-8 spring.datasource.test2.username = root spring.datasource.test2.password = root