1. 程式人生 > >Spring整合Mybatis錯誤Result Maps collection already contains val

Spring整合Mybatis錯誤Result Maps collection already contains val

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

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



           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述