[英]MyBatis log4j configuration to log only SQL names from mapper XMLs and not SQLs
當我在log4j.properties
設置以下屬性時,
log4j.logger.package.mapper=DEBUG
MyBatis 會將作業委托給log4j
, log4j
將打印日志中的 WHOLE SQL
語句。 有沒有辦法只記錄SQL
語句的名稱(XML 中的id
與JAVA
中的映射器函數名稱匹配,如果使用注釋完成)?
前任。 我在映射器中的選擇查詢是這樣的,如果通過XML
完成:
<select id="selectPerson" parameterType="int" ....
然后我只想記錄selectPerson
。 目前它正在被記錄,但它還包括帶有所有參數值的SQL
語句。 我只想從日志中隱藏實際的SQL
查詢。
MyBatis 的日志實現沒有隱藏 SQL 的選項,但是,可以使用自定義攔截器來實現它。
假設您使用的是最新版本的 MyBatis,這里是一個示例實現。
package pkg;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;
@Intercepts({
@Signature(
type = Executor.class,
method = "update",
args = { MappedStatement.class, Object.class }),
@Signature(
type = Executor.class,
method = "query",
args = { MappedStatement.class, Object.class,
RowBounds.class, ResultHandler.class })
})
public class LogStatementIdInterceptor implements Interceptor {
private static final Logger logger = Logger.getLogger(LogStatementInterceptor.class);
@Override
public Object intercept(Invocation invocation) throws Throwable {
MappedStatement ms = (MappedStatement) invocation.getArgs()[0];
logger.debug(ms.getId());
return invocation.proceed();
}
}
要注冊攔截器,請將以下條目添加到 XML 配置文件中。
<plugins>
<plugin interceptor="pkg.LogStatementIdInterceptor" />
</plugins>
使用 Java 配置,將攔截器的實例傳遞給org.apache.ibatis.session.Configuration#addInterceptor()
。
在log4j.properties
......
log4j.logger.pkg.LogStatementIdInterceptor=DEBUG
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.