package com.bos.nsc.simulator;
<?xml version="1.0" encoding="UTF-8"?>
<handlers>
<!--handlers that are added named sql-->
<!-- handler-namedsql.xml
class:the class name of the handler , mush implement com.primeton.das.sql.impl.handler.INamedSqlHandler
matchName:the id of the matched named SQL
-->
<!--
<handler id="handler2"
class="com.primeton.server.das.namedsql.handler.Handler1">
<match matchName="selectSchoolByKey"></match>
<match matchName="insertSchool"></match>
<match matchName="updateSchool"></match>
<match matchName="delete.deleteSchool"></match>
</handler>
-->
<handler id="logSqlhandler"
class="com.bos.nsc.common.NameSqlLogInterceptor">
<match matchName="com.bos.nsc.*" isRegex="true"></match>
<!-- <match matchName="insertSchool"></match>
<match matchName="updateSchool"></match>
<match matchName="delete.deleteSchool"></match> -->
</handler>
</handlers>
import java.util.List;
import com.primeton.btp.api.core.logger.ILogger;
import com.primeton.btp.api.core.logger.LoggerFactory;
import com.primeton.das.sql.impl.handler.INamedSqlHandler;
import com.primeton.das.sql.impl.ibatis.sqlmap.client.InterceptorContext;
import com.primeton.das.sql.impl.ibatis.sqlmap.client.InterceptorContext.Param;
public class NameSqlLogInterceptor implements INamedSqlHandler{
private ILogger LOGGER=LoggerFactory.getLogger(NameSqlLogInterceptor.class);
public void beforeDelete(InterceptorContext context) {
// TODO 自动生成的方法存根
logParamSql(context);
}
public void beforeInsert(InterceptorContext context) {
// TODO 自动生成的方法存根
logParamSql(context);
}
public void beforeQuery(InterceptorContext context) {
// TODO 自动生成的方法存根
logParamSql(context);
}
public void beforeUpdate(InterceptorContext context) {
// TODO 自动生成的方法存根
logParamSql(context);
}
public void logParamSql(InterceptorContext context){
try {
String sql=context.getSql();
LOGGER.info("oldsql:"+sql);
Object value=null;
List<Param> param=context.getParameters();
if(param==null){LOGGER.info("paramArray=null"); return;}
for (int i = 0; i < param.size(); i++) {
value=param.get(i)==null?null:param.get(i).getValue();
sql=sql.replaceFirst("\\?", value==null?"null":"'"+value.toString()+"'");
LOGGER.info("param["+i+"]:"+(value==null?"null":"'"+value.toString()+"'"));
}
LOGGER.info("paramedSql:"+sql);
} catch (Throwable e) {
LOGGER.error("执行sql前打印日志异常:"+e.getMessage());
}
}
}