一、前言
通過前面我們也知道,通過getmapper方式來進行查詢,最后會通過mappermehod類,對接口中傳來的參數也會在這個類里面進行一個解析,隨后就傳到對應位置,與sql里面的參數進行一個匹配,最后獲取結果。對于mybatis通常傳參(這里忽略掉rowbounds和resulthandler兩種類型)有幾種方式。
1、javabean類型參數
2、非javabean類型參數
注意,本文是基于mybatis3.5.0版本進行分析。
1、參數的存儲
2、對sql語句中參數的賦值
下面將圍繞這這兩方面進行
二、參數的存儲
先看下面一段代碼
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@test public void testselectordinaryparam() throws exception{ sqlsession sqlsession = mybatisutil.getsessionfactory().opensession(); usermapper mapper = sqlsession.getmapper(usermapper. class ); list<user> userlist = mapper.selectbyordinaryparam( "張三1號" ); system.out.println(userlist); sqlsession.close(); } list<user> selectbyordinaryparam(string username); // mapper接口 <select id= "selectbyordinaryparam" resultmap= "baseresultmap" > select <include refid= "base_column_list" /> from user where username = #{username,jdbctype=varchar} </select> |
或許有的人會奇怪,這個mapper接口沒有帶@param注解,怎么能在mapper配置文件中直接帶上參數名呢,不是會報錯嗎,
在mybatis里面,對單個參數而言,直接使用參數名是沒問題的,如果是多個參數就不能這樣了,下面我們來了解下,mybatis的解析過程,請看下面代碼,位于mappermehod類的內部類methodsignature構造函數中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public methodsignature(configuration configuration, class <?> mapperinterface, method method) { type resolvedreturntype = typeparameterresolver.resolvereturntype(method, mapperinterface); if (resolvedreturntype instanceof class <?>) { this .returntype = ( class <?>) resolvedreturntype; } else if (resolvedreturntype instanceof parameterizedtype) { this .returntype = ( class <?>) ((parameterizedtype) resolvedreturntype).getrawtype(); } else { this .returntype = method.getreturntype(); } this .returnsvoid = void . class .equals( this .returntype); this .returnsmany = configuration.getobjectfactory().iscollection( this .returntype) || this .returntype.isarray(); this .returnscursor = cursor. class .equals( this .returntype); this .returnsoptional = optional. class .equals( this .returntype); this .mapkey = getmapkey(method); this .returnsmap = this .mapkey != null ; this .rowboundsindex = getuniqueparamindex(method, rowbounds. class ); this .resulthandlerindex = getuniqueparamindex(method, resulthandler. class ); // 參數解析類 this .paramnameresolver = new paramnameresolver(configuration, method); } |
參數的存儲解析皆由paramnameresolver類來進行操作,先看下該類的構造函數
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
|
/** * config 全局的配置文件中心 * method 實際執行的方法,也就是mapper接口中的抽象方法 * */ public paramnameresolver(configuration config, method method) { // 獲取method中的所有參數類型 final class <?>[] paramtypes = method.getparametertypes(); // 獲取參數中含有的注解,主要是為了@param注解做準備 final annotation[][] paramannotations = method.getparameterannotations(); final sortedmap<integer, string> map = new treemap<>(); // 這里實際上獲取的值就是參數的個數。也就是二維數組的行長度 int paramcount = paramannotations.length; // get names from @param annotations for ( int paramindex = 0 ; paramindex < paramcount; paramindex++) { // 排除rowbounds和resulthandler兩種類型的參數 if (isspecialparameter(paramtypes[paramindex])) { // skip special parameters continue ; } string name = null ; // 如果參數中含有@param注解,則只用@param注解的值作為參數名 for (annotation annotation : paramannotations[paramindex]) { if (annotation instanceof param) { hasparamannotation = true ; name = ((param) annotation).value(); break ; } } // 即參數沒有@param注解 if (name == null ) { // 參數實際名稱,其實這個值默認就是true,具體可以查看configuration類中的該屬性值,當然也可以在配置文件進行配置關閉 // 如果jdk處于1.8版本,且編譯時帶上了-parameters 參數,那么獲取的就是實際的參數名,如methoda(string username) // 獲取的就是username,否則獲取的就是args0 后面的數字就是參數所在位置 if (config.isuseactualparamname()) { name = getactualparamname(method, paramindex); } // 如果以上條件都不滿足,則將參數名配置為 0,1,2../ if (name == null ) { // use the parameter index as the name ("0", "1", ...) // gcode issue #71 name = string.valueof(map.size()); } } map.put(paramindex, name); } names = collections.unmodifiablesortedmap(map); } |
這個構造函數的作用就是對參數名稱進行一個封裝,得到一個 “參數位置-->參數名稱 “ 的一個map結構,這樣做的目的是為了替換參數值,我們也清楚,實際傳過來的參數就是一個一個object數組結構,我們也可以將它理解為map結構。即 index --> 參數值,此就和之前的 map結構有了對應,也就最終可以得到一個 參數名稱 ---> 參數值 的一個對應關系。
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
|
public object execute(sqlsession sqlsession, object[] args) { object result; switch (command.gettype()) { // 其它情況忽略掉 case select: // 這里參數中含有resulthandler,暫不做討論 if (method.returnsvoid() && method.hasresulthandler()) { executewithresulthandler(sqlsession, args); result = null ; } else if (method.returnsmany()) { // 1、 返回結果為集合類型或數組類型,這種情況適用于大多數情況 result = executeformany(sqlsession, args); } else if (method.returnsmap()) { // 返回結果為map類型 result = executeformap(sqlsession, args); } else if (method.returnscursor()) { result = executeforcursor(sqlsession, args); } else { // 2、返回結果javabean類型,或普通的基礎類型及其包裝類等 object param = method.convertargstosqlcommandparam(args); result = sqlsession.selectone(command.getname(), param); // 對java8中的optional進行了支持 if (method.returnsoptional() && (result == null || !method.getreturntype().equals(result.getclass()))) { result = optional.ofnullable(result); } } break ; default : throw new bindingexception( "unknown execution method for: " + command.getname()); } if (result == null && method.getreturntype().isprimitive() && !method.returnsvoid()) { throw new bindingexception( "mapper method '" + command.getname() + " attempted to return null from a method with a primitive return type (" + method.getreturntype() + ")." ); } return result; } |
這里主要分析1情況。對于2情況也就是接下來要說的參數賦值情況,不過要先介紹下method.convertargstosqlcommandparam這代碼帶來的一個結果是怎么樣的
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
|
public object convertargstosqlcommandparam(object[] args) { return paramnameresolver.getnamedparams(args); } public object getnamedparams(object[] args) { final int paramcount = names.size(); if (args == null || paramcount == 0 ) { return null ; } else if (!hasparamannotation && paramcount == 1 ) { // 1 return args[names.firstkey()]; } else { final map<string, object> param = new parammap<>(); int i = 0 ; for (map.entry<integer, string> entry : names.entryset()) { param.put(entry.getvalue(), args[entry.getkey()]); // add generic param names (param1, param2, ...) final string genericparamname = generic_name_prefix + string.valueof(i + 1 ); // ensure not to overwrite parameter named with @param if (!names.containsvalue(genericparamname)) { param.put(genericparamname, args[entry.getkey()]); } i++; } return param; } } |
可以很清楚的知道最后又調用了paramnameresolver類的getnamedpaams方法,這個方法的主要作用就是,將原來的參數位置 --> 參數名稱 映射關系轉為 參數名稱 --->參數值 ,并且新加一個參數名和參數值得一個對應關系。即
param1 ->參數值1
param2 -->參數值2
當然如果只有一個參數,如代碼中的1部分,若參數沒有@param注解,且只有一個參數,則不會加入上述的一個對象關系,這也就是前面說的,對于單個參數,可以直接在sql中寫參數名就ok的原因。下面回到前面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
private <e> object executeformany(sqlsession sqlsession, object[] args) { list<e> result; // 獲取對應的一個映射關系,param類型有可能為map或null或參數實際類型 object param = method.convertargstosqlcommandparam(args); if (method.hasrowbounds()) { rowbounds rowbounds = method.extractrowbounds(args); result = sqlsession.<e>selectlist(command.getname(), param, rowbounds); } else { result = sqlsession.<e>selectlist(command.getname(), param); } // 如果返回結果類型和method的返回結果類型不一致,則進行轉換數據結構 // 其實就是result返回結果不是list類型,而是其他集合類型或數組類型 if (!method.getreturntype().isassignablefrom(result.getclass())) { if (method.getreturntype().isarray()) { // 為數組結果 return converttoarray(result); } else { // 其他集合類型 return converttodeclaredcollection(sqlsession.getconfiguration(), result); } } return result; } |
代碼也不復雜,就是將得到的參數對應關系傳入,最終獲取結果,根據實際需求進行結果轉換。
3、對sql語句中參數的賦值
其實前面一篇博客中也有涉及到。參數賦值的位置在defaultparameterhandler類里面,可以查看前面一篇博客,這里不做過多介紹,傳送門 mybatis查詢語句的背后之封裝數據
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。
原文鏈接:http://www.cnblogs.com/qm-article/p/10658527.html