OKJSP에 회원님께서 MyBatis 인터셉터 관련 내용을 문의했었는데 답변단 내용에
관련 글을 공유코자 올려봅니다.
자체적으로 인터셉터 된다는걸 알고 있지는 않았었는데 질문이 올라와 테스트겸 해 봤는데 잘되네요..ㅎㅎ
뭐든간에 매뉴얼 정독이 필요한 부분이긴 하나 그럴 시간이 부족한 현실이 조금 아쉽네요..ㅋㅋ
<plugins> <plugin interceptor="handler.QueryInterceptor"/> <plugin interceptor="handler.UpdateInterceptor"/> </plugins>
package handler; import java.util.Properties; import org.apache.ibatis.cache.CacheKey; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.session.ResultHandler; import org.apache.ibatis.session.RowBounds; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; @Intercepts ( { @Signature(type = Executor.class, method = "update", args = {MappedStatement.class, Object.class}) ,@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}) ,@Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}) } ) public class QueryInterceptor implements Interceptor { private Logger log = LogManager.getLogger(getClass()); @Override public Object intercept(Invocation invocation) throws Throwable { Object[] args = invocation.getArgs(); MappedStatement ms = (MappedStatement)args[0]; Object param = (Object)args[1]; BoundSql boundSql = ms.getBoundSql(param); System.out.println("===================================="); System.out.println(invocation.getMethod().getName()); System.out.println("===================================="); System.out.println(ms.getId()); System.out.println("===================================="); System.out.println(boundSql.getSql()); System.out.println("===================================="); System.out.println(param); System.out.println("===================================="); log.debug(ms.getId()); return invocation.proceed(); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { } }
package handler; import java.sql.Statement; import java.util.Properties; import org.apache.ibatis.executor.statement.StatementHandler; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.session.ResultHandler; import org.apache.log4j.LogManager; import org.apache.log4j.Logger; @Intercepts ( { @Signature(type = StatementHandler.class, method = "update", args = {Statement.class}) ,@Signature(type = StatementHandler.class, method = "query", args = {Statement.class, ResultHandler.class}) } ) public class UpdateInterceptor implements Interceptor { private Logger log = LogManager.getLogger(getClass()); @Override public Object intercept(Invocation invocation) throws Throwable { StatementHandler handler = (StatementHandler) invocation.getTarget(); //쿼리문 String sql = handler.getBoundSql().getSql(); //매핑 자료 String param = handler.getParameterHandler().getParameterObject() != null ? handler.getParameterHandler().getParameterObject().toString() : ""; System.out.println("--------------------------------"); System.out.println(sql); System.out.println(param); System.out.println("--------------------------------"); System.out.println(invocation.getMethod().getName()); System.out.println(invocation.getMethod().getAnnotations()); System.out.println("--------------------------------"); log.debug(sql); log.debug(param); return invocation.proceed(); } @Override public Object plugin(Object target) { return Plugin.wrap(target, this); } @Override public void setProperties(Properties properties) { } }