log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory). 解决方法

最近在使用mybatis的时候一直出现如下警告:

log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).

看内容是mybatis的Logger出了点问题,原因是项目中没有log4j的配置文件,所以需要添加一个名为log4j.properties的文件到项目的src中,内容如下:

log4j.rootLogger=INFO,Console

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.Threshold=DEBUG
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%d]%l%5p:%m%n

log4j.appender.DebugFile=org.apache.log4j.RollingFileAppender
log4j.appender.DebugFile.File=../log/debugFile.log
#log4j.appender.DebugFile.File=debugFile.log
log4j.appender.DebugFile.Append=true
log4j.appender.DebugFile.Threshold=DEBUG
log4j.appender.DebugFile.layout=org.apache.log4j.PatternLayout
log4j.appender.DebugFile.layout.ConversionPattern=[%d]%l%5p:%m%n
log4j.appender.DebugFile.MaxFileSize=20MB
log4j.appender.DebugFile.MaxBackupIndex=10

log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG

log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Connection = INFO
log4j.logger.java.sql.Statement = DEBUG
log4j.logger.java.sql.PreparedStatement = DEBUG
log4j.logger.java.sql.ResultSet = DEBUG

log4j.logger.com.yuetao=DEBUG

这样之后这个警告就不会出现了。

mybatis generator自动生成代码报The error occurred while processing mapper_resultMap[BaseResultMap]错误的解决方法

今天在用mybatis自动生成代码之后遇到了一个错误:

org.apache.ibatis.exceptions.PersistenceException :
### Error building SqlSession.
### The error may exist in com/erp/webservice/mapper/UsersMapper.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.erp.webservice.inter.UsersMapper.BaseResultMap
     at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
     at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:51)
     at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:35)
     at com.erp.webservice.test.Test.<clinit>( Test.java:22)
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.erp.webservice.inter.UsersMapper.BaseResultMap
     at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:107)
     at org.apache.ibatis.builder.xml.XMLConfigBuilder.parse(XMLConfigBuilder.java:89)
     at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:49)
     ... 2 more
Caused by: java.lang.RuntimeException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException : Result Maps collection already contains value for com.erp.webservice.inter.UsersMapper.BaseResultMap
     at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:113)
     at org.apache.ibatis.builder.xml.XMLMapperBuilder.parse(XMLMapperBuilder.java:88)
     at org.apache.ibatis.builder.xml.XMLConfigBuilder.mapperElement(XMLConfigBuilder.java:325)
     at org.apache.ibatis.builder.xml.XMLConfigBuilder.parseConfiguration(XMLConfigBuilder.java:105)
     ... 4 more
Caused by: java.lang.IllegalArgumentException: Result Maps collection already contains value for com.erp.webservice.inter.UsersMapper.BaseResultMap
     at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:710)
     at org.apache.ibatis.session.Configuration$StrictMap.put(Configuration.java:682)
     at org.apache.ibatis.session.Configuration.addResultMap(Configuration.java:473)
     at org.apache.ibatis.builder.MapperBuilderAssistant.addResultMap(MapperBuilderAssistant.java:204)
     at org.apache.ibatis.builder.ResultMapResolver.resolve(ResultMapResolver.java:44)
     at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:277)
     at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElement(XMLMapperBuilder.java:244)
     at org.apache.ibatis.builder.xml.XMLMapperBuilder.resultMapElements(XMLMapperBuilder.java:236)
     at org.apache.ibatis.builder.xml.XMLMapperBuilder.configurationElement(XMLMapperBuilder.java:109)
     ... 7 more
Exception in thread "main" java.lang.NullPointerException
     at com.erp.webservice.test.Test.main( Test.java:29)

后来发现是我运行了多次自动生成代码的程序,导致了com/erp/webservice/mapper/UsersMapper.xml的内容多出来很多,原来自动生成代码的时候生成的XML代码不是覆盖原来的,而是附加的XML的尾部的。

第一次运行自动生成代码程序:

mapper1

第二次运行自动生成代码程序:

mapper2

看看,XML文件明显大了很多,只要删掉原来的XML然后重新运行自动生成代码的程序即可。