热门关键字:
jquery > jquery教程 > java > mybatis运行时SQL输出的java代码类

mybatis运行时SQL输出的java代码类

953
作者:管理员
发布时间:2015/7/28 15:46:26
评论数:0
转载请自觉注明原文:http://www.jq-school.com/Show.aspx?id=692
package com.xzq.util;

import java.sql.Connection;
import java.util.Properties;

import org.apache.ibatis.executor.statement.BaseStatementHandler;
import org.apache.ibatis.executor.statement.RoutingStatementHandler;
import org.apache.ibatis.executor.statement.StatementHandler;
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;

@Intercepts( {@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class})})
public class SQLInterceptor implements Interceptor {
	
    public Object intercept(Invocation invocation) throws Throwable {
    	 RoutingStatementHandler statementHandler = (RoutingStatementHandler)invocation.getTarget(); 
    	 BoundSql boundSql = statementHandler.getBoundSql();
    	 BaseStatementHandler delegate = (BaseStatementHandler) ReflectHelper.getValueByFieldName(statementHandler, "delegate");
    	 MappedStatement mappedStatement = (MappedStatement) ReflectHelper.getValueByFieldName(delegate, "mappedStatement");
    	 System.out.println("-------------------------------------------------" + mappedStatement.getId());
    	 System.out.println(boundSql.getSql()); 
    	 return invocation.proceed();  
    }

	@Override
    public Object plugin(Object arg0) {
        return Plugin.wrap(arg0, this);
    }

	@Override
	public void setProperties(Properties properties) { 
	}
}




如果您觉得本文的内容对您的学习有所帮助:支付鼓励



关键字:mybatis Interceptor
友荐云推荐