All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yuehuanghun.mybatis.milu.MiluInterceptor Maven / Gradle / Ivy

There is a newer version: 1.18.0
Show newest version
package com.yuehuanghun.mybatis.milu;

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.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import com.yuehuanghun.mybatis.milu.mapping.ResultMapHelper;

/**
 * 仅用于执行查询后,清除ThreadLocal数据
 * 
 * @author yuehuanghun
 *
 */
@Intercepts(
        {
                @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
                @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class}),
        }
)
public class MiluInterceptor implements Interceptor {
    private String countSuffix = "_COUNT";
    
	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		String statementId = null;
		try {
			MappedStatement statement = (MappedStatement) invocation.getArgs()[0];
			statementId = statement.getId();
			return invocation.proceed();
		} finally {
			//判断是否为PageHelper的行数查询statement
			if(statementId != null && !statementId.endsWith(countSuffix)) {
				ResultMapHelper.clear();
			}
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy