java.lang.IllegalArgumentException:结果映射集合已经包含值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37257251/
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-11-03 02:21:26  来源:igfitidea点击:

java.lang.IllegalArgumentException: Result Maps collection already contains value for

javamysqlxmlspringmybatis

提问by Russell Fillmore

Hey I'm using Mybatis with Spring Annotations.

嘿,我正在使用带有 Spring 注释的 Mybatis。

and getting this error:

并收到此错误:

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.mypackage.mappers.QuestionsMapper.Question

here is the domain class (sans getters and setters):

这是域类(没有 getter 和 setter):

public class Question {


    String optionsAsString;
    String typeAsString;
    Integer fieldId;
    String title;
    String description;

    public Question(){
    }   
}

here is my Mapper.Java class

这是我的 Mapper.Java 类

@MapperScan
public interface Mapper {

public List<Question> getQuestions(@Param("shifts") List<Integer> shifts, @Param("job_id") Integer job_id);
}

lastly here is the Mapper.xml

最后这里是 Mapper.xml

<mapper namespace="com.mypackage.mappers.Mapper">
<resultMap type="com.mypackage.domain.Question" id="Question">
    <id column="field_id" property="fieldId" />
    <result column="data_type" property="typeAsString" />
    <result column="title" property="title" />
    <result column="description" property="description" />
    <result column="options" property="optionsAsString" />
</resultMap>

<select id="com.mypackage.mappers.Mapper.getQuestions" resultMap="Question" timeout="10">
    SELECT
    f.field_id,
    f.data_type,
    f.title,
    f.options,
    f.description 
    FROM
        (SELECT DISTINCT q.*
        FROM
            question_services qs INNER JOIN
            questions q 
            ON qs.field_id=q.field_id AND q.job_id = qs.job_id INNER JOIN
            services s
            ON qs.service_id = s.service_id and qs.job_id = s.job_id
            WHERE s.job_id = #{job_id} AND s.service_id in
            <foreach item="shift" collection="shifts" open="(" separator="," close=")">
                #{shift}
            </foreach>
        ) f
</select>

I'm inclined to believe there is something wrong with the xml select statement. Probably with how I am using foreach. I have another mapper using a similar format it just does not use for each and it is not having any problems.

我倾向于相信 xml select 语句有问题。可能与我使用 foreach 的方式有关。我有另一个使用类似格式的映射器,它只是不用于每个映射器,并且没有任何问题。

采纳答案by Russell Fillmore

Yep It looks like there was an error somewhere in my select statement. I ended up just rewriting it a different way.

是的,我的 select 语句中的某处似乎有错误。我最终只是以不同的方式重写它。

<select id="getQuestions" resultMap="Question">
    SELECT
        q.field_id,
        q.data_type,
        q.title,
        q.description,
        q.options
    FROM
        questions q
    WHERE
        job_id = #{job_id}
    AND
        field_id
    IN
        (SELECT 
            fs.field_id
        FROM
            question_services qs
        INNER JOIN 
            services s 
        ON
            qs.service_id = s.service_id 
        AND
            qs.job_id = s.job_id
        WHERE
            s.job_id=#{job_id}
        AND
            s.service_id
        IN
        <foreach item="item" index="index" collection="shifts" open="(" separator="," close=")">
            #{item}
        </foreach>
        );
</select>

回答by Manu Bhat

Adding this answer since in my case the problem was different but the exception was same . The exception said ,

添加此答案是因为在我的情况下问题不同但异常相同。例外说,

Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.mypackage.model.mymapper.BaseResultMap

This can also happen if you have the same mapper xml present in multiple locations and that duplicate xml gets picked up and parsed. Its good to check that even if by mistake you have taken a backup of the mapper xmls and that lies in the location or sub folders which the mybatis configurer scans for.

如果在多个位置存在相同的映射器 xml 并且重复的 xml 被拾取和解析,也会发生这种情况。即使错误地备份了映射器 xmls 并且位于 mybatis 配置器扫描的位置或子文件夹中,也可以检查一下。