mybatis查詢語句揭秘之參數(shù)解析
一、前言
通過前面我們也知道,通過getMapper方式來進行查詢,最后會通過mapperMehod類,對接口中傳來的參數(shù)也會在這個類里面進行一個解析,隨后就傳到對應(yīng)位置,與sql里面的參數(shù)進行一個匹配,最后獲取結(jié)果。對于mybatis通常傳參(這里忽略掉Rowbounds和ResultHandler兩種類型)有幾種方式。
1、javabean類型參數(shù)
2、非javabean類型參數(shù)
注意,本文是基于mybatis3.5.0版本進行分析。
1、參數(shù)的存儲
2、對sql語句中參數(shù)的賦值
下面將圍繞這這兩方面進行
二、參數(shù)的存儲
先看下面一段代碼
@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ù)名呢,不是會報錯嗎,
在mybatis里面,對單個參數(shù)而言,直接使用參數(shù)名是沒問題的,如果是多個參數(shù)就不能這樣了,下面我們來了解下,mybatis的解析過程,請看下面代碼,位于MapperMehod類的內(nèi)部類MethodSignature構(gòu)造函數(shù)中
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類來進行操作,先看下該類的構(gòu)造函數(shù)
/** * config 全局的配置文件中心 * method 實際執(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ù)組的行長度 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ù)實際名稱,其實這個值默認(rèn)就是true,具體可以查看Configuration類中的該屬性值,當(dāng)然也可以在配置文件進行配置關(guān)閉 // 如果jdk處于1.8版本,且編譯時帶上了-parameters 參數(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ù)名稱進行一個封裝,得到一個 “參數(shù)位置-->參數(shù)名稱 “ 的一個map結(jié)構(gòu),這樣做的目的是為了替換參數(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)系。
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進行了支持 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é)果是怎么樣的
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的原因。下面回到前面
private <E> Object executeForMany(SqlSession sqlSession, Object[] args) { List<E> result; // 獲取對應(yīng)的一個映射關(guān)系,param類型有可能為map或null或參數(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é)果類型不一致,則進行轉(zhuǎn)換數(shù)據(jù)結(jié)構(gòu) // 其實就是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ù)實際需求進行結(jié)果轉(zhuǎn)換。
3、對sql語句中參數(shù)的賦值
其實前面一篇博客中也有涉及到。參數(shù)賦值的位置在DefaultParameterHandler類里面,可以查看前面一篇博客,這里不做過多介紹,傳送門 mybatis查詢語句的背后之封裝數(shù)據(jù)
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
Spring MVC-@RequestMapping注解詳解
@RequestMapping注解的作用,就是將請求和處理請求的控制器方法關(guān)聯(lián)起來,建立映射關(guān)系。這篇文章主要給大家介紹了關(guān)于SpringMVC中@RequestMapping注解用法,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-04-04Java開發(fā)之Spring連接數(shù)據(jù)庫方法實例分析
這篇文章主要介紹了Java開發(fā)之Spring連接數(shù)據(jù)庫方法,以實例形式較為詳細的分析了Java Spring開發(fā)中針對數(shù)據(jù)庫的相關(guān)操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10springsecurity實現(xiàn)用戶登錄認(rèn)證快速使用示例代碼(前后端分離項目)
這篇文章主要介紹了springsecurity實現(xiàn)用戶登錄認(rèn)證快速使用示例代碼(前后端分離項目),本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-03-03Java基于jeeplus vue實現(xiàn)簡單工作流過程圖解
這篇文章主要介紹了Java基于jeeplus vue實現(xiàn)簡單工作流過程圖解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-04-04Java Swing實現(xiàn)餐廳點餐系統(tǒng)源碼(收藏版)
這篇文章主要介紹了Java Swing實現(xiàn)餐廳點餐系統(tǒng)源碼,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02Spring Boot 如何將 Word 轉(zhuǎn)換為 PDF
這篇文章主要介紹了Spring Boot將Word轉(zhuǎn)換為 PDF,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-08-08