How to solve Mybatis java.lang.IllegalArgumentException: Result Maps collection already contains value for X

  • 2020-05-26 08:32:01
  • OfStack

I need to integrate the framework spring, struts2 and mybatis3 for the project these two days. However, this error always occurs during the startup, which has puzzled me for a long time. The answers I found on the Internet are not what I wanted.

user - mapper. xml is as follows:


<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  <mapper namespace="com.bmdc.dj.user.dao.UserDao">
    <resultMap type="user" id="userResult">
     <id property="user_id" column="USER_ID"/>
      <result property="login_name" column="LOGIN_NAME"/>
      <result property="real_name" column="REAL_NAME"/>
      <result property="password" column="PASSWORD"/>
   </resultMap>
   <insert id="add" parameterType="user">
     insert into users (user_id, login_name, real_name, password)
     values(#{user_id}, #{login_name}, #{real_name}, #{password})
   </insert>
 </mapper>

Where namespace is the interface path.

Mybatis. xml as follows:


<?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
 <configuration> 
   <!--  The alias  -->
   <typeAliases>
     <typeAlias type="com.bmdc.dj.user.domain.User" alias="user"/>
   </typeAliases>
    <mappers>  
     <mapper resource="com/bmdc/dj/user/dao/user-mapper.xml" />  
   </mappers>
 </configuration>

The other configuration files are correct, so I won't write them. This will cause the error Java.lang.IllegalArgumentException: Result Maps collection already contains value for XXX.

The solution: delete Mybatis.xml < mappers > Tag all content. Because if the user-mapper.xml interface is in the same path as namespace, there is no need to reconfigure it in mybaits.xml.

The revised Mybatis.xml is as follows:


 <?xml version="1.0" encoding="UTF-8" ?>
 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
 <configuration> 
   <!--  The alias  -->
   <typeAliases>
     <typeAlias type="com.bmdc.dj.user.domain.User" alias="user"/>
   </typeAliases>
 </configuration>

Related articles: