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