Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 56; columnNumber: 17

在mybatis中使用log4j遇到的问题,先解决这个异常问题,顺便再给log4j做一个简单的总结:

1.问题

Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
### Error building SqlSession.
### Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 56; columnNumber: 17; 元素类型为 "configuration" 的内容必须匹配 "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,reflectorFactory?,plugins?,environments?,databaseIdProvider?,mappers?)"。
at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:64)

2.解决问题

在mybatis-config.xml配置文件中将这段代码

<settings>
<!-- 开启log4j来记录日志 -->
<setting name="logImpl" value="log4j"/>
</settings>

放在

<properties resource="properties/db.properties"></properties>

后面

其实控制台已经打印出原因了,但是没有看懂起初

mybatis-config.xml配置文件配置时,要注意节点顺序

看下面的报错提示信息,在写配置是要严格按照下面的节点顺序来:

元素类型为 "configuration" 的内容必须匹配 "(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,objectWrapperFactory?,plugins?,environments?

,databaseIdProvider?,mappers?)"。

 是不是感觉很无语。。。

sout("------------------------分割线---------------------------------------------")

下面来做下log4j的简单总结

log4j在mybatis中使用的简单配置

第一步(在项目中导入log4j的包)

第二步(在src下创建log4j.properties)要求文件名必须是log4j.properties也必须在src下,假如不在更根目录下,需要在web,xml里面配置

 

##rootCategory:表示日志信息输出级别和日志信息输出位置    log4j常用的输出级别分别是
##fatal(致命错误)>error(错误)>warn(警告)>info(普通信息)>debug(调试信息)
##我们设置的是ERROR,那么小于该级别的是不会输出的

log4j.rootCategory= debug, stdout , R

 ##表示对特定的类设置输出信息级别,如果没有写接口那么可以log4j.logger.命名空间=级别,

##当然也可对特定的方法和包设置输出级别
 ##特定的包:log4j.logger.包名=级别
## 特定的方法: log4j.logger.包名.方法名=级别
log4j.logger.mapper=DEBUG
##控制台打印设置
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=[QC] %p [%t] %C.%M(%L) | %m%n
##文件存储设置
log4j.appender.R=org.apache.log4j.DailyRollingFileAppender

##指定文件输出的位置
log4j.appender.R.File=e:\log4j\qc.log
log4j.appender.R.layout=org.apache.log4j.PatternLayout

%p:代表日志界别

[%t]:代表执行的方法

%C :则表示输出的是      包名+类名
%d:输出的是                   时间(可以设置输出样式%d(yyyy-MM-dd HH:mm:ss))
%L:表示输出的                行号
%m:表示想要输出:        想要输出的信息(一般是异常信息)
%n:表示的是                    换行

log4j.appender.R.layout.ConversionPattern=%d-[TS] %p %t %c - %m%n

第三步(mabatis-config.xml文件中配置如下信息)

<settings>
<!-- 开启log4j来记录日志 -->
<setting name="logImpl" value="log4j"/>
</settings>

第四步:运行一个方法查看效果

a.查看控制台:

[QC] DEBUG [main] org.apache.ibatis.logging.LogFactory.setImplementation(135) | Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
[QC] DEBUG [main] org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(316) | PooledDataSource forcefully closed/removed all connections.
[QC] DEBUG [main] org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(316) | PooledDataSource forcefully closed/removed all connections.
[QC] DEBUG [main] org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(316) | PooledDataSource forcefully closed/removed all connections.
[QC] DEBUG [main] org.apache.ibatis.datasource.pooled.PooledDataSource.forceCloseAll(316) | PooledDataSource forcefully closed/removed all connections.
[QC] DEBUG [main] org.apache.ibatis.transaction.jdbc.JdbcTransaction.openConnection(136) | Opening JDBC Connection
[QC] DEBUG [main] org.apache.ibatis.datasource.pooled.PooledDataSource.popConnection(387) | Created connection 1766724936.
[QC] DEBUG [main] org.apache.ibatis.transaction.jdbc.JdbcTransaction.setDesiredAutoCommit(100) | Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@694e1548]
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(145) | ==> Preparing: select * from t_user where userid=?
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(145) | ==> Parameters: 1(Integer)
[QC] DEBUG [main] org.apache.ibatis.logging.jdbc.BaseJdbcLogger.debug(145) | <== Total: 1
lisi
[QC] DEBUG [main] org.apache.ibatis.transaction.jdbc.JdbcTransaction.resetAutoCommit(122) | Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@694e1548]
[QC] DEBUG [main] org.apache.ibatis.transaction.jdbc.JdbcTransaction.close(90) | Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@694e1548]
[QC] DEBUG [main] org.apache.ibatis.datasource.pooled.PooledDataSource.pushConnection(344) | Returned connection 1766724936 to pool.

b.查看磁盘文件

2021-01-10 20:27:50,554-[TS] DEBUG main org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
2021-01-10 20:27:50,758-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2021-01-10 20:27:50,867-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2021-01-10 20:27:50,867-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2021-01-10 20:27:50,867-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2021-01-10 20:27:51,304-[TS] DEBUG main org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
2021-01-10 20:28:02,594-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 1766724936.
2021-01-10 20:28:02,594-[TS] DEBUG main org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@694e1548]
2021-01-10 20:28:02,594-[TS] DEBUG main mapper.UserMapper.findUserById - ==> Preparing: select * from t_user where userid=?
2021-01-10 20:28:02,861-[TS] DEBUG main mapper.UserMapper.findUserById - ==> Parameters: 1(Integer)
2021-01-10 20:28:03,141-[TS] DEBUG main mapper.UserMapper.findUserById - <== Total: 1
2021-01-10 20:28:03,141-[TS] DEBUG main org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@694e1548]
2021-01-10 20:28:03,141-[TS] DEBUG main org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@694e1548]
2021-01-10 20:28:03,141-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 1766724936 to pool.
2021-01-10 21:04:14,438-[TS] DEBUG main org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
2021-01-10 21:04:14,781-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2021-01-10 21:04:14,875-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2021-01-10 21:04:14,875-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2021-01-10 21:04:14,885-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2021-01-10 21:04:15,282-[TS] DEBUG main org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
2021-01-10 21:04:16,031-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 1766724936.
2021-01-10 21:04:16,031-[TS] DEBUG main org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@694e1548]
2021-01-10 21:04:16,041-[TS] DEBUG main mapper.UserMapper.findUserById - ==> Preparing: select * from t_user where userid=?
2021-01-10 21:04:16,249-[TS] DEBUG main mapper.UserMapper.findUserById - ==> Parameters: 1(Integer)
2021-01-10 21:04:16,362-[TS] DEBUG main mapper.UserMapper.findUserById - <== Total: 1
2021-01-10 21:04:16,372-[TS] DEBUG main org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@694e1548]
2021-01-10 21:04:16,372-[TS] DEBUG main org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@694e1548]
2021-01-10 21:04:16,372-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 1766724936 to pool.
2021-01-10 21:14:28,673-[TS] DEBUG main org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
2021-01-10 21:14:28,703-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2021-01-10 21:14:28,713-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2021-01-10 21:14:28,713-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2021-01-10 21:14:28,713-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
2021-01-10 21:14:28,868-[TS] DEBUG main org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
2021-01-10 21:14:29,175-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 1766724936.
2021-01-10 21:14:29,176-[TS] DEBUG main org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.T4CConnection@694e1548]
2021-01-10 21:14:29,180-[TS] DEBUG main mapper.UserMapper.findUserById - ==> Preparing: select * from t_user where userid=?
2021-01-10 21:14:29,329-[TS] DEBUG main mapper.UserMapper.findUserById - ==> Parameters: 2(Integer)
2021-01-10 21:14:29,429-[TS] DEBUG main mapper.UserMapper.findUserById - <== Total: 1
2021-01-10 21:14:29,429-[TS] DEBUG main org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [oracle.jdbc.driver.T4CConnection@694e1548]
2021-01-10 21:14:29,429-[TS] DEBUG main org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [oracle.jdbc.driver.T4CConnection@694e1548]
2021-01-10 21:14:29,429-[TS] DEBUG main org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 1766724936 to pool.

原创不易,转载须注明出处。

如果这篇博文对你有帮助,请你动动你发财的小手支持一下,谢谢

原文地址:https://www.cnblogs.com/flyfishing1991/p/14259616.html