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

mybatis省略@Param注解操作

 更新時(shí)間:2020年11月27日 10:17:09   作者:陽(yáng)光下的藍(lán)色街燈  
這篇文章主要介紹了mybatis省略@Param注解操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

項(xiàng)目是Springboot+mybatis,每次寫(xiě)一堆@Param注解感覺(jué)挺麻煩,就找方法想把這個(gè)注解給省了,最后確實(shí)找到一個(gè)方法

1.在mybatis的配置里有個(gè)屬性u(píng)seActualParamName,允許使用方法簽名中的名稱(chēng)作為語(yǔ)句參數(shù)名稱(chēng)

我用的mybatis:3.4.2版本Configuration中useActualParamName的默認(rèn)值為true

源碼簡(jiǎn)單分析:

MapperMethod的execute方法中獲取參數(shù)的方法convertArgsToSqlCommandParam
public Object execute(SqlSession sqlSession, Object[] args) {
  Object result;
  Object param;
  switch(this.command.getType()) {
  case INSERT:
    param = this.method.convertArgsToSqlCommandParam(args);
    result = this.rowCountResult(sqlSession.insert(this.command.getName(), param));
    break;
  case UPDATE:
    param = this.method.convertArgsToSqlCommandParam(args);
    result = this.rowCountResult(sqlSession.update(this.command.getName(), param));
    break;
  case DELETE:
    param = this.method.convertArgsToSqlCommandParam(args);
    result = this.rowCountResult(sqlSession.delete(this.command.getName(), param));
    break;
  case SELECT:
    if (this.method.returnsVoid() && this.method.hasResultHandler()) {
      this.executeWithResultHandler(sqlSession, args);
      result = null;
    } else if (this.method.returnsMany()) {
      result = this.executeForMany(sqlSession, args);
    } else if (this.method.returnsMap()) {
      result = this.executeForMap(sqlSession, args);
    } else if (this.method.returnsCursor()) {
      result = this.executeForCursor(sqlSession, args);
    } else {
      param = this.method.convertArgsToSqlCommandParam(args);
      result = sqlSession.selectOne(this.command.getName(), param);
      if (this.method.returnsOptional() && (result == null || !this.method.getReturnType().equals(result.getClass()))) {
        result = Optional.ofNullable(result);
      }
    }
    break;
  case FLUSH:
    result = sqlSession.flushStatements();
    break;
  default:
    throw new BindingException("Unknown execution method for: " + this.command.getName());
  }

  if (result == null && this.method.getReturnType().isPrimitive() && !this.method.returnsVoid()) {
    throw new BindingException("Mapper method '" + this.command.getName() + " attempted to return null from a method with a primitive return type (" + this.method.getReturnType() + ").");
  } else {
    return result;
  }
}

然后再看參數(shù)是怎么來(lái)的,convertArgsToSqlCommandParam在MapperMethod的內(nèi)部類(lèi)MethodSignature中:

public Object convertArgsToSqlCommandParam(Object[] args) {
  return this.paramNameResolver.getNamedParams(args);
}

getNamedParams在ParamNameResolver,看一下ParamNameResolver的構(gòu)造方法:

public ParamNameResolver(Configuration config, Method method) {
  Class<?>[] paramTypes = method.getParameterTypes();
  Annotation[][] paramAnnotations = method.getParameterAnnotations();
  SortedMap<Integer, String> map = new TreeMap();
  int paramCount = paramAnnotations.length;

  for(int paramIndex = 0; paramIndex < paramCount; ++paramIndex) {
    if (!isSpecialParameter(paramTypes[paramIndex])) {
      String name = null;
      Annotation[] var9 = paramAnnotations[paramIndex];
      int var10 = var9.length;

      for(int var11 = 0; var11 < var10; ++var11) {
        Annotation annotation = var9[var11];
        if (annotation instanceof Param) {
          this.hasParamAnnotation = true;
          name = ((Param)annotation).value();
          break;
        }
      }

      if (name == null) {
        if (config.isUseActualParamName()) {
          name = this.getActualParamName(method, paramIndex);
        }

        if (name == null) {
          name = String.valueOf(map.size());
        }
      }

      map.put(paramIndex, name);
    }
  }

  this.names = Collections.unmodifiableSortedMap(map);
}

isUseActualParamName出現(xiàn)了,總算找到正主了,前邊一堆都是瞎扯。

2.只有這一個(gè)屬性還不行,還要能取到方法里定義的參數(shù)名,這就需要java8的一個(gè)新特性了,在maven-compiler-plugin編譯器的配置項(xiàng)中配置-parameters參數(shù)。

在Java 8中這個(gè)特性是默認(rèn)關(guān)閉的,因此如果不帶-parameters參數(shù)編譯上述代碼并運(yùn)行,獲取到的參數(shù)名是arg0,arg1......

帶上這個(gè)參數(shù)后獲取到的參數(shù)名就是定義的參數(shù)名了,例如void test(String testArg1, String testArg2),取到的就是testArg1,testArg2。

最后就把@Param注解給省略了,對(duì)于想省事的開(kāi)發(fā)來(lái)說(shuō)還是挺好用的

補(bǔ)充知識(shí):mybatis使用@param("xxx")注解傳參和不使用的區(qū)別

我就廢話(huà)不多說(shuō)了,大家還是直接看代碼吧~

public interface SystemParameterMapper {
  int deleteByPrimaryKey(Integer id);

  int insert(SystemParameterDO record);

  SystemParameterDO selectByPrimaryKey(Integer id);//不使用注解

  List<SystemParameterDO> selectAll();

  int updateByPrimaryKey(SystemParameterDO record);

  SystemParameterDO getByParamID(@Param("paramID") String paramID);//使用注解
}

跟映射的xml

<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  select id, paramID, paramContent, paramType, memo
  from wh_system_parameter
  where id = #{id,jdbcType=INTEGER}
 </select>

<select id="getByParamID" resultMap="BaseResultMap">
  select id, paramID, paramContent, paramType, memo
  from wh_system_parameter
  where paramID = #{paramID}
 </select>

區(qū)別是:使用注解可以不用加parameterType

以上這篇mybatis省略@Param注解操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java中循環(huán)刪除list中元素的方法總結(jié)

    java中循環(huán)刪除list中元素的方法總結(jié)

    下面小編就為大家?guī)?lái)一篇java中循環(huán)刪除list中元素的方法總結(jié)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-12-12
  • 一篇文章帶你了解Java Stream流

    一篇文章帶你了解Java Stream流

    Stream流是數(shù)據(jù)渠道,用于操作數(shù)據(jù)源(集合、數(shù)組等)所生成的元素序列。這篇文章主要介紹了Java8新特性Stream流的相關(guān)資料,需要的朋友參考下吧
    2021-08-08
  • spring boot添加新模塊的方法教程

    spring boot添加新模塊的方法教程

    這篇文章主要給大家介紹了關(guān)于spring boot添加新模塊的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11
  • Java Socket實(shí)現(xiàn)多人聊天系統(tǒng)

    Java Socket實(shí)現(xiàn)多人聊天系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了Java Socket實(shí)現(xiàn)多人聊天系統(tǒng),具有圖形界面,實(shí)現(xiàn)文件傳輸功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-07-07
  • 基于Scala和Java方法的相互調(diào)用

    基于Scala和Java方法的相互調(diào)用

    這篇文章主要介紹了Scala和Java方法的相互調(diào)用,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java中的拷貝數(shù)組CopyOnWriteArrayList詳解

    Java中的拷貝數(shù)組CopyOnWriteArrayList詳解

    這篇文章主要介紹了Java中的拷貝數(shù)組CopyOnWriteArrayList詳解,ArrayList和LinkedList都不是線(xiàn)程安全的,如果需要線(xiàn)程安全的List,可以使用synchronizedList來(lái)生成一個(gè)同步list,但是這個(gè)同步list的方法都是通過(guò)synchronized修飾來(lái)保證同步的,需要的朋友可以參考下
    2023-12-12
  • java 調(diào)用本地?fù)P聲器的步驟

    java 調(diào)用本地?fù)P聲器的步驟

    博主的畢設(shè)系統(tǒng)在做一個(gè)餐廳的點(diǎn)餐管理系統(tǒng),在進(jìn)行移動(dòng)端頁(yè)面開(kāi)發(fā)的時(shí)候突發(fā)奇想做一個(gè)呼叫功能,因此就有了這篇文章
    2021-05-05
  • 在CentOS系統(tǒng)中檢測(cè)Java安裝及運(yùn)行jar應(yīng)用的方法

    在CentOS系統(tǒng)中檢測(cè)Java安裝及運(yùn)行jar應(yīng)用的方法

    這篇文章主要介紹了在CentOS系統(tǒng)中檢測(cè)Java安裝及運(yùn)行jar應(yīng)用的方法,同樣適用于Fedora等其他RedHat系的Linux系統(tǒng),需要的朋友可以參考下
    2015-06-06
  • java中的4種循環(huán)方法示例詳情

    java中的4種循環(huán)方法示例詳情

    大家好,本篇文章主要講的是java中的4種循環(huán)方法示例詳情,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話(huà)記得收藏一下,方便下次瀏覽
    2021-12-12
  • JAVA對(duì)象中使用?static?和?String?基礎(chǔ)探究

    JAVA對(duì)象中使用?static?和?String?基礎(chǔ)探究

    這篇文章主要介紹了JAVA對(duì)象中使用static和String基礎(chǔ)探究,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09

最新評(píng)論

丰镇市| 民勤县| 崇明县| 饶阳县| 新乡县| 拜城县| 汾西县| 庐江县| 德昌县| 阳高县| 苏州市| 盐城市| 长沙市| 通江县| 美姑县| 古蔺县| 思茅市| 峨眉山市| 华亭县| 兴海县| 上林县| 平武县| 沙洋县| 临沂市| 宁安市| 蒙城县| 秦皇岛市| SHOW| 阜康市| 墨脱县| 汨罗市| 阿拉善左旗| 丹江口市| 台州市| 辉南县| 三原县| 鸡西市| 湟中县| 泾源县| 奇台县| 铜陵市|