MyBatis< collection>中的组合键映射 [英] Composite keys in MyBatis <collection> mappings
问题描述
我无法将组合键传递给MyBatis <collection>
元素(使用版本3.2.7). MyBatis文档指出:
I am unable to pass a composite key to a MyBatis <collection>
element (using version 3.2.7). The MyBatis documentation states:
注意:要处理复合键,您可以使用语法column ="{prop1 = col1,prop2 = col2}"指定多个列名称以传递给嵌套的select语句.这将导致针对目标嵌套选择语句的参数对象设置prop1和prop2.
Note: To deal with composite keys, you can specify multiple column names to pass to the nested select statement by using the syntax column="{prop1=col1,prop2=col2}". This will cause prop1 and prop2 to be set against the parameter object for the target nested select statement.
但是,我为实现此目的所做的所有尝试都产生了异常
However, all my attempts to implement this produce the Exception
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: Error instantiating class java.lang.Integer with invalid types () or values (). Cause: java.lang.NoSuchMethodException: java.lang.Integer.<init>()
该集合(位于另一个ResultsMap中)是:
The collection (which resides in another ResultsMap) is:
<collection property="foos" ofType="FooObject"
column="{param1=user_id,param2=foo_id}" select="getFoosByUser" >
<id property="userId" column="user_id" />
<id property="foo" column="foo_id" />
<result property="fooName" column="foo_name" />
</collection>
它应该返回一个Foo对象的ArrayList.组合键是user_id和foo_id.选择查询为:
It should return an ArrayList of Foo objects. The composite key is user_id and foo_id. The select query is:
<select id="getFoosByUser" parameterType="Integer" resultType="FooObject">
SELECT
user_id AS userId,
foo_id AS fooId,
foo_name AS fooName
FROM foo_table
WHERE user_id = #{param1}
AND foo_id = #{param2}
</select>
如果我仅使用一个参数(例如,删除了foo_id=#{param2}
,然后在集合中使用column=user_id
,但是我无法弄清楚如何正确构造两个键的column属性.有什么想法吗?
The query works correctly if I only use one parameter, e.g. removed foo_id=#{param2}
and then use column=user_id
in the collection, but I cannot work out how to structure the column attribute correctly for two keys. Any ideas?
推荐答案
当参数不止一个时,MyBatis通过使用parameterType
感到困惑.像这样修改您的查询映射:
MyBatis is confused by using parameterType
when there are more than one parameter. Modify you query mapping like this:
<select id="getFoosByUser" resultType="FooObject">
SELECT
user_id AS userId,
foo_id AS fooId,
foo_name AS fooName
FROM foo_table
WHERE user_id = #{param1}
AND foo_id = #{param2}
</select>
这篇关于MyBatis< collection>中的组合键映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!