본문 바로가기

DATABASE/Error

[Error] Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for mapper.saveData - mybatis, root-context.xml mapper선언 에러

 

DAO, DTO, VO 를 통한 Mybatis 연동 설정 중 에러 발생.

 

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'sqlSessionFactory' defined in ServletContext resource[/WEB-INF/spring/root-context.xml]: 
Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: 
Failed to parse mapping resource: 'file [C:\Users\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\sample\WEB-INF\classes\mapper\sampleMapper.xml]';
 nested exception is org.apache.ibatis.builder.BuilderException:
 Error parsing Mapper XML. The XML location is 'file
 [C:\Users\eclipse-workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\assignment00\WEB-INF\classes\mapper\sampleMapper.xml]'.
 Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for mapper.saveData

 

 

원인

  1. mapper id 가 틀린 경우
  2. Parameter와 bean의 field 명이 틀린 경우
  3. .xml 에서 정의한 namespace와 DAO SQL 문에서 선언한 namespace가 다를 경우
  4. mapper가 정의가 되어 있지 않은 경우
  5. mapper에 정의 된 같은 이름의 namespace가 여러개 일 경우

구글링 시 대부분이 5개 중 하나의 에러라고 함.

하지만 나는 전부 해당되지 않음.

좀 더 찾아보니,

root-Context.xml OR MyBatis-config.xml 에서 mapper.xml을 선언하여 중복 선언으로 판단하여 발생한 에러.

둘 중 하나에서만 mapper.xml 을 선언 해야 함.

 

	<!-- mybatis setting - sqlSessionFactory  -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
			<property name="dataSource" ref="dataSource" />
			<property name="mapperLocations" value="classpath:/mapper/*.xml" />		
			<property name="configLocation" value="classpath:/config/mybatis-config.xml" />
	</bean>

 

<mappers>
    <mapper resource="mapper/assignmentMapper.xml"/>
</mappers>

 

나는 mybatis-config.xml 에서 지워줬다.

해결