[英]Passing parameter to MyBatis @Select
I am writing my fist MyBatis application and I stuck around @Select. 我正在写我的拳头MyBatis应用程序,但我坚持使用@Select。 I do not know what is the problem with my @Select definition, everything seems fine but I got a
Parameter not found
exception. 我不知道我的@Select定义出了什么问题,一切似乎都很好,但是出现了“
Parameter not found
异常。
I have followed the same pattern when I pass parameters to my my @Insert statement and it works without problem. 当我将参数传递给我的@Insert语句时,我遵循了相同的模式,并且可以正常工作。
I use MyBatis 3.4.2. 我使用MyBatis 3.4.2。
This is my @Select: 这是我的@Select:
@Select("SELECT * "
+ "FROM configuration "
+ "WHERE key_name = #{key} AND "
+ "(#{userId} IS NULL AND user_id IS NULL) OR user_id = #{userId} AND "
+ "status = 1")
Configuration findByKeyAndUserId(String key, Long userId);
The exception what I got: 我得到的例外:
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: org.apache.ibatis.binding.BindingException: Parameter 'key' not found. Available parameters are [arg1, arg0, param1, param2]
### Cause: org.apache.ibatis.binding.BindingException: Parameter 'key' not found. Available parameters are [arg1, arg0, param1, param2]
When you pass a single parameter object, properties are accessed directly through getter or key set for a map. 当传递单个参数对象时,可以直接通过地图的getter或键集访问属性。 When you want to pass multiple parameters in the method, you have to name the parameters with annotation:
当您想在方法中传递多个参数时,必须使用注释命名参数:
Configuration findByKeyAndUserId(@Param("key") String key, @Param("userId") Long userId);
This annotation based syntax actually behave like a key-value map. 这种基于注释的语法实际上就像键值映射。 Keys are provided by @Param.
密钥由@Param提供。 The name you choose for parameter variables are not visible.
您为参数变量选择的名称不可见。
Please try -parameters
compile option provided since JDK 8. You can omit a @Param
annotation. 请尝试从JDK 8开始提供的
-parameters
编译选项。您可以省略@Param
批注。
See https://github.com/mybatis/mybatis-3/issues/549 参见https://github.com/mybatis/mybatis-3/issues/549
Thanks. 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.