mybatis省略@Param注解操作

 更新时间:2020年11月27日 10:17:09   作者:阳光下的蓝色街灯  
这篇文章主要介绍了mybatis省略@Param注解操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

项目是Springboot+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>

区别是:使用注解可以不用加parameterType

以上这篇mybatis省略@Param注解操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • java中循环删除list中元素的方法总结

    java中循环删除list中元素的方法总结

    下面小编就为大家带来一篇java中循环删除list中元素的方法总结。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • 一篇文章带你了解Java Stream流

    一篇文章带你了解Java Stream流

    Stream流是数据渠道,用于操作数据源(集合、数组等)所生成的元素序列。这篇文章主要介绍了Java8新特性Stream流的相关资料,需要的朋友参考下吧
    2021-08-08
  • spring boot添加新模块的方法教程

    spring boot添加新模块的方法教程

    这篇文章主要给大家介绍了关于spring boot添加新模块的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-11-11
  • Java Socket实现多人聊天系统

    Java Socket实现多人聊天系统

    这篇文章主要为大家详细介绍了Java Socket实现多人聊天系统,具有图形界面,实现文件传输功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-07-07
  • 基于Scala和Java方法的相互调用

    基于Scala和Java方法的相互调用

    这篇文章主要介绍了Scala和Java方法的相互调用,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Java中的拷贝数组CopyOnWriteArrayList详解

    Java中的拷贝数组CopyOnWriteArrayList详解

    这篇文章主要介绍了Java中的拷贝数组CopyOnWriteArrayList详解,ArrayList和LinkedList都不是线程安全的,如果需要线程安全的List,可以使用synchronizedList来生成一个同步list,但是这个同步list的方法都是通过synchronized修饰来保证同步的,需要的朋友可以参考下
    2023-12-12
  • java 调用本地扬声器的步骤

    java 调用本地扬声器的步骤

    博主的毕设系统在做一个餐厅的点餐管理系统,在进行移动端页面开发的时候突发奇想做一个呼叫功能,因此就有了这篇文章
    2021-05-05
  • 在CentOS系统中检测Java安装及运行jar应用的方法

    在CentOS系统中检测Java安装及运行jar应用的方法

    这篇文章主要介绍了在CentOS系统中检测Java安装及运行jar应用的方法,同样适用于Fedora等其他RedHat系的Linux系统,需要的朋友可以参考下
    2015-06-06
  • java中的4种循环方法示例详情

    java中的4种循环方法示例详情

    大家好,本篇文章主要讲的是java中的4种循环方法示例详情,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下,方便下次浏览
    2021-12-12
  • JAVA对象中使用 static 和 String 基础探究

    JAVA对象中使用 static 和 String 基础探究

    这篇文章主要介绍了JAVA对象中使用static和String基础探究,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09

最新评论