mybatis查詢語(yǔ)句揭秘之參數(shù)解析
一、前言
通過前面我們也知道,通過getMapper方式來(lái)進(jìn)行查詢,最后會(huì)通過mapperMehod類,對(duì)接口中傳來(lái)的參數(shù)也會(huì)在這個(gè)類里面進(jìn)行一個(gè)解析,隨后就傳到對(duì)應(yīng)位置,與sql里面的參數(shù)進(jìn)行一個(gè)匹配,最后獲取結(jié)果。對(duì)于mybatis通常傳參(這里忽略掉Rowbounds和ResultHandler兩種類型)有幾種方式。
1、javabean類型參數(shù)
2、非javabean類型參數(shù)
注意,本文是基于mybatis3.5.0版本進(jìn)行分析。
1、參數(shù)的存儲(chǔ)
2、對(duì)sql語(yǔ)句中參數(shù)的賦值
下面將圍繞這這兩方面進(jìn)行
二、參數(shù)的存儲(chǔ)
先看下面一段代碼
@Test public void testSelectOrdinaryParam() throws Exception{ SqlSession sqlSession = MybatisUtil.getSessionFactory().openSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.selectByOrdinaryParam("張三1號(hào)"); 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>
或許有的人會(huì)奇怪,這個(gè)mapper接口沒有帶@Param注解,怎么能在mapper配置文件中直接帶上參數(shù)名呢,不是會(huì)報(bào)錯(cuò)嗎,
在mybatis里面,對(duì)單個(gè)參數(shù)而言,直接使用參數(shù)名是沒問題的,如果是多個(gè)參數(shù)就不能這樣了,下面我們來(lái)了解下,mybatis的解析過程,請(qǐng)看下面代碼,位于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ù)的存儲(chǔ)解析皆由ParamNameResolver類來(lái)進(jìn)行操作,先看下該類的構(gòu)造函數(shù)
/** * 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ù)的個(gè)數(shù)。也就是二維數(shù)組的行長(zhǎng)度 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í)這個(gè)值默認(rèn)就是true,具體可以查看Configuration類中的該屬性值,當(dāng)然也可以在配置文件進(jìn)行配置關(guān)閉 // 如果jdk處于1.8版本,且編譯時(shí)帶上了-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è)構(gòu)造函數(shù)的作用就是對(duì)參數(shù)名稱進(jìn)行一個(gè)封裝,得到一個(gè) “參數(shù)位置-->參數(shù)名稱 “ 的一個(gè)map結(jié)構(gòu),這樣做的目的是為了替換參數(shù)值,我們也清楚,實(shí)際傳過來(lái)的參數(shù)就是一個(gè)一個(gè)Object數(shù)組結(jié)構(gòu),我們也可以將它理解為map結(jié)構(gòu)。即 index --> 參數(shù)值,此就和之前的 map結(jié)構(gòu)有了對(duì)應(yīng),也就最終可以得到一個(gè) 參數(shù)名稱 ---> 參數(shù)值 的一個(gè)對(duì)應(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); // 對(duì)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情況。對(duì)于2情況也就是接下來(lái)要說(shuō)的參數(shù)賦值情況,不過要先介紹下method.convertArgsToSqlCommandParam這代碼帶來(lái)的一個(gè)結(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方法,這個(gè)方法的主要作用就是,將原來(lái)的參數(shù)位置 --> 參數(shù)名稱 映射關(guān)系轉(zhuǎn)為 參數(shù)名稱 --->參數(shù)值 ,并且新加一個(gè)參數(shù)名和參數(shù)值得一個(gè)對(duì)應(yīng)關(guān)系。即
param1 ->參數(shù)值1
param2 -->參數(shù)值2
當(dāng)然如果只有一個(gè)參數(shù),如代碼中的1部分,若參數(shù)沒有@Param注解,且只有一個(gè)參數(shù),則不會(huì)加入上述的一個(gè)對(duì)象關(guān)系,這也就是前面說(shuō)的,對(duì)于單個(gè)參數(shù),可以直接在sql中寫參數(shù)名就ok的原因。下面回到前面
private <E> Object executeForMany(SqlSession sqlSession, Object[] args) { List<E> result; // 獲取對(duì)應(yīng)的一個(gè)映射關(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ù)對(duì)應(yīng)關(guān)系傳入,最終獲取結(jié)果,根據(jù)實(shí)際需求進(jìn)行結(jié)果轉(zhuǎn)換。
3、對(duì)sql語(yǔ)句中參數(shù)的賦值
其實(shí)前面一篇博客中也有涉及到。參數(shù)賦值的位置在DefaultParameterHandler類里面,可以查看前面一篇博客,這里不做過多介紹,傳送門 mybatis查詢語(yǔ)句的背后之封裝數(shù)據(jù)
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Java創(chuàng)建和啟動(dòng)線程的兩種方式實(shí)例分析
這篇文章主要介紹了Java創(chuàng)建和啟動(dòng)線程的兩種方式,結(jié)合實(shí)例形式分析了java多線程創(chuàng)建、使用相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-09-09Spring MVC-@RequestMapping注解詳解
@RequestMapping注解的作用,就是將請(qǐng)求和處理請(qǐng)求的控制器方法關(guān)聯(lián)起來(lái),建立映射關(guān)系。這篇文章主要給大家介紹了關(guān)于SpringMVC中@RequestMapping注解用法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04Java開發(fā)之Spring連接數(shù)據(jù)庫(kù)方法實(shí)例分析
這篇文章主要介紹了Java開發(fā)之Spring連接數(shù)據(jù)庫(kù)方法,以實(shí)例形式較為詳細(xì)的分析了Java Spring開發(fā)中針對(duì)數(shù)據(jù)庫(kù)的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10springsecurity實(shí)現(xiàn)用戶登錄認(rèn)證快速使用示例代碼(前后端分離項(xiàng)目)
這篇文章主要介紹了springsecurity實(shí)現(xiàn)用戶登錄認(rèn)證快速使用示例代碼(前后端分離項(xiàng)目),本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-03-03Java基于jeeplus vue實(shí)現(xiàn)簡(jiǎn)單工作流過程圖解
這篇文章主要介紹了Java基于jeeplus vue實(shí)現(xiàn)簡(jiǎn)單工作流過程圖解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Java Swing實(shí)現(xiàn)餐廳點(diǎn)餐系統(tǒng)源碼(收藏版)
這篇文章主要介紹了Java Swing實(shí)現(xiàn)餐廳點(diǎn)餐系統(tǒng)源碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02Spring Boot 如何將 Word 轉(zhuǎn)換為 PDF
這篇文章主要介紹了Spring Boot將Word轉(zhuǎn)換為 PDF,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08