How to intercept all execution sql, in ibatis and record it in database

now you need to intercept all the execution sql of ibatis and record them in the database, but you can"t find the relevant interceptor of ibatis (only the interceptor of mybatis). How to get all execution sql and record it?

Nov.29,2021

Old project? Use ibatis?


help star , thank you


if you use spring aop to intercept the methods under SqlMapClientTemplate, you can intercept all executable sql and perform operations.

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


Menu