现在需要将所有ibatis的执行sql进行拦截并记录进数据库,但是没有找到ibatis的相关拦截器(只找到了mybatis的拦截器),请问应该如何获取到所有的执行sql并进行记录。
3 回答
www说
TA贡献1775条经验 获得超8个赞
通过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;
}
}
添加回答
举报
0/150
提交
取消