最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

mybatis查詢語句揭秘之參數(shù)解析

 更新時間:2019年04月07日 09:37:47   作者:不懂是非  
這篇文章主要給大家介紹了關(guān)于mybatis查詢語句之參數(shù)解析的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用mybatis具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧

一、前言

通過前面我們也知道,通過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ù)的存儲

先看下面一段代碼

@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類來進(jìn)行操作,先看下該類的構(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)然也可以在配置文件進(jìn)行配置關(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ù)名稱進(jìn)行一個封裝,得到一個  “參數(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進(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é)果是怎么樣的

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é)果類型不一致,則進(jìn)行轉(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ù)實際需求進(jìn)行結(jié)果轉(zhuǎn)換。

3、對sql語句中參數(shù)的賦值

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

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。

相關(guān)文章

  • ElasticSearch自定義注解增刪改方式

    ElasticSearch自定義注解增刪改方式

    這篇文章主要介紹了ElasticSearch自定義注解增刪改方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • Tomcat能起開,但是訪問不進(jìn)8080首頁的問題解決方案

    Tomcat能起開,但是訪問不進(jìn)8080首頁的問題解決方案

    這篇文章主要介紹了Tomcat能起開,但是訪問不進(jìn)8080首頁的問題解決方案的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • 一文解析Apache?Avro數(shù)據(jù)

    一文解析Apache?Avro數(shù)據(jù)

    本文是avro解析的demo,當(dāng)前FlinkSQL僅適用于簡單的avro數(shù)據(jù)解析,復(fù)雜嵌套avro數(shù)據(jù)暫時不支持。本文主要解析Apache?Avro數(shù)據(jù)的相關(guān)內(nèi)容,感興趣的朋友一起看看吧
    2021-12-12
  • 基于ComponentScan注解的掃描范圍及源碼解析

    基于ComponentScan注解的掃描范圍及源碼解析

    這篇文章主要介紹了基于ComponentScan注解的掃描范圍及源碼解析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Java Yml格式轉(zhuǎn)換為Properties問題

    Java Yml格式轉(zhuǎn)換為Properties問題

    本文介紹了作者編寫一個Java工具類來解決在線YAML到Properties轉(zhuǎn)換時屬性內(nèi)容遺漏的問題,通過遍歷YAML文件的樹結(jié)構(gòu),作者成功實現(xiàn)了屬性的完整轉(zhuǎn)換,總結(jié)指出,該工具類適用于多種數(shù)據(jù)類型,并且代碼簡潔易懂
    2024-12-12
  • Java多線程連續(xù)打印abc實現(xiàn)方法詳解

    Java多線程連續(xù)打印abc實現(xiàn)方法詳解

    這篇文章主要介紹了Java多線程連續(xù)打印abc實現(xiàn)方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-03-03
  • Java 前臺加后臺精品圖書管理系統(tǒng)的實現(xiàn)

    Java 前臺加后臺精品圖書管理系統(tǒng)的實現(xiàn)

    相信每一個學(xué)生學(xué)編程的時候,應(yīng)該都會寫一個小項目——圖書管理系統(tǒng)。為什么這么說呢?我認(rèn)為一個學(xué)校的氛圍很大一部分可以從圖書館的氛圍看出來,而圖書管理系統(tǒng)這個不大不小的項目,接觸的多,也比較熟悉,不會有陌生感,能夠練手,又有些難度,所以我的小項目也來了
    2021-11-11
  • Spring Hibernate實現(xiàn)分頁功能

    Spring Hibernate實現(xiàn)分頁功能

    這篇文章主要為大家詳細(xì)介紹了Spring Hibernate實現(xiàn)分頁功能的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • SpringBoot disruptor高性能隊列使用

    SpringBoot disruptor高性能隊列使用

    這篇文章主要介紹了SpringBoot disruptor高性能隊列使用,Disruptor是英國外匯交易公司LMAX開發(fā)的一個高性能隊列,研發(fā)的初衷是解決內(nèi)存隊列的延遲問題
    2023-02-02
  • Java8與Scala中的Lambda表達(dá)式深入講解

    Java8與Scala中的Lambda表達(dá)式深入講解

    這篇文章主要給大家介紹了關(guān)于Java8與Scala中Lambda表達(dá)式的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11

最新評論

边坝县| 杭锦后旗| 三穗县| 慈溪市| 运城市| 那曲县| 西畴县| 吴旗县| 云梦县| 科技| 盐边县| 荔浦县| 山东省| 东乌珠穆沁旗| 哈密市| 自治县| 永年县| 建水县| 安泽县| 光山县| 东源县| 金川县| 广安市| 南宁市| 锦屏县| 焦作市| 兴文县| 仪陇县| 商南县| 昌邑市| 泌阳县| 辉县市| 寿宁县| 房产| 公安县| 昌乐县| 河西区| 清远市| 大余县| 博乐市| 理塘县|