myBatis 3.2.7 如何打印 sql
该文中使用的log框架为logback
myBatis3.0.6左右的版本时
打印sql的时候只需要配置如下属性:
<logger name="java.sql.Connection" level="DEBUG" />
<logger name="java.sql.Statement" level="DEBUG" />
<logger name="java.sql.PreparedStatement" level="DEBUG" />
源码解析:
PreparedStatementLogger里面看这个log.isDebugEnabled()
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
try {
if (EXECUTE_METHODS.contains(method.getName())) {
if (log.isDebugEnabled()) {
log.debug("==> Executing: " + removeBreakingWhitespace(sql));
log.debug("==> Parameters: " + getParameterValueString());
}
clearColumnInfo();
if ("executeQuery".equals(method.getName())) {
ResultSet rs = (ResultSet) method.invoke(statement, params);
if (rs != null) {
return ResultSetLogger.newInstance(rs);
} else {
return null;
}
} else {
return method.invoke(statement, params);
}
}
这个log定义的是PreparedStatement
private static final Log log = LogFactory.getLog(PreparedStatement.class);
在myBatis3.2.7左右版本
更改了打印Sql的模式,它将sql打印细化到了每一个mapperStatement的每一个方法上。
如果你打算有一个全局配置打印所有的sql,则需要如下配置
在mybatis的configuration中增加setting配置
<settings>
<setting name="logPrefix" value="dao."/>
</settings>
然后增加配置
<logger name="dao" level="DEBUG"/>
源码解析:
ConnectionLogger
public Object invoke(Object proxy, Method method, Object[] params)
throws Throwable {
try {
if (Object.class.equals(method.getDeclaringClass())) {
return method.invoke(this, params);
}
if ("prepareStatement".equals(method.getName())) {
if (isDebugEnabled()) {
debug(" Preparing: " + removeBreakingWhitespace((String) params[0]), true);
}
PreparedStatement stmt = (PreparedStatement) method.invoke(connection, params);
stmt = PreparedStatementLogger.newInstance(stmt, statementLog, queryStack);
return stmt;
}
其中的isDebugEnabled()指的是
protected boolean isDebugEnabled() {
return statementLog.isDebugEnabled();
}
注意这里的statementLog,看SimpleExecutor的prepareStatement(handler, ms.getStatementLog());
public <E> List<E> doQuery(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, BoundSql boundSql) throws SQLException {
Statement stmt = null;
try {
Configuration configuration = ms.getConfiguration();
StatementHandler handler = configuration.newStatementHandler(wrapper, ms, parameter, rowBounds, resultHandler, boundSql);
stmt = prepareStatement(handler, ms.getStatementLog());
return handler.<E>query(stmt, resultHandler);
} finally {
closeStatement(stmt);
}
}
这个statementLog是ms.getStatementLog()而来的。而MappedStatement的StatementLog
String logId = id;
if (configuration.getLogPrefix() != null) logId = configuration.getLogPrefix() + id;
mappedStatement.statementLog = LogFactory.getLog(logId);
这里可以看到,logPrefix决定了所有log前缀,所以只需要配置logPrefix就行了
myBatis 3.2.7 如何打印 sql的更多相关文章
- mybatis和redis整合 log4j打印sql语句
首先,需要在项目中引进jedis-2.8.1.jar包,在pom.xml里加上 <dependency> <groupId>redis.clients</groupId& ...
- MyBatis配置:在控制台打印SQL语句
1.在spring-mybatis.xml中配置语句 注意:value=”classpath:mybatis-config.xml”这个文件如果之前没有,是需要新建的 2.新建mybatis-con ...
- 配置mybatis解决log4j未正常打印sql的问题
在mybatis-config.xml中增加配置: <settings> <setting name="logImpl" value="STDOUT_L ...
- mybatis 打印sql 语句
拦截器 package com.cares.asis.mybatis.interceptor; import java.text.DateFormat; import java.util.Date; ...
- mybatis 控制台打印sql
开发时调试使用 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBe ...
- mybatis 打印sql log配置
mybatis 打印sql log, 方便调试.如何配置呢? log4j.xml : <!-- 打印sql start --> <appender name="IBatis ...
- mybatis 打印 sql
该文中使用的log框架为logback myBatis3.0.6左右的版本时 打印sql的时候只需要配置如下属性: <logger name="java.sql.Connection& ...
- mybatis下使用log4j打印sql语句和执行结果
转载自:https://www.cnblogs.com/jeevan/p/3493972.html 本来以为很简单的问题, 结果自己搞了半天还是不行; 然后google, baidu, 搜出来各种方法 ...
- (后端)SpringBoot中Mybatis打印sql(转)
原文地址:https://www.cnblogs.com/expiator/p/8664977.html 如果使用的是application.properties文件,加入如下配置: logging. ...
随机推荐
- Azure 基础:自定义 Table storage 查询条件
本文是在 <Azure 基础:Table storage> 一文的基础上介绍如何自定义 Azure Table storage 的查询过滤条件.如果您还不太清楚 Azure Table s ...
- FME Cloud 账号申请流程
第一步,访问SAFE的FME Cloud注册页,官网明确表态,如果你是一个新的FME Cloud用户,你可以免费获得一个初级版.地址:https://console.fmecloud.safe.com ...
- WebService技术,服务端发布到Tomcat(使用Servlet发布),客户端使用axis2实现(二)
还是在WebService技术,服务端and客户端JDK-wsimport工具(一)的基础上实现.新建一个包:com.aixs2client.目录结构如下: 一.服务端: 1.还是使用com.webs ...
- PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历
题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子. ...
- Final发布——视频博客
1.视频链接 视频上传至优酷自频道,地址链接:http://v.youku.com/v_show/id_XMzk1OTIwNTUwMA==.html?spm=a2h0j.11185381.listit ...
- 【Alpha】第六次Scrum meeting
今日任务一览: 姓名 今日完成任务 所耗时间 刘乾 今日完成了python的一个template引擎airspeed的使用,并使用该引擎成功跑出一份latex模板替换文件. Issue链接:https ...
- 【Beta阶段】第五次Scrum Meeting!
每日任务内容: 本次会议为第五次Scrum Meeting会议~ 由于本次会议项目经理召开时间依旧较晚,在公寓7层召开,女生参与了线上会议. 队员 昨日完成任务 明日要完成任务 刘乾 #167(未完成 ...
- Linux第一章第二章学习笔记
第一章 Linux内核简介 1.1 Unix的历史 它是现存操作系统中最强大最优秀的系统. 设计简洁,在发布时提供原代码. 所有东西都被当做文件对待. Unix的内核和其他相关软件是用C语言编写而成的 ...
- LINUX内核分析第二周学习总结——操作系统是如何工作的
LINUX内核分析第二周学习总结——操作系统是如何工作的 张忻(原创作品转载请注明出处) <Linux内核分析>MOOC课程http://mooc.study.163.com/course ...
- ElasticSearch 2 (5) - Document APIs
ElasticSearch 2.1.1 (5) - Document APIs This section describes the following CRUD APIs: Single docu ...