一、前言
通過前面我們也知道,通過getmapper方式來進(jìn)行查詢,最后會通過mappermehod類,對接口中傳來的參數(shù)也會在這個類里面進(jìn)行一個解析,隨后就傳到對應(yīng)位置,與sql里面的參數(shù)進(jìn)行一個匹配,最后獲取結(jié)果。對于mybatis通常傳參(這里忽略掉rowbounds和resulthandler兩種類型)有幾種方式。
1、javabean類型參數(shù)
2、非javabean類型參數(shù)
注意,本文是基于mybatis3.5.0版本進(jìn)行分析。
1、參數(shù)的存儲
2、對sql語句中參數(shù)的賦值
下面將圍繞這這兩方面進(jìn)行
二、參數(shù)的存儲
先看下面一段代碼
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配置文件中直接帶上參數(shù)名呢,不是會報(bào)錯嗎,
在mybatis里面,對單個參數(shù)而言,直接使用參數(shù)名是沒問題的,如果是多個參數(shù)就不能這樣了,下面我們來了解下,mybatis的解析過程,請看下面代碼,位于mappermehod類的內(nèi)部類methodsignature構(gòu)造函數(shù)中
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 ); // 參數(shù)解析類 this .paramnameresolver = new paramnameresolver(configuration, method); } |
參數(shù)的存儲解析皆由paramnameresolver類來進(jìn)行操作,先看下該類的構(gòu)造函數(shù)
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 實(shí)際執(zhí)行的方法,也就是mapper接口中的抽象方法 * */ public paramnameresolver(configuration config, method method) { // 獲取method中的所有參數(shù)類型 final class <?>[] paramtypes = method.getparametertypes(); // 獲取參數(shù)中含有的注解,主要是為了@param注解做準(zhǔn)備 final annotation[][] paramannotations = method.getparameterannotations(); final sortedmap<integer, string> map = new treemap<>(); // 這里實(shí)際上獲取的值就是參數(shù)的個數(shù)。也就是二維數(shù)組的行長度 int paramcount = paramannotations.length; // get names from @param annotations for ( int paramindex = 0 ; paramindex < paramcount; paramindex++) { // 排除rowbounds和resulthandler兩種類型的參數(shù) if (isspecialparameter(paramtypes[paramindex])) { // skip special parameters continue ; } string name = null ; // 如果參數(shù)中含有@param注解,則只用@param注解的值作為參數(shù)名 for (annotation annotation : paramannotations[paramindex]) { if (annotation instanceof param) { hasparamannotation = true ; name = ((param) annotation).value(); break ; } } // 即參數(shù)沒有@param注解 if (name == null ) { // 參數(shù)實(shí)際名稱,其實(shí)這個值默認(rèn)就是true,具體可以查看configuration類中的該屬性值,當(dāng)然也可以在配置文件進(jìn)行配置關(guān)閉 // 如果jdk處于1.8版本,且編譯時帶上了-parameters 參數(shù),那么獲取的就是實(shí)際的參數(shù)名,如methoda(string username) // 獲取的就是username,否則獲取的就是args0 后面的數(shù)字就是參數(shù)所在位置 if (config.isuseactualparamname()) { name = getactualparamname(method, paramindex); } // 如果以上條件都不滿足,則將參數(shù)名配置為 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); } |
這個構(gòu)造函數(shù)的作用就是對參數(shù)名稱進(jìn)行一個封裝,得到一個 “參數(shù)位置-->參數(shù)名稱 “ 的一個map結(jié)構(gòu),這樣做的目的是為了替換參數(shù)值,我們也清楚,實(shí)際傳過來的參數(shù)就是一個一個object數(shù)組結(jié)構(gòu),我們也可以將它理解為map結(jié)構(gòu)。即 index --> 參數(shù)值,此就和之前的 map結(jié)構(gòu)有了對應(yīng),也就最終可以得到一個 參數(shù)名稱 ---> 參數(shù)值 的一個對應(yīng)關(guān)系。
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: // 這里參數(shù)中含有resulthandler,暫不做討論 if (method.returnsvoid() && method.hasresulthandler()) { executewithresulthandler(sqlsession, args); result = null ; } else if (method.returnsmany()) { // 1、 返回結(jié)果為集合類型或數(shù)組類型,這種情況適用于大多數(shù)情況 result = executeformany(sqlsession, args); } else if (method.returnsmap()) { // 返回結(jié)果為map類型 result = executeformap(sqlsession, args); } else if (method.returnscursor()) { result = executeforcursor(sqlsession, args); } else { // 2、返回結(jié)果javabean類型,或普通的基礎(chǔ)類型及其包裝類等 object param = method.convertargstosqlcommandparam(args); result = sqlsession.selectone(command.getname(), param); // 對java8中的optional進(jìn)行了支持 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情況也就是接下來要說的參數(shù)賦值情況,不過要先介紹下method.convertargstosqlcommandparam這代碼帶來的一個結(jié)果是怎么樣的
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; } } |
可以很清楚的知道最后又調(diào)用了paramnameresolver類的getnamedpaams方法,這個方法的主要作用就是,將原來的參數(shù)位置 --> 參數(shù)名稱 映射關(guān)系轉(zhuǎn)為 參數(shù)名稱 --->參數(shù)值 ,并且新加一個參數(shù)名和參數(shù)值得一個對應(yīng)關(guān)系。即
param1 ->參數(shù)值1
param2 -->參數(shù)值2
當(dāng)然如果只有一個參數(shù),如代碼中的1部分,若參數(shù)沒有@param注解,且只有一個參數(shù),則不會加入上述的一個對象關(guān)系,這也就是前面說的,對于單個參數(shù),可以直接在sql中寫參數(shù)名就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; // 獲取對應(yīng)的一個映射關(guān)系,param類型有可能為map或null或參數(shù)實(shí)際類型 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); } // 如果返回結(jié)果類型和method的返回結(jié)果類型不一致,則進(jìn)行轉(zhuǎn)換數(shù)據(jù)結(jié)構(gòu) // 其實(shí)就是result返回結(jié)果不是list類型,而是其他集合類型或數(shù)組類型 if (!method.getreturntype().isassignablefrom(result.getclass())) { if (method.getreturntype().isarray()) { // 為數(shù)組結(jié)果 return converttoarray(result); } else { // 其他集合類型 return converttodeclaredcollection(sqlsession.getconfiguration(), result); } } return result; } |
代碼也不復(fù)雜,就是將得到的參數(shù)對應(yīng)關(guān)系傳入,最終獲取結(jié)果,根據(jù)實(shí)際需求進(jìn)行結(jié)果轉(zhuǎn)換。
3、對sql語句中參數(shù)的賦值
其實(shí)前面一篇博客中也有涉及到。參數(shù)賦值的位置在defaultparameterhandler類里面,可以查看前面一篇博客,這里不做過多介紹,傳送門 mybatis查詢語句的背后之封裝數(shù)據(jù)
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對服務(wù)器之家的支持。
原文鏈接:http://www.cnblogs.com/qm-article/p/10658527.html