Spring5整合Mybatis出现 Mapped Statements collection already contains value for xxxx

出现错误提示映射文件已经存在value for xxx

 本人在使用Spring5集成Mybatis使用的时候,配置Mybatis以及spring5之后,运行spring测试类方法,程序报错:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:

Error creating bean with name ‘userMapper’ defined in file
[E:\CodePrograms\IdeaProject\SSMConstruction\target\classes\com\ssmconstruction\repository\UserMapper.class]:
Unsatisfied dependency expressed through bean property
‘sqlSessionFactory’;
nested exception is org.springframework.beans.factory.BeanCreationException: Error
creating bean with name ‘sqlSessionFactory’ defined in class path
resource [Spring-Config.xml]: Invocation of init method failed;
nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: ‘file
[E:\CodePrograms\IdeaProject\SSMConstruction\target\classes\com\ssmconstruction\repository\BaseMapper.xml]’;
nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is ‘file
[E:\CodePrograms\IdeaProject\SSMConstruction\target\classes\com\ssmconstruction\repository\BaseMapper.xml]’.

  Cause: java.lang.IllegalArgumentException: 
  Mapped Statements collection already contains value for com.ssmconstruction.repository.BaseMapper.getTableColNameByDbNameAndTableName.

  please check com/ssmconstruction/repository/BaseMapper.xml and file

[E:\CodePrograms\IdeaProject\SSMConstruction\target\classes\com\ssmconstruction\repository\BaseMapper.xml]

 最终提示无法加载ApplicationContext对象,在查阅百度发现解决方案主要集中以下四种情况:

1、Mapper文件中存在重复的ID

解决方法:
检查mapper文件中是否存在相同的id名称

2、mapper文件中的参数类型以及返回值类型错误

解决方法:
检查属性参数以及返回值类型的时候,分为两种情况,一种是基本数据类型,Mabatis官方已经使用别名进行了处理,因此使用简写(非全路径,类名小写可以被识别)但是自定义数据类型没有在Mabatis配置文件中配置别名的时候使用了非全路径进行表示
以下参考Mabatis官方配置别名:


别名	映射的类型
_byte	byte
_long	long
_short	short
_int	int
_integer	int
_double	double
_float	float
_boolean	boolean
string	String
byte	Byte
long	Long
short	Short
int	Integer
integer	Integer
double	Double
float	Float
boolean	Boolean
date	Date
decimal	BigDecimal
bigdecimal	BigDecimal
object	Object
map	Map
hashmap	HashMap
list	List
arraylist	ArrayList
collection	Collection
iterator	Iterator

配置自定义别名扫描(参考官方文档):

类型别名(typeAliases)
类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写。例如:

<typeAliases>
  <typeAlias alias="Author" type="domain.blog.Author"/>
  <typeAlias alias="Blog" type="domain.blog.Blog"/>
  <typeAlias alias="Comment" type="domain.blog.Comment"/>
  <typeAlias alias="Post" type="domain.blog.Post"/>
  <typeAlias alias="Section" type="domain.blog.Section"/>
  <typeAlias alias="Tag" type="domain.blog.Tag"/>
</typeAliases>
当这样配置时,Blog 可以用在任何使用 domain.blog.Blog 的地方。

也可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean,比如:

<typeAliases>
  <package name="domain.blog"/>
</typeAliases>
每一个在包 domain.blog 中的 Java Bean,在没有注解的情况下,会使用 Bean 的首字母小写的非限定类名来作为它的别名。 比如 domain.blog.Author 的别名为 author;若有注解,则别名为其注解值。见下面的例子:

@Alias("author")
public class Author {
    ...
}

3、在sqlSessionFactory中配置了多个xml地址

解决方法:
而不是使用通配符号进行处理(classpath:com/ssmconstruction/repository/*.xml

   <!--整合MyBatis-->
    <!--注册SqlSessionFactoryBean初始化MyBatis-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:MyBatis.xml"/>
        <property name="mapperLocations" value="classpath:com/ssmconstruction/repository/*.xml"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>

4、在SqlSessionFactory中配置了mapperlocation而且也在mybatis配置类中配置了<mappers>

本人出错地方 Mark一下

如下情况:

Mybatis配置
<!--    开启mapper扫描(采用包扫描)-->
    <mappers>
        <package name="com.ssmconstruction.repository"/>
    </mappers>
Spring ApplicationContext配置
   <!--整合MyBatis-->
    <!--注册SqlSessionFactoryBean初始化MyBatis-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="configLocation" value="classpath:MyBatis.xml"/>
        <property name="mapperLocations" value="classpath:com/ssmconstruction/repository/*.xml"/>
        <property name="dataSource" ref="dataSource"/>
    </bean>

这样配置导致的重复扫描Mapper,参考文档 mapperLocation属性
得知加载mapper的xml映射文件 ,配置该属性后全局配置文件可以不需要再配置,因此全部配置时会出现重复扫扫描<mappers>

解决方法:
使用其中一种扫描方式即可!

 本人也是正在学习,如果以上有什么错误的地方,麻烦各位纠正,当让如果能帮到大家的话,也是很有幸了,谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不会拯救世界的SuperMan

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值