biip的拦截器打印sql

9 篇文章 0 订阅

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());
        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值