Java 使用参数名称时映射器出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19991023/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:06:13  来源:igfitidea点击:

Error in mapper when using parameter name

javamybatis

提问by mrkb80

I am using Spring 3.2.0 MVC with mybatis 3.2.3 and mybatis-spring 1.2.1 with ojdbc6 11.2.0.2.0

我正在使用带有 mybatis 3.2.3 的 Spring 3.2.0 MVC 和带有 ojdbc6 11.2.0.2.0 的 mybatis-spring 1.2.1

I have an XML mapper defined with 2 parameters of different types (date and integer). I reference them in the query as #{myid} and #{mydate} but I get an error from ibatis:

我有一个用 2 个不同类型(日期和整数)参数定义的 XML 映射器。我在查询中将它们引用为 #{myid} 和 #{mydate} 但我从 ibatis 收到错误:

org.apache.ibatis.binding.BindingException: Parameter 'myid' not found. Available parameters are [1, 0, param1, param2]

If I reference the parameters as #{0} and #{1} everything works fine.

如果我将参数引用为 #{0} 和 #{1},一切正常。

I have a another mapper with only a single parameter and I am able to reference the parameter as #{myDate} (the only difference is that I have the following in the XML:

我有一个只有一个参数的另一个映射器,我可以将该参数引用为 #{myDate} (唯一的区别是我在 XML 中有以下内容:

<select id="getAllbyDate" parameterType="date" resultType="com.test.myTest">

My problem is that the query with multiple parameters does not allow me to specify the parameter name in the XML file. I am able to reference by name with a single parameter.

我的问题是带有多个参数的查询不允许我在 XML 文件中指定参数名称。我可以使用单个参数按名称进行引用。

采纳答案by Larry.Z

In mapper Interface java file write the method like this

在映射器接口java文件中编写这样的方法

public List<com.test.myTest> getAllbyDate(@Param("date") Date date, @Param("myid") int myid) 

Then, modify xml file

然后,修改xml文件

<select id="getAllbyDate" parameterType="map" resultType="com.test.myTest"> 

Mybatis put all parameters with annotation @Paraminto map.

Mybatis 将所有带注解的参数@Param放入 map 中。