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

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

mybatis省略@Param注解操作

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

項(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ù)的方法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; }}

然后再看參數(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 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>

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

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

相關(guān)文章:
主站蜘蛛池模板: 精品伊人久久 | 亚洲高清在线 | 国产美女视频网站 | 午夜一级片 | 日韩在线免费观看视频 | 一区二区三区视频在线 | 欧美顶级黄色大片免费 | 免费av网站在线观看 | 亚洲精品在 | 亚洲精品911 | 欧美狠狠操 | 午夜影院在线 | 激情视频网址 | 久热精品在线 | www久| 成人福利在线 | 国产在线成人 | 五月婷婷中文字幕 | 久久99九九 | 欧美激情国产精品 | 国产69精品久久久久久 | 国产欧美日韩综合 | 日韩天堂在线 | 不卡免费视频 | 国产成人精品免费 | 在线日韩一区 | www色| 黑人精品xxx一区一二区 | 伊人久久免费视频 | 91精品国产99久久久久久红楼 | 午夜视频在线免费观看 | 欧美一级特黄aaaaaa | 精品福利一区 | 91在线小视频| 欧美黄色一级视频 | 久久精品一区二区三区四区五区 | 国产精品一区二区在线免费观看 | 日本免费毛片 | 毛片网站视频 | 午夜免费视频 | 久久成人综合 |