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

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

小怪兽爱吃肉
浏览 846回答 3
3回答

www说

通过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?

holdtom

帮忙star一个,谢谢
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java