轉載請註明出處。。。html
經過前面咱們也知道,經過getMapper方式來進行查詢,最後會經過mapperMehod類,對接口中傳來的參數也會在這個類裏面進行一個解析,隨後就傳到對應位置,與sql裏面的參數進行一個匹配,最後獲取結果。對於mybatis一般傳參(這裏忽略掉Rowbounds和ResultHandler兩種類型)有幾種方式。java
一、javabean類型參數sql
二、非javabean類型參數數組
注意,本文是基於mybatis3.5.0版本進行分析。數據結構
一、參數的存儲mybatis
二、對sql語句中參數的賦值app
下面將圍繞這這兩方面進行函數
先看下面一段代碼post
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配置文件中直接帶上參數名呢,不是會報錯嗎,this
在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()) {// 一、 返回結果爲集合類型或數組類型,這種狀況適用於大多數狀況 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 {// 二、返回結果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 }
代碼也不復雜,就是將獲得的參數對應關係傳入,最終獲取結果,根據實際需求進行結果轉換。
其實前面一篇博客中也有涉及到。參數賦值的位置在DefaultParameterHandler類裏面,能夠查看前面一篇博客,這裏不作過多介紹,傳送門 mybatis查詢語句的背後之封裝數據
---------------------------------------------------------------------------------------------------------------------------------------分界線--------------------------------------------------------------------------------------------------------
如有不足或錯誤之處,還望指正,謝謝!