Since MyBatis is easy to learn, it is easier to get started than Hibername, and the code can be generated automatically. These days the following code is automatically generated, for reference:
http://0609xiaohua.iteye.com/blog/1453570
But copy the code in, run the next, but can't run up, reported the following error:
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
Later, I downloaded an Eclipse project that could run directly on the CSDN, and compared it to the previous project, as if it were no different. Finally make a trick: code comparison tool, found that the generated petmapper.xml more than hundreds of lines!!!
It turns out that I did not delete the previously generated petmapper.xml when I was running, and then added it again when I generated the code again! So before you run code generation, you need to clear out the XML files that were previously generated to make a mistake.
Mybatisgenerator code automatically generates an error result Maps collection already contains value for Baseresultmap