将参数传递给MyBatis @Select [英] Passing parameter to MyBatis @Select
问题描述
我正在写我的拳头MyBatis应用程序,但我坚持使用@Select.我不知道我的@Select定义出了什么问题,一切似乎都很好,但是出现了Parameter not found
异常.
I am writing my fist MyBatis application and I stuck around @Select. I do not know what is the problem with my @Select definition, everything seems fine but I got a Parameter not found
exception.
当我将参数传递给我的@Insert语句时,我遵循了相同的模式,并且可以正常工作.
I have followed the same pattern when I pass parameters to my my @Insert statement and it works without problem.
我使用MyBatis 3.4.2.
I use MyBatis 3.4.2.
这是我的@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);
我得到的例外情况:
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]
推荐答案
当您传递单个参数对象时,可以直接通过地图的getter或键集访问属性.当您想在方法中传递多个参数时,必须使用注释命名参数:
When you pass a single parameter object, properties are accessed directly through getter or key set for a map. 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);
这种基于注释的语法实际上就像键值映射.密钥由@Param提供.您为参数变量选择的名称不可见.
This annotation based syntax actually behave like a key-value map. Keys are provided by @Param. The name you choose for parameter variables are not visible.
这篇关于将参数传递给MyBatis @Select的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!