Spring集成Mybatis错误Result Maps collection already contains value for XXX

Spring在集成Mybatis出现如下错误:

SpringResult Maps collection already contains value for com.guowei.maven.framework.dao.UserMapper.resultUser

at org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:468)

at org.mybatis.spring.SqlSessionFactoryBean.afterPropertiesSet(SqlSessionFactoryBean.java:343)

at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutow

ireCapableBeanFactory.java:1637)

其中Spring的配置文件applicationContext.xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:c="http://www.springframework.org/schema/c"
	xmlns:cache="http://www.springframework.org/schema/cache"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:lang="http://www.springframework.org/schema/lang"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:task="http://www.springframework.org/schema/task"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd
		http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-4.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
		http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
		http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
	
   <span style="color:#ff0000;"> <!--  扫描 mappers 自动配置 -->      
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">    
    	<property name="basePackage" value="com.guowei.maven.framework.dao" />      
    </bean>  </span>
    
    <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:mysqldb.properties</value>
            </list>
        </property>
    </bean>
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreResourceNotFound" value="false" />
        <property name="properties" ref="configProperties" />
    </bean>
    
	<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
		<property name="driverClassName" value="${driver}" />
		<property name="url" value="${url}" />
		<property name="username" value="${username}" />
		<property name="password" value="${password}" />
	</bean>
	
	<span style="color:#ff0000;"><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!--dataSource属性指定要用到的连接池-->
		<property name="dataSource" ref="dataSource" />
		<!--configLocation属性指定mybatis的核心配置文件-->
		<property name="configLocation" value="mybatisconf.xml" />
		<!--mapperLocations属性指定mybatis的映射文件-->
		<property name="mapperLocations" value="classpath*:com/guowei/maven/framework/**/*.xml" />
	</bean></span>
	
</beans>

Mybatis的配置文件mybatisconf.xml内容如下:

<?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>
	<!-- 引用db.properties配置文件 -->
    <properties resource="mysqldb.properties"/>
    
	<typeAliases>
		<typeAlias alias="User" type="com.guowei.maven.framework.model.User" />
		<typeAlias alias="Order" type="com.guowei.maven.framework.model.Order" />
	</typeAliases>


  <span style="color:#ff0000;"><mappers>
    <mapper resource="com/guowei/maven/framework/mapping/UserMapper.xml"/>
  </mappers></span>

</configuration>


错误的原因如下:

因为Spring的配置文件里的MapperScannerConfigurer(或sqlSessionFactoryd的mapperLocation)和mybatis的配置文件都定义了mapper.xml,因此就会出现 java.lang.IllegalArgumentException: Result Maps collection already contains value for XXX这个错误。


解决方案:

删除mybatis配置文件里关于mapper.xml配置的<mappers></mappers>节点。


参考文献:

1、http://www.blogjava.net/crazycy/archive/2014/07/07/415523.html

2、http://www.programering.com/a/MDN0QDNwATU.html

3、http://blog.csdn.net/zht666/article/details/38706083

4、http://mybatis.github.io/spring/factorybean.html



  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值