Mapped Statements collection already contains value for com.xxx.mapper.xxxMapper.xxx. please check..

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)中的映射去除即可

在这里插入图片描述

以上

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值