mybatis省略@Param注解操作
項(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)文章:
1. 關(guān)于Oracle中sqlldr的用法大全2. MySQL修改字符集的實(shí)戰(zhàn)教程3. 探討SQL Server 2005.NET CLR編程4. SQL SERVER 2005 EXPRESS不能遠(yuǎn)程連接的問題5. SQL Server 2000企業(yè)版安裝教程6. 使用SQL語句快速獲取SQL Server數(shù)據(jù)字典7. Oracle中的高效SQL編寫PARALLEL解析8. Microsoft SQL Server 查詢處理器的內(nèi)部機(jī)制與結(jié)構(gòu)(1)9. SQL Server中, DateTime (日期)型操作10. 一個(gè)SQL Server Sa密碼破解的存儲(chǔ)過程
