com.wsun.seap.dao.persistence.interceptor.PreparePaginationInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for com.wsun.seap.dao.persistence.interceptor.PreparePaginationInterceptor.java

Source

/**
 * Copyright &copy; 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 */
package com.wsun.seap.dao.persistence.interceptor;

import com.wsun.seap.common.utils.ReflectionsUtil;
import com.wsun.seap.dao.interceptor.BaseInterceptor;
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.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;

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

/**
 * Mybatis??StatementHandlerprepare
 *
 * @author poplar.yfyang / thinkgem
 * @version 2013-8-28
 */
@Intercepts({ @Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class }) })
public class PreparePaginationInterceptor extends BaseInterceptor {

    private static final long serialVersionUID = 1L;

    public PreparePaginationInterceptor() {
        super();
    }

    @Override
    public Object intercept(Invocation ivk) throws Throwable {
        if (ivk.getTarget().getClass().isAssignableFrom(RoutingStatementHandler.class)) {
            final RoutingStatementHandler statementHandler = (RoutingStatementHandler) ivk.getTarget();
            final BaseStatementHandler delegate = (BaseStatementHandler) ReflectionsUtil
                    .getFieldValue(statementHandler, DELEGATE);
            final MappedStatement mappedStatement = (MappedStatement) ReflectionsUtil.getFieldValue(delegate,
                    MAPPED_STATEMENT);

            BoundSql boundSql = delegate.getBoundSql();
            //SQL<select>parameterType??Mapper??,??
            Object parameterObject = boundSql.getParameterObject();
            if (parameterObject == null) {
                log.error("?");
                throw new NullPointerException("parameterObject?");
            } else {
                final Connection connection = (Connection) ivk.getArgs()[MAPPED_STATEMENT_INDEX];
                final String sql = boundSql.getSql();
                //
            }

            if (boundSql.getSql() == null || "".equals(boundSql.getSql())) {
                return null;
            }
        }
        return ivk.proceed();
    }

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

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