spring整合mybatis报错: Mapped Statements collection already contains value for com.xxx.mapper.xxxMapper.xxx. please check…
mybatis-spring 将mybatis整合至spring时报错:java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.xxx.mapper.xxxMapper.xxxx. please check …
原因:使用的是spring映射配置,而mybatis的Mapper配置文件中出现了相同的映射结果集
Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for com.chino.mapper.UserMapper.selectUser. please check com/chino/mapper/UserMapper.xml and file [xxxxxxx\com\chino\mapper\UserMapper.xml]
解析:
简述:
原先存在着一个mybatis配置文件,而我们要将spring和mybatis进行整合
此处是在整合过程中,新建了一个将mybatis整合到spring之后的配置文件,而原来的配置文件仍旧存在,映射结果集出现冲突,从而出错;
原理分析:
文件分析
下面是测试类中的两个测试方法:
方法一是使用Mybatis,直接通过SqlSessionFactory和Mapper接口实现;
方法二是spring整合mybatis,通过SqlsessionTemplate和MapperImpl接口实现类实现;
方法一加载了mybatis-config.xml,mybatis配置文件
使用sqlSessionFactory对象来获取sqlSession,
sqlSession获取UserMapper接口 (getMapper()) 来得到UserMapper对象
方法二中加载了spring-dao.xml配置文件
通过获取Ioc容器中的userMapper的bean实例来获取方法执行对象 (而该实例其实是UserMapperImpl实现类)
userMapper bean中注入的是userMapperImpl实现类
UserMapperImpl实现类实现了UserMapper接口,注入了sqlSessionTemplate对象
所以可以看出,使用mybatis时使用的是UserMapper接口;而使用spring整合mybatis时使用的是UserMapperImpl实现类并且spring配置还会引入mybatis的配置文件;
而mybatis的配置文件中可能存在声明使用mapper接口定义的< mapper >,而这个定义会在你使用spring容器获取bean时产生冲突;
总而言之,就是两种方法实现方式冲突了
解决:将原先配置文件(mybatis-config.xml)中的映射去除即可
以上