欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mybatis报错——Mapped Statements collection already contains value for 。。。

程序员文章站 2022-03-08 15:55:41
...

Mybatis报错——Mapped Statements collection already contains value for com.mapper.EmployeeMapperPlus.getEmpByIdDe

异常如下:

org.apache.ibatis.exceptions.PersistenceException: 
### Error building SqlSession.
### The error may exist in com/mapper/EmployeeMapperPlus.xml
### The error occurred while processing mapper_resultMap[MyEmp_disc]_discriminator_case[1]
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. 
Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.mapper.EmployeeMapperPlus.getEmpByIdDe
    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
    at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
    ... 31 more

报错分析:
关键是这两句:

Error parsing Mapper XML
意思是:Mapper.xml映射文件解析错误

Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.mapper.EmployeeMapperPlus.getEmpByIdDe

意思是:映射声明集合中已经包含方法
com.mapper.EmployeeMapperPlus.getEmpByIdDe

报错原因:
Mapper映射文件中SQL标签的id属性,是与Mapper接口中的方法一一对应的。
上面错误顾名思义就是SQL标签中有 id 重复的标签。故而报出映射声明集合中已经包含某方法(Mapped Statements collection already contains value for )

如下代码,第一个标签与第三个标签id相同就会报这个异常。

<mapper namespace="com.mapper.EmployeeMapperPlus">
    <select id="getEmpByIdDe" resultMap="MySinpleEmp">
        select * from tbl_employee where id = #{id}
    </select>

    <select id="getEmpByIdStep" resultMap="MyEmpByStep">
        select * from tbl_employee where id = #{id}
    </select>
    <select id="getEmpByIdDe" resultMap="MyEmp_disc">
        select * from tbl_employee where id = #{id}
    </select>
</mapper>

错误解决:
查找根据报错提示的映射文件名和方法名,查找是否有id属性重复的SQL标签。

相关标签: mybatis 异常