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

Rus*_*ore 3 java xml mysql spring mybatis

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

并收到此错误:

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
Run Code Online (Sandbox Code Playgroud)

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

public class Question {


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

    public Question(){
    }   
}
Run Code Online (Sandbox Code Playgroud)

这是我的 Mapper.Java 类

@MapperScan
public interface Mapper {

public List<Question> getQuestions(@Param("shifts") List<Integer> shifts, @Param("job_id") Integer job_id);
}
Run Code Online (Sandbox Code Playgroud)

最后这里是 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>
Run Code Online (Sandbox Code Playgroud)

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

小智 7

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

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
Run Code Online (Sandbox Code Playgroud)

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


Rus*_*ore 2

是的,我的 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>
Run Code Online (Sandbox Code Playgroud)