ssm整合遇到的坑:mybatis generator 生成代码重复问题

最近在跟着B站尚硅谷学习ssm代码整合案例(av35988777), 遇到了一个问题(同时也是很多人遇到的), 仅此记录.

关键词:ssm generator  逆向工程 自动生成 代码重复

报错提示:

警告: Exception encountered during context initialization - cancelling refresh attempt: 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 [C:\Users\47444\Desktop\workspace\springboot\ssm-crud\target\classes\mapper\DepartmentMapper.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. The XML location is 'file [C:\Users\47444\Desktop\workspace\springboot\ssm-crud\target\classes\mapper\DepartmentMapper.xml]'. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.atguigu.crud.dao.DepartmentMapper.BaseResultMap
五月 09, 2019 8:51:28 上午 org.springframework.test.context.TestContextManager prepareTestInstance
严重: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@573f2bb1] to prepare test instance [com.atguigu.crud.test.MapperTest@5038d0b5]
java.lang.IllegalStateException: Failed to load ApplicationContext

一般报错都是在刚开始的一个错误导致后面一连串雪崩式的报错, 所以找错误一般是从前面找起. 

出错原因:

从上面提取出出错的原因DepartmentMapper.xml]. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.atguigu.crud.dao.DepartmentMapper.BaseResultMap

字面意思就是,在逆向工程生成的DepartmentMapper.xml文件导致了一个错误:结果集已存在导致结果集重复. 

我们找到这个xml看一下

可以明显的看出,这里自动生成的代码重复了.这就是导致上述错误的元凶.(其實是我自己運行了兩次逆向工程測試類, 而代碼默認重复自動生成時是追加而不是覆蓋,手賤/捂脸)

解决方法:

1. 若该工程表不多, 可以手动删除重复代码, 这是最快也是最直观的方法.

2. 将逆向工程自动生成的代码删掉,其中包括

com.atguigu.crud.bean
com.atguigu.crud.dao
..\resources\mapper\xxxMapper.xml

之后重新运行一次逆向工程测试类:com.atguigu.crud.test.MBGTest

3. 在网上查出,可以在mybatis配置生成时候<table>便签头部可以添加一个schema属性,用来标识。(尚未测试)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值