欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

如何在mybatis中實現(xiàn)省略@Param注解-創(chuàng)新互聯(lián)

如何在mybatis中實現(xiàn)省略@Param注解?針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:申請域名、網(wǎng)頁空間、營銷軟件、網(wǎng)站建設(shè)、林州網(wǎng)站維護、網(wǎng)站推廣。

1.在mybatis的配置里有個屬性useActualParamName,允許使用方法簽名中的名稱作為語句參數(shù)名稱


我用的mybatis:3.4.2版本Configuration中useActualParamName的默認值為true

源碼簡單分析:

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ù)是怎么來的,convertArgsToSqlCommandParam在MapperMethod的內(nè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.只有這一個屬性還不行,還要能取到方法里定義的參數(shù)名,這就需要java8的一個新特性了,在maven-compiler-plugin編譯器的配置項中配置-parameters參數(shù)。

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

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

最后就把@Param注解給省略了,對于想省事的開發(fā)來說還是挺好用的

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

我就廢話不多說了,大家還是直接看代碼吧~

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>

關(guān)于如何在mybatis中實現(xiàn)省略@Param注解問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。

本文題目:如何在mybatis中實現(xiàn)省略@Param注解-創(chuàng)新互聯(lián)
文章網(wǎng)址:http://chinadenli.net/article22/ddehjc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、品牌網(wǎng)站設(shè)計網(wǎng)站設(shè)計、外貿(mào)建站、營銷型網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都定制網(wǎng)站網(wǎng)頁設(shè)計