问题:
通过配置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&useUnicode=true&characterEncoding=utf-8&useSSL=false&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