Spring配置文件
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean"
p:configLocation="classpath:com/tradinglink/conf/mybatis-config.xml"
p:mapperLocations="classpath:org/tradinglink/dao/*.xml">
<property name="dataSource" ref="directDataSource"/>
</bean>
<!-- integration spring and mybatis, scan all dao -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
p:basePackage="org.tradinglink.dao"
p:sqlSessionFactoryBeanName="sqlSessionFactory"/>
mybatis-config.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 >
<!--
This file is generated by MyBatis Generator.
This file is the shell of a Mapper Config file - in many cases you will need to add
to this file before it is usable by MyBatis.
This file was generated on Mon Jul 07 09:52:49 SGT 2014.
-->
<mappers >
<mapper resource="org/tradinglink/dao/mapperxml/YYUserGroupMapper.xml" />
<mapper resource="org/tradinglink/dao/mapperxml/YYUserMapper.xml" />
<mapper resource="org/tradinglink/dao/mapperxml/YYUserLineMapper.xml" />
<mapper resource="org/tradinglink/dao/mapperxml/YYUserLinkGroupMapper.xml" />
<mapper resource="org/tradinglink/dao/mapperxml/YYUserLinkLineMapper.xml" />
</mappers>
</configuration>
分析与解决
因为Spring的MapperScannerConfigurer和mybatis-config.xml都定义了定义了mapperxml,因此就会出现 java.lang.IllegalArgumentException: Result Maps collection already contains value for XXX这个错误。
解决办法是:删除Mybatis.xml中的<mappers>标签所有内容。因为如果user-mapper.xml与namespace的接口在同一路径下,就不需要在mybaits.xml中再进行配置了。