欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis省略@Param注解操作

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

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

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

我用的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ù)是怎么來的,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.只有這一個(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ì)于想省事的開發(fā)來說還是挺好用的

補(bǔ)充知識(shí):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>

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

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

相關(guān)文章

  • Java中使用RediSearch實(shí)現(xiàn)高效的數(shù)據(jù)檢索功能

    Java中使用RediSearch實(shí)現(xiàn)高效的數(shù)據(jù)檢索功能

    RediSearch是一款構(gòu)建在Redis上的搜索引擎,它為Redis數(shù)據(jù)庫提供了全文搜索、排序、過濾和聚合等高級(jí)查詢功能,本文將介紹如何在Java應(yīng)用中集成并使用RediSearch,以實(shí)現(xiàn)高效的數(shù)據(jù)檢索功能,感興趣的朋友跟著小編一起來看看吧
    2024-05-05
  • Springboot使用cache緩存過程代碼實(shí)例

    Springboot使用cache緩存過程代碼實(shí)例

    這篇文章主要介紹了Springboot使用cache緩存過程代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • java.sql.SQLRecoverableException關(guān)閉的連接異常問題及解決辦法

    java.sql.SQLRecoverableException關(guān)閉的連接異常問題及解決辦法

    當(dāng)數(shù)據(jù)庫連接池中的連接被創(chuàng)建而長時(shí)間不使用的情況下,該連接會(huì)自動(dòng)回收并失效,就導(dǎo)致客戶端程序報(bào)“ java.sql.SQLException: Io 異常: Connection reset” 或“java.sql.SQLException 關(guān)閉的連接”異常問題,下面給大家分享解決方案,一起看看吧
    2024-03-03
  • Java中判斷字符串是否相等的實(shí)現(xiàn)

    Java中判斷字符串是否相等的實(shí)現(xiàn)

    這篇文章主要介紹了Java中判斷字符串是否相等的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • MybatisPlus操作符和運(yùn)算值詳解

    MybatisPlus操作符和運(yùn)算值詳解

    在前端到后端的數(shù)據(jù)傳遞中,處理動(dòng)態(tài)運(yùn)算條件是一個(gè)常見的需求,本文介紹了如何在MybatisPlus中處理運(yùn)算符和運(yùn)算值的動(dòng)態(tài)拼接問題,感興趣的朋友一起看看吧
    2024-10-10
  • Java字符串詳解的實(shí)例介紹

    Java字符串詳解的實(shí)例介紹

    本篇文章介紹了,在Java中關(guān)于字符串詳解一些實(shí)例操作,需要的朋友參考下
    2013-04-04
  • Java?Stream?流中?Collectors.toMap?的用法詳解

    Java?Stream?流中?Collectors.toMap?的用法詳解

    這篇文章主要介紹了Stream?流中?Collectors.toMap?的用法,Collectors.toMap()方法是把List轉(zhuǎn)Map的操作,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2024-01-01
  • 詳解SpringBoot配置devtools實(shí)現(xiàn)熱部署

    詳解SpringBoot配置devtools實(shí)現(xiàn)熱部署

    本篇文章主要介紹了詳解SpringBoot配置devtools實(shí)現(xiàn)熱部署 ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-05-05
  • Java中快速把map轉(zhuǎn)成json格式的方法

    Java中快速把map轉(zhuǎn)成json格式的方法

    這篇文章主要介紹了Java中快速把map轉(zhuǎn)成json格式的方法,本文使用json-lib.jar中的JSONSerializer.toJSON方法實(shí)現(xiàn)快速把map轉(zhuǎn)換成json,需要的朋友可以參考下
    2015-07-07
  • java實(shí)現(xiàn)清理DNS Cache的方法

    java實(shí)現(xiàn)清理DNS Cache的方法

    這篇文章主要介紹了java實(shí)現(xiàn)清理DNS Cache的方法,分析了幾種常用的清理方法,并給出了反射清理的完整實(shí)例,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-01-01

最新評(píng)論