如何拦截ibatis中所有的执行sql,并记录进数据库

现在需要将所有ibatis的执行sql进行拦截并记录进数据库,但是没有找到ibatis的相关拦截器(只找到了mybatis的拦截器),请问应该如何获取到所有的执行sql并进行记录。

阅读 4.5k
3 个回答

通过spring aop去拦截SqlMapClientTemplate下的方法,即可进行对所有执行sql的拦截,并进行操作。

package com.detain.system.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.detain.system.service.SystemService;

@Component
@Aspect
public class OperationRecordLog {
    
    @Autowired
    private SystemService systemService;
    
    @Around(value = "execution(* org.springframework.orm.ibatis.SqlMapClientTemplate.delete(..)) or execution(* org.springframework.orm.ibatis.SqlMapClientTemplate.insert(..)) or execution(* org.springframework.orm.ibatis.SqlMapClientTemplate.queryForList(..)) or execution(* org.springframework.orm.ibatis.SqlMapClientTemplate.queryForMap(..)) or execution(* org.springframework.orm.ibatis.SqlMapClientTemplate.queryForObject(..)) or execution(* org.springframework.orm.ibatis.SqlMapClientTemplate.queryWithRowHandler(..)) or execution(* org.springframework.orm.ibatis.SqlMapClientTemplate.update(..))")
    public Object exec(ProceedingJoinPoint invocation) throws Throwable {
        Object result = invocation.proceed();
        Object[] args = invocation.getArgs();
        if (args.length > 0) {
            if (!args[0].toString().equals("system.saveSqlOperationRecord")) {
                try {
                    if (args.length>1) {
                        systemService.saveSqlOperationRecord(args[0].toString(), args[1]);
                    } else {
                        systemService.saveSqlOperationRecord(args[0].toString(), "");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
    
}


老项目?用的是ibatis?

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题