【SSM开发】MyBatis Generator踩过的坑
一个在使用MyBatis Generator工具的时候,花了一段时间才解决的一个坑。
0x01 问题出现
在前几次整合SSM框架的时候,一直都是带着尝试的心态去学习的,因为原本比较少用偏向于原生SQL语句的Mybatis,一直都是用的Hibernate。
尝试整合Mybatis的时候,需要用到大量的原生SQL语句,偶然发现了mybatis generator这个工具。
最后整合阶段的时候报了错误,一直以为是代码的问题,最后发现是自动生成的*Mapper.xml文件的问题。
0x02 排错过程
因为我在生成的时候又更新了一个字段,所以我就就又重新用生成功能生成了一遍,后来就一直报错了。
报错如下:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘articleApiService’: Unsatisfied dependency expressed through field ‘articleMapper’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘articleMapper’ defined in file [D:\j2ee.metadata.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\cms\WEB-INF\classes\com\colodoo\cms\dao\ArticleMapper.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 [applicationContext.xml]: Invocation of init method failed; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: ‘file [D:\j2ee.metadata.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\cms\WEB-INF\classes\mapper\MenuMapper.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.colodoo.cms.dao.MenuMapper.BaseResultMap
然后去查看Mapper文件的BaseResultMap,下意识的看了下字段的名称有没有错误,都没有。
但是如果你一直拖到底下的BaseResultMap你会发现,他和前面的BaseResultMap直接重复了,之后我发现不仅这个BaseResultMap重复了,他直接在原本的文件上再添加到下面。
0x03 解决问题
在生成Mapper.xml文件的时候,如果本来已经存在,那么请先删除后再生成,.java文件无所谓,因为*.Mapper.xml是增量更新的。可能会冲突!