mybatis generator 修改xml文件後啟動Tomcat伺服器時出現的Result Maps collection already contains value for BaseResultMap異常, ...
由於mybatis簡單易學,比起Hibername來,更容易上手,代碼也能自動生成。這幾天研究了下代碼自動生成的,參考:
http://0609xiaohua.iteye.com/blog/1453570
但是把代碼複製進來,運行了下,卻跑不起來,報以下錯誤:
Exception in thread "main" Java.lang.ExceptionInInitializerError
at com.test.Test.main(Test.java:12)
Caused by: org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### The error may exist in com/mapper/PetMapper.xml
### The error occurred while processing mapper_resultMap[BaseResultMap]
### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.RuntimeException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.mapper.PetMapper.BaseResultMap
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:8)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:32)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:16)
at com.util.SessionFactory.<clinit>(SessionFactory.java:21)
... 1 more
Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.RuntimeException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.mapper.PetMapper.BaseResultMap
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:85)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:69)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:30)
... 3 more
Caused by: java.lang.RuntimeException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.mapper.PetMapper.BaseResultMap
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:97)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:73)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:255)
at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:83)
... 5 more
Caused by: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.mapper.PetMapper.BaseResultMap
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:578)
at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:551)
at org.apache.ibatis.session.Configuration.addResultMap(Configuration.java:356)
at org.apache.ibatis.builder.MapperBuilderAssistant.addResultMap(MapperBuilderAssistant.java:138)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:231)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:201)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:196)
at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:93)
... 8 more
後來在CSDN上下載了個可以直接運行的eclipse 項目,再與之前的項目對比,好像也沒什麼不同。最後使絕招:代碼對比工具,發現生成的PetMapper.xml多了幾百行!!!
因為我在使用generator自動生成工具之後,又修改了generator,本以為修改命令裡面的-overwrite參數能自動覆蓋,但是事實上不可以,所以之前的和現在的兩個混合在一起導致了錯誤。解決辦法就是刪掉之前生成的mapper和pojo包,重新生成,然後複製到工程下麵。問題就解決了!!
原來是我在運行時沒有把之前已經生成的PetMapper.xml刪除掉,再次生成代碼時,又附加了上去!所以在運行代碼生成之前,要把以前已經生成的xml文件清掉,以妨出錯。