午夜剧场伦理_日本一道高清_国产又黄又硬_91黄色网战_女同久久另类69精品国产_妹妹的朋友在线

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

mybatis省略@Param注解操作

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

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

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

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

源碼簡單分析:

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.只有這一個屬性還不行,還要能取到方法里定義的參數(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 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注解操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Mybatis 數(shù)據(jù)庫
相關(guān)文章:
主站蜘蛛池模板: 亚洲天堂福利 | 91精品国产综合久久久蜜臀粉嫩 | 欧美激情精品久久久久久蜜臀 | 成人毛片在线播放 | 国产精品高潮视频 | 国产精选在线 | 免费日本黄色网址 | 国产男女无套免费网站 | 视频一区二区在线 | 精品自拍视频 | 成年人视频在线免费看 | 中文成人在线 | 天天操婷婷 | 日韩精品在线视频观看 | 国产精品久久久精品四季影院 | 999国产视频| 91麻豆产精品久久久久久 | 水果视频污| 欧美一级欧美三级 | 中国黄色小视频 | 亚洲资源在线播放 | 四虎音影 | 黄色片网站在线播放 | 日韩av免费播放 | 欧美国产日韩视频 | 国产日韩欧美在线观看视频 | 欧美一级一区二区三区 | 国产毛片欧美毛片久久久 | 亚洲免费av一区二区 | 色先锋影音资源 | 永久免费看mv网站入口 | 欧美性久久久 | av在线首页 | 看真人毛片| 五月天婷婷激情 | 激情开心成人网 | 亚洲男女av| 午夜免费av | 色资源在线观看 | 国产一区二区三区视频 | 欧美一区二区三区精品 |