1. 程式人生 > >mybatis查詢語句的背後之參數解析

mybatis查詢語句的背後之參數解析

對應關系 類的構造函數 多個參數 col object c per 調用 文件中 config

轉載請註明出處。。。

一、前言

通過前面我們也知道,通過getMapper方式來進行查詢,最後會通過mapperMehod類,對接口中傳來的參數也會在這個類裏面進行一個解析,隨後就傳到對應位置,與sql裏面的參數進行一個匹配,最後獲取結果。對於mybatis通常傳參(這裏忽略掉Rowbounds和ResultHandler兩種類型)有幾種方式。

1、javabean類型參數

2、非javabean類型參數

註意,本文是基於mybatis3.5.0版本進行分析。

1、參數的存儲

2、對sql語句中參數的賦值

下面將圍繞這這兩方面進行

二、參數的存儲

先看下面一段代碼

 1
@Test 2 public void testSelectOrdinaryParam() throws Exception{ 3 SqlSession sqlSession = MybatisUtil.getSessionFactory().openSession(); 4 UserMapper mapper = sqlSession.getMapper(UserMapper.class); 5 List<User> userList = mapper.selectByOrdinaryParam("張三1號");
6 System.out.println(userList); 7 sqlSession.close(); 8 } 9 List<User> selectByOrdinaryParam(String username); // mapper接口 10 <select id="selectByOrdinaryParam" resultMap="BaseResultMap"> 11 select 12 <include refid="Base_Column_List"/> 13
from user 14 where username = #{username,jdbcType=VARCHAR} 15 </select>

或許有的人會奇怪,這個mapper接口沒有帶@Param註解,怎麽能在mapper配置文件中直接帶上參數名呢,不是會報錯嗎,

在mybatis裏面,對單個參數而言,直接使用參數名是沒問題的,如果是多個參數就不能這樣了,下面我們來了解下,mybatis的解析過程,請看下面代碼,位於MapperMehod類的內部類MethodSignature構造函數中

 1 public MethodSignature(Configuration configuration, Class<?> mapperInterface, Method method) {
 2       Type resolvedReturnType = TypeParameterResolver.resolveReturnType(method, mapperInterface);
 3       if (resolvedReturnType instanceof Class<?>) {
 4         this.returnType = (Class<?>) resolvedReturnType;
 5       } else if (resolvedReturnType instanceof ParameterizedType) {
 6         this.returnType = (Class<?>) ((ParameterizedType) resolvedReturnType).getRawType();
 7       } else {
 8         this.returnType = method.getReturnType();
 9       }
10       this.returnsVoid = void.class.equals(this.returnType);
11       this.returnsMany = configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray();
12       this.returnsCursor = Cursor.class.equals(this.returnType);
13       this.returnsOptional = Optional.class.equals(this.returnType);
14       this.mapKey = getMapKey(method);
15       this.returnsMap = this.mapKey != null;
16       this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class);
17       this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class);
18      // 參數解析類
19       this.paramNameResolver = new ParamNameResolver(configuration, method);
20     }

參數的存儲解析皆由ParamNameResolver類來進行操作,先看下該類的構造函數

 1 /**
 2  * config 全局的配置文件中心
 3  * method 實際執行的方法,也就是mapper接口中的抽象方法
 4  * 
 5  */
 6 public ParamNameResolver(Configuration config, Method method) {
 7     // 獲取method中的所有參數類型
 8     final Class<?>[] paramTypes = method.getParameterTypes();
 9     // 獲取參數中含有的註解,主要是為了@Param註解做準備
10     final Annotation[][] paramAnnotations = method.getParameterAnnotations();
11     final SortedMap<Integer, String> map = new TreeMap<>();
12     // 這裏實際上獲取的值就是參數的個數。也就是二維數組的行長度
13     int paramCount = paramAnnotations.length;
14     // get names from @Param annotations
15     for (int paramIndex = 0; paramIndex < paramCount; paramIndex++) {
16       // 排除RowBounds和ResultHandler兩種類型的參數
17       if (isSpecialParameter(paramTypes[paramIndex])) {
18         // skip special parameters
19         continue;
20       }
21       String name = null;
22       // 如果參數中含有@Param註解,則只用@Param註解的值作為參數名
23       for (Annotation annotation : paramAnnotations[paramIndex]) {
24         if (annotation instanceof Param) {
25           hasParamAnnotation = true;
26           name = ((Param) annotation).value();
27           break;
28         }
29       }
30       // 即參數沒有@Param註解
31       if (name == null) {
32         // 參數實際名稱,其實這個值默認就是true,具體可以查看Configuration類中的該屬性值,當然也可以在配置文件進行配置關閉
33         // 如果jdk處於1.8版本,且編譯時帶上了-parameters 參數,那麽獲取的就是實際的參數名,如methodA(String username)
34         // 獲取的就是username,否則獲取的就是args0  後面的數字就是參數所在位置
35         if (config.isUseActualParamName()) {
36           name = getActualParamName(method, paramIndex);
37         }
38         // 如果以上條件都不滿足,則將參數名配置為 0,1,2../
39         if (name == null) {
40           // use the parameter index as the name ("0", "1", ...)
41           // gcode issue #71
42           name = String.valueOf(map.size());
43         }
44       }
45       map.put(paramIndex, name);
46     }
47     names = Collections.unmodifiableSortedMap(map);
48   }

這個構造函數的作用就是對參數名稱進行一個封裝,得到一個 “參數位置-->參數名稱 “ 的一個map結構,這樣做的目的是為了替換參數值,我們也清楚,實際傳過來的參數就是一個一個Object數組結構,我們也可以將它理解為map結構。即 index --> 參數值,此就和之前的 map結構有了對應,也就最終可以得到一個 參數名稱 ---> 參數值 的一個對應關系。

 1 public Object execute(SqlSession sqlSession, Object[] args) {
 2     Object result;
 3     switch (command.getType()) {
 4       // 其它情況忽略掉
 5       case SELECT:
 6         // 這裏參數中含有resultHandler,暫不做討論
 7         if (method.returnsVoid() && method.hasResultHandler()) {
 8           executeWithResultHandler(sqlSession, args);
 9           result = null;
10         } else if (method.returnsMany()) {// 1、 返回結果為集合類型或數組類型,這種情況適用於大多數情況
11           result = executeForMany(sqlSession, args);
12         } else if (method.returnsMap()) {// 返回結果為Map類型
13           result = executeForMap(sqlSession, args);
14         } else if (method.returnsCursor()) {
15           result = executeForCursor(sqlSession, args);
16         } else {// 2、返回結果javabean類型,或普通的基礎類型及其包裝類等  
17           Object param = method.convertArgsToSqlCommandParam(args);
18           result = sqlSession.selectOne(command.getName(), param);
19           // 對java8中的optional進行了支持
20           if (method.returnsOptional() &&
21               (result == null || !method.getReturnType().equals(result.getClass()))) {
22             result = Optional.ofNullable(result);
23           }
24         }
25         break;
26       default:
27         throw new BindingException("Unknown execution method for: " + command.getName());
28     }
29     if (result == null && method.getReturnType().isPrimitive() && !method.returnsVoid()) {
30       throw new BindingException("Mapper method ‘" + command.getName()
31           + " attempted to return null from a method with a primitive return type (" + method.getReturnType() + ").");
32     }
33     return result;
34   }

這裏主要分析1情況。對於2情況也就是接下來要說的參數賦值情況,不過要先介紹下method.convertArgsToSqlCommandParam這代碼帶來的一個結果是怎麽樣的

 1 public Object convertArgsToSqlCommandParam(Object[] args) {
 2       return paramNameResolver.getNamedParams(args);
 3     }
 4 
 5 public Object getNamedParams(Object[] args) {
 6     final int paramCount = names.size();
 7     if (args == null || paramCount == 0) {
 8       return null;
 9     } else if (!hasParamAnnotation && paramCount == 1) {// 1
10       return args[names.firstKey()];
11     } else {
12       final Map<String, Object> param = new ParamMap<>();
13       int i = 0;
14       for (Map.Entry<Integer, String> entry : names.entrySet()) {
15         param.put(entry.getValue(), args[entry.getKey()]);
16         // add generic param names (param1, param2, ...)
17         final String genericParamName = GENERIC_NAME_PREFIX + String.valueOf(i + 1);
18         // ensure not to overwrite parameter named with @Param
19         if (!names.containsValue(genericParamName)) {
20           param.put(genericParamName, args[entry.getKey()]);
21         }
22         i++;
23       }
24       return param;
25     }
26   }

可以很清楚的知道最後又調用了ParamNameResolver類的getNamedPaams方法,這個方法的主要作用就是,將原來的參數位置 --> 參數名稱 映射關系轉為 參數名稱 --->參數值 ,並且新加一個參數名和參數值得一個對應關系。即

param1 ->參數值1

param2 -->參數值2

當然如果只有一個參數,如代碼中的1部分,若參數沒有@Param註解,且只有一個參數,則不會加入上述的一個對象關系,這也就是前面說的,對於單個參數,可以直接在sql中寫參數名就ok的原因。下面回到前面

 1 private <E> Object executeForMany(SqlSession sqlSession, Object[] args) {
 2     List<E> result;
 3     // 獲取對應的一個映射關系,param類型有可能為map或null或參數實際類型
 4     Object param = method.convertArgsToSqlCommandParam(args);
 5     if (method.hasRowBounds()) {
 6       RowBounds rowBounds = method.extractRowBounds(args);
 7       result = sqlSession.<E>selectList(command.getName(), param, rowBounds);
 8     } else {
 9       result = sqlSession.<E>selectList(command.getName(), param);
10     }
11     // 如果返回結果類型和method的返回結果類型不一致,則進行轉換數據結構
12     // 其實就是result返回結果不是List類型,而是其他集合類型或數組類型
13     if (!method.getReturnType().isAssignableFrom(result.getClass())) {
14       if (method.getReturnType().isArray()) {// 為數組結果
15         return convertToArray(result);
16       } else {// 其他集合類型
17         return convertToDeclaredCollection(sqlSession.getConfiguration(), result);
18       }
19     }
20     return result;
21   }

代碼也不復雜,就是將得到的參數對應關系傳入,最終獲取結果,根據實際需求進行結果轉換。

3、對sql語句中參數的賦值

其實前面一篇博客中也有涉及到。參數賦值的位置在DefaultParameterHandler類裏面,可以查看前面一篇博客,這裏不做過多介紹,傳送門 mybatis查詢語句的背後之封裝數據

---------------------------------------------------------------------------------------------------------------------------------------分界線--------------------------------------------------------------------------------------------------------

若有不足或錯誤之處,還望指正,謝謝!

mybatis查詢語句的背後之參數解析