前言
本篇教程偏向實戰,程序猿直接copy代碼加入到自己的項目中做簡單的修修改改便可使用,而對于springboot以及mybatis不在此進行展開介紹,如有讀者希望了解可以給我留言,并持續關注,我后續會慢慢更新。(黑色區域代碼部分,安卓手機可手動向左滑動,來查看全部代碼)
整合
其實整合很簡單,如果是用gradle的話,在build.gradle文件里加入
1
|
compile( 'org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1' ) |
如果是用maven的話在pom.xml文件里加入
單庫配置:
引入之后,默認情況下,spring boot會自動為我們配置好一個datasource,它會在classpath中搜索h2、hsqldb等內存數據庫的jar包,如果找到了,就會自動配置一個內存數據庫的datasource。
如果在application.yml或application.property
中指定了spring.datasource.*
的相關配置參數,spring boot就會使用該配置創建一個datasource。
然后會自動創建使用該datasource的sqlsessionfactorybean以及sqlsessiontemplate。會自動掃描你的mappers,連接到sqlsessiontemplate,并注冊到spring上下文中。
1
2
3
4
|
spring.datasource.url=jdbc:mysql: //localhost/test spring.datasource.username=dbuser spring.datasource.password=dbpass spring.datasource.driver- class -name=com.mysql.jdbc.driver |
更多參數請查看datasourceproperties
多庫配置:
由于業務需要,項目要同時使用多個數據庫進行業務開發:
首先,我們必須在application.property中自定義兩個數據源的配置,一個使用first.datasource.*,另一個使用second.datasource.*,為了能使別人一眼看出連接的是什么庫,可以使用數據庫命名,比如user庫,則可以使用user.datasource.*,在使用多數據源的時候,所有必要配置都不能省略。
1
2
3
4
5
6
7
8
9
10
|
first.datasource.url=jdbc:mysql: //localhost/first first.datasource.username=dbuser1 first.datasource.password=dbpass1 first.datasource.driver- class -name=com.mysql.jdbc.driver first.datasource.type=com.alibaba.druid.pool.druiddatasource //我用的是druid,也可以不加用默認的 second.datasource.url=jdbc:mysql: //localhost/second second.datasource.username=dbuser2 second.datasource.password=dbpass2 second.datasource.driver- class -name=com.mysql.jdbc.driver second.datasource.type=com.alibaba.druid.pool.druiddatasource |
直接上代碼,我的做法是將兩個數據源用兩個配置類創建:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
@configuration @mapperscan (basepackages = { "com.user.server.dao" }, sqlsessiontemplateref = "usersqlsessiontemplate" ) public class usermybatisconfig { @bean (name = "userdatasource" ) @primary //必須加此注解,不然報錯,下一個類則不需要添加 @configurationproperties (prefix = "first.datasource" ) // prefix值必須是application.properteis中對應屬性的前綴 public datasource userdatasource() { return datasourcebuilder.create().build(); } @bean public sqlsessionfactory usersqlsessionfactory( @qualifier ( "userdatasource" ) datasource datasource) throws exception { sqlsessionfactorybean bean = new sqlsessionfactorybean(); bean.setdatasource(datasource); //添加xml目錄 resourcepatternresolver resolver = new pathmatchingresourcepatternresolver(); try { bean.setmapperlocations(resolver.getresources( "classpath*:com/user/server/dao/mapping/*.xml" )); return bean.getobject(); } catch (exception e) { e.printstacktrace(); throw new runtimeexception(e); } } @bean public sqlsessiontemplate usersqlsessiontemplate( @qualifier ( "usersqlsessionfactory" ) sqlsessionfactory sqlsessionfactory) throws exception { sqlsessiontemplate template = new sqlsessiontemplate(sqlsessionfactory); // 使用上面配置的factory return template; } } @configuration @mapperscan (basepackages = { "com.airmi.server.dao" }, sqlsessiontemplateref = "autotestsqlsessiontemplate" ) public class autotestmybatisconfig { @bean @configurationproperties (prefix = "autotest.datasource" ) public datasource autotestdatasource() { return datasourcebuilder.create().build(); } @bean public sqlsessiontemplate autotestsqlsessiontemplate( @qualifier ( "autotestsqlsessionfactory" ) sqlsessionfactory sqlsessionfactory) throws exception { sqlsessiontemplate template = new sqlsessiontemplate(sqlsessionfactory); return template; } @bean public sqlsessionfactory autotestsqlsessionfactory( @qualifier ( "autotestdatasource" ) datasource datasource) throws exception { sqlsessionfactorybean bean = new sqlsessionfactorybean(); bean.setdatasource(datasource); //添加xml目錄 resourcepatternresolver resolver = new pathmatchingresourcepatternresolver(); try { bean.setmapperlocations(resolver.getresources( "classpath*:com/airmi/server/dao/mapping/*.xml" )); return bean.getobject(); } catch (exception e) { e.printstacktrace(); throw new runtimeexception(e); } } } |
@primary //該注解表示在同一個接口有多個實現類可以注入的時候,默認選擇哪一個,而不是讓autowire注解報錯,官網要求當多個數據源時,必須指定一個datasource,另一個datasource則不用添加。
@qualifier 根據名稱進行注入,通常是在具有相同的多個類型的實例的一個注入(例如有多個datasource類型的實例)。
1
|
@mapperscan (basepackages = { "com.user.server.dao" }, sqlsessiontemplateref = "usersqlsessiontemplate" ) basepackages為mapper所在的包,sqlsessiontemplateref要引用的實例。 |
user代碼結構如下:
總結
以上所述是小編給大家介紹的spring boot 整合mybatis 使用多數據源的實現方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.jianshu.com/p/0fb2bad454e4