av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

mybatis省略@Param注解操作

瀏覽:120日期:2023-10-21 12:45:58

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

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

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

源碼簡單分析:

MapperMethod的execute方法中獲取參數的方法convertArgsToSqlCommandParampublic 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; }}

然后再看參數是怎么來的,convertArgsToSqlCommandParam在MapperMethod的內部類MethodSignature中:

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

getNamedParams在ParamNameResolver,看一下ParamNameResolver的構造方法:

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出現了,總算找到正主了,前邊一堆都是瞎扯。

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

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

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

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

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

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

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 parameterType='java.lang.Integer' resultMap='BaseResultMap'> select id, paramID, paramContent, paramType, memo from wh_system_parameter where id = #{id,jdbcType=INTEGER} </select><select resultMap='BaseResultMap'> select id, paramID, paramContent, paramType, memo from wh_system_parameter where paramID = #{paramID} </select>

區別是:使用注解可以不用加parameterType

以上這篇mybatis省略@Param注解操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 中文字幕亚洲视频 | 午夜免费视频 | 国产欧美一区二区三区久久人妖 | h视频在线观看免费 | 国产精品成人一区二区三区夜夜夜 | 黄色片网站在线观看 | 中文字幕在线视频一区二区三区 | www视频在线观看 | 免费成人在线网 | 国产精品视频久久久 | 天天操天天干天天透 | 日韩欧美亚洲 | 欧美一区二区三区视频在线 | 日本精品在线观看 | 中文在线一区二区 | 国产亚洲人成a在线v网站 | 久久久久黑人 | 亚洲精品九九 | 久久久成人免费一区二区 | 日韩有码在线播放 | 久久精品国产99国产精品 | 国产在线一区二区三区 | 电影在线 | 精品中文字幕视频 | 狠狠热视频| 精品免费国产一区二区三区四区介绍 | 久久的色| 亚洲草草视频 | 欧美日韩精品一区 | 色婷婷av99xx| 欧美一级电影免费观看 | 精品一区二区三区四区外站 | 成人毛片在线视频 | 日韩一区二区av | 成人性生交大片免费看中文带字幕 | 蜜臀网| 国产成人精品久久二区二区91 | 91视频网 | 欧美男人天堂 | 日韩电影中文字幕 | 国产精品久久久久久久久久妇女 |