本文介绍了MyBatis< collection>中的组合键映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将组合键传递给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:

但是,我为实现此目的所做的所有尝试都产生了异常

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&lt; collection&gt;中的组合键映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:26