前言:
我们在实际开发项目中,有的时候会用到mybatis的逆向工程,这个东西在某种程度的确很省事,但有时候也会因此出现bug,这篇文章讲解下如何解决题目中的bug。
正文:
一、复现问题
1.1 点击maven插件时,一不小心多点了一下,看mapper.xml时又没注意,这时候逆向工程生成xxxxMapper.xml,xxx实体类,xxxMapper时并不会重复生成xxx实体类和xxxMapper的接口,只会在xml里多生成一遍增删改查语句
1.2 会在xml里多生成一份增删改查和<resultMap id="BaseResultMap''>,如下图的第4行和第104行
1.3 这样你在启动项目时就出现下面的错误
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-04-26 20:34:27.567 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'adminLogController': Unsatisfied dependency expressed through field 'adminLogService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'AdminLogService': Unsatisfied dependency expressed through field 'adminLogDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'adminLogDao' defined in file [F:\tower\chinatower_main_service\target\classes\com\mainapp\chinatower_main_service\business\dao\AdminLogDao.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 org.springframework.core.NestedIOException: Failed to parse mapping resource: 'file [F:\tower\chinatower_main_service\target\classes\mybatis\StudentMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.mainapp.chinatower_main_service.business.dao.StudentMapper.BaseResultMap
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:374)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1395)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:592)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.facto