温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在mybatis中实现省略@Param注解

发布时间:2020-11-30 16:36:15 来源:亿速云 阅读:406 作者:Leah 栏目:开发技术

如何在mybatis中实现省略@Param注解?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

1.在mybatis的配置里有个属性useActualParamName,允许使用方法签名中的名称作为语句参数名称

我用的mybatis:3.4.2版本Configuration中useActualParamName的默认值为true

源码简单分析:

MapperMethod的execute方法中获取参数的方法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;   } }

然后再看参数是怎么来的,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 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>

关于如何在mybatis中实现省略@Param注解问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI