摘要
本篇文章只是個人閱讀mybatis源碼總結的經驗或者個人理解mybatis的基本輪廓,作為拋磚引玉的功能,希望對你有幫助,如果需要深入了解細節還需親自去閱讀源碼。
mybatis基本架構
mybatis的源碼應該算是比較容易閱讀的,首先mybatis核心功能就是執行sql語句,但在其基礎上又有許多增強的地方(動態sql,orm等)。看一個框架的時候,第一步是對整個框架有一個大體的了解。例如mybatis,我們可以從初始化到完成一個sql請求為主線,看一下涉及了哪些類。我個人總結了一下,mybatis的框架主要的核心類有4個
configuration
configuration就是用于解析、保存、處理mybatis的配置內容,包括了
- mybatis基本配置,例如支持數據庫中的字段支持下標轉駝峰mapunderscoretocamelcase=true等等,參看mybatis配置說明
- sqlmapper管理,也就是通過xml或者注解寫的一些sql映射。相關的類可以查看源碼中mappedstatement類。
- 創建類,configuration還有一些創建類的功能,例如executor、statementhandler。這個2個類后面還會說到
小節configuration
總結configuration的功能,當然,如何讀取和解析相關文件是configuration中大部分代碼做的事。這些都是為了準備后面mybatis運行的基本條件。configuration中創建類是因為創建的這些類都依賴于configuration(但這樣做數據和邏輯沒有做到分離)。
sqlsession
sqlsession可能是mybatis中我們最常用的類,其實他是一個門面類,直接對外提供服務
1
2
3
4
5
6
7
8
9
|
public interface sqlsession extends closeable { <t> t selectone(string statement); <e> list<e> selectlist(string statement, object parameter); int delete(string statement); void rollback(); void commit(); ... } |
這些方法都是直接提供給外部調用的。看到這些方法是不是很親切。(我個人在看源碼的時候看到一些自己用過的一些類或方法的時候都有種莫名的親近感。感覺終于和我的認知世界有交集了)
sqlsession的創建
sqlsessionfactor是用于創建sqlsession建造者,提供給外部快速創建一個sqlsession。是一個工廠類,而sqlsessionfactor的創建則是由sqlsessionfactorbuilder。
executor
前面說了sqlsession只是一個門面類,executor才是負責sql語句執行的。因此executor才是整個mybatis核心。executor的實現類有
- baseexecutor:看名字知道是最基礎executor,其他的executor都和這個類有一定的關系
- cachingexecutor:每次查詢的時候會先從緩存中獲取,每次有增刪改的時候會讓緩存失效。cachingexecutor其實是一個代理內,內部代理了baseexecutor(或其子類)。在baseexecutor基礎上增加了緩存操作。
相關類
我們看一個executor參數最多的一個方法
1
|
<e> list<e> query(mappedstatement ms, object parameter, rowbounds rowbounds, resulthandler resulthandler, cachekey cachekey, boundsql boundsql) throws sqlexception; |
這些類都對執行sql有一定關系
mappedstatement
具體點來理解就是我們定義的sql映射語句,例如我們xml定義的:
1
2
3
4
|
<select id= "selectcountbypath" parametertype= "java.lang.string" resulttype= "java.lang.long" > select count( 1 ) from config where path = #{path} </select> |
paramter
這個就是傳遞給sql映射的參數,用于生成和填充動態sql語句
rowbound
限定一次查詢數據量,類很簡單,看代碼就明白,不多說
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class rowbounds { public static final int no_row_offset = 0 ; public static final int no_row_limit = integer.max_value; public static final rowbounds default = new rowbounds(); private int offset; private int limit; public rowbounds() { this .offset = no_row_offset; this .limit = no_row_limit; } public rowbounds( int offset, int limit) { this .offset = offset; this .limit = limit; } } |
resulthandler
這個和本地緩存有關,用于保存一個查詢語句的緩存對象,下次有相同的查詢語句的時候就會先嘗試從本地緩存中獲取。 注意:
,mybatis有2級緩存,第一級是cachingexecutor,第二級緩存就是mybatis的本地緩存,也就是和resulthandler
緩存失效策略是和一級緩存一樣,任何增刪改都會清空本地緩存
cachekey
一個查詢語句的在本地緩存中的key,根據sql語句,參數等等組成
boundsql
這個對象就是本次實際需要執行的sql語句有關的信息,
1
2
3
4
5
6
7
|
public class boundsql { private string sql; private list<parametermapping> parametermappings; private object parameterobject; private map<string, object> additionalparameters; private metaobject metaparameters; ... |
如果說parameter參數是實際傳入的參數,那么boundsql就是根據傳入參數進行相關解析后的結果。他的創建在mappedstatement中,根據parameter和當前執行mappedstatement生成
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public boundsql getboundsql(object parameterobject) { boundsql boundsql = sqlsource.getboundsql(parameterobject); list<parametermapping> parametermappings = boundsql.getparametermappings(); if (parametermappings == null || parametermappings.isempty()) { boundsql = new boundsql(configuration, boundsql.getsql(), parametermap.getparametermappings(), parameterobject); } // check for nested result maps in parameter mappings (issue #30) for (parametermapping pm : boundsql.getparametermappings()) { string rmid = pm.getresultmapid(); if (rmid != null ) { resultmap rm = configuration.getresultmap(rmid); if (rm != null ) { hasnestedresultmaps |= rm.hasnestedresultmaps(); } } } return boundsql; } |
interceptor
mybatis提供了interceptor用于在執行executor之前進行一些操作,mybatis是怎么使用interceptor。其實就是在創建executor時候,會
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
public executor newexecutor(transaction transaction, executortype executortype) { executortype = executortype == null ? defaultexecutortype : executortype; executortype = executortype == null ? executortype.simple : executortype; executor executor; if (executortype.batch == executortype) { executor = new batchexecutor( this , transaction); } else if (executortype.reuse == executortype) { executor = new reuseexecutor( this , transaction); } else { executor = new simpleexecutor( this , transaction); } if (cacheenabled) { executor = new cachingexecutor(executor); } //看這里!!! executor = (executor) interceptorchain.pluginall(executor); return executor; } |
這里主要是通過jdk動態代理實現的
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
|
public class plugin implements invocationhandler { ... public static object wrap(object target, interceptor interceptor) { map< class <?>, set<method>> signaturemap = getsignaturemap(interceptor); class <?> type = target.getclass(); class <?>[] interfaces = getallinterfaces(type, signaturemap); if (interfaces.length > 0 ) { return proxy.newproxyinstance( type.getclassloader(), interfaces, new plugin(target, interceptor, signaturemap)); } return target; } ... @override public object invoke(object proxy, method method, object[] args) throws throwable { try { set<method> methods = signaturemap.get(method.getdeclaringclass()); if (methods != null && methods.contains(method)) { return interceptor.intercept( new invocation(target, method, args)); } return method.invoke(target, args); } catch (exception e) { throw exceptionutil.unwrapthrowable(e); } } |
這樣在調用executor的時候就會先判斷是否滿足interceptor的執行條件,滿足則會先執行intercepter#intercept()方法
最底層的handler
要說直接和jdbc打交道的就是各種handler類,例如
- statementhandler: 處理java.sql.statement
- parameterhandler: 向preparedstatement中設置參數
- resultsethandler:處理sql執行結果,并轉換成指定的類對象 上面的這些其實都不復雜,所以代碼還是比較好理解的
transaction
每個executor生成的時候都會把transaction傳入,在baseexecutor中transaction是其成員變量,那transaction的作用是什么呢?
1
2
3
4
5
6
7
|
public interface transaction { connection getconnection() throws sqlexception; void commit() throws sqlexception; void rollback() throws sqlexception; void close() throws sqlexception; integer gettimeout() throws sqlexception; } |
其實之前一直都沒提到過connect誰來管理,這里可以看出來,transaction負責了connection的獲取,以及對這次connect的提交和回滾等操作。這個類也是比較好理解的。executor的commit或者rollback最后都是調用transaction的
總結
可以看出,mybatis的源碼是比較容易閱讀的(相對于spring等)。上面介紹了框架中的一些核心類,但是很多細節的地方值得我們去深挖。這個就需要我們能沉下來好好閱讀代碼。
原文鏈接:http://www.cnblogs.com/lizo/p/7281441.html