2013년 6월 26일 수요일

MyBatis에서 ResultSet 처리부분 가로채기(플러그인) - 참조

출처:
http://beyondj2ee.pbworks.com/w/page/53607818/MyBatis%EC%97%90%EC%84%9C%20ResultSet%20%EC%B2%98%EB%A6%AC%EB%B6%80%EB%B6%84%20%EA%B0%80%EB%A1%9C%EC%B1%84%EA%B8%B0(%ED%94%8C%EB%9F%AC%EA%B7%B8%EC%9D%B8

package org.syzone.demo.plugins;

import org.apache.ibatis.executor.resultset.ResultSetHandler;
import org.apache.ibatis.plugin.*;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.util.*;

/**
 *
 *
 * @author ZengJun
 * @version $$Revision$$
 */
@Intercepts({@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})})
public class DemoPlugin implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        List actual = new ArrayList();
        Object[] args = invocation.getArgs();
        Statement statement = (Statement) args[0];
        ResultSet rs = statement.getResultSet();
        while (rs == null) {
            if (statement.getMoreResults()) {
                rs = statement.getResultSet();
            } else {
                if (statement.getUpdateCount() == -1) {
                    break;
                }
            }
        }
        if (rs != null) {
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            Object[] metaData = new Object[columnCount];
            for (int i = 1; i <= columnCount; i++) {
                Map columnMeta = new LinkedHashMap();
                String columnName = rsmd.getColumnName(i);
                String columnLabel = rsmd.getColumnLabel(i);
                String columnType = rsmd.getColumnTypeName(i);
                columnMeta.put("columnName", columnName);
                columnMeta.put("columnLabel", columnLabel);
                columnMeta.put("columnType", columnType);
                metaData[i-1] = columnMeta;
            }
            actual.add(metaData);
        }
        Object results = invocation.proceed();
        actual.add(results);
        return actual;
    }

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

    @Override
    public void setProperties(Properties properties) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}

댓글 없음:

댓글 쓰기