Mybatis逆向工程后报错IllegalArgumentException: Result Maps collection already contains value for xxx

问题:
通过配置mybatis逆向工程的配置文件generatorConfig.xml自动生成实体类,接口和xml文件十分方便,但是在启动springboot时,报错
IllegalArgumentException: Result Maps collection already contains value for xxx
原因其实是,在生成的xxxMapper.xml文件中的resultMap重复了
可能存在的情况有两种:

1.再次使用逆向工程生成接口和xml文件时,没有删除原来的xml文件,导致resultMap重复

下面为复制了控制台报错的开头一部分

Error creating bean with name 'itemKillMapper' defined in file 
[D:\apache-tomcat-8.5.42\apache-tomcat-8.5.42\webapps\server\WEB-INF\classes\com\pitt\kill\model\mapper\ItemKillMapper.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 [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: 
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: 
Factory method 'sqlSessionFactory' threw exception; 
nested exception is java.lang.NoSuchMethodError: org.apache.ibatis.session.Configuration.setVfsImpl(Ljava/lang/Class;)V

删除原来的xml文件重新生成即可解决

2.使用了version 8.x 的 Connector/J

<dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <version>8.0.11</version>
</dependency>

由于我使用了8.0.11版本的mysql-connector-java,逆向工程生成实体类时会自动生成UserWithBLOBs.java 和 UserKey.java文件,而这两个文件会导致UserMapper.xml中的resultMap重复。 从而产生报错。

MyBatis Generator 的官方文档中对这个问题进行了描述

If you are using version 8.x of Connector/J you may notice that the generator attempts to generate code for tables in the MySql information schemas (sys, information_schema, performance_schema, etc.) This is probably not what you want! To disable this behavior, add the property “nullCatalogMeansCurrent=true” to your JDBC URL.

翻译为:

如果您使用的是Connector / J的8.x版,您可能会注意到生成器尝试为MySql信息模式(sys,information_schema,performance_schema等)中的表生成代码。这可能不是您想要的! 要禁用此行为,请将属性“nullCatalogMeansCurrent = true”添加到JDBC URL。

解决:
1.更换低版本Connector
2.将属性“nullCatalogMeansCurrent = true”添加到JDBC URL(推荐)

 <jdbcConnection 
     driverClass="com.mysql.cj.jdbc.Driver"
     connectionURL="jdbc:mysql://localhost:3306/db_second_kill?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=false&amp;nullCatalogMeansCurrent = true" 
     userId="root"
     password="123456">
 </jdbcConnection>

此外,注意!
如果在properties配置文件中配置了mybatis.mapper-locations,则在mybatis的xml配置文件中不用再写< mapper resource >配置

mybatis.config-location=classpath:mybatis-config.xml
mybatis.checkConfigLocation = true
mybatis.mapper-locations=classpath:mappers/*.xml
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值