com.ext_ext.mybatisext.interceptor.MyBatisInvocation Maven / Gradle / Ivy
package com.ext_ext.mybatisext.interceptor;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
public class MyBatisInvocation {
private final SqlSession sqlSession;
private final Object[] args;
private final Class> mapperInterface;
private final Method method;
private final MyBatisInterceptor[] interceptors;
private int index = 0;
private int batchIndex = -1;
private final Map paramsMap;
private final Map methodCache;
public MyBatisInvocation(Class> mapperInterface, SqlSession sqlSession, Method method, Object[] args,
MyBatisInterceptor[] interceptors, Map methodCache) {
this.sqlSession = sqlSession;
this.args = args;
this.mapperInterface = mapperInterface;
this.method = method;
this.interceptors = interceptors;
this.methodCache = methodCache;
paramsMap = getParamMap(method, args);
}
public Object execute() throws Throwable {
if (interceptors == null) {
final MapperMethod mapperMethod = cachedMapperMethod();
return mapperMethod.execute(sqlSession, args);
}
if (index < interceptors.length) {
return interceptors[index++].invoke(this);
} else {
final MapperMethod mapperMethod = cachedMapperMethod();
return mapperMethod.execute(sqlSession, args);
}
}
private MapperMethod cachedMapperMethod() {
MapperMethod mapperMethod = methodCache.get(method);
if (mapperMethod == null) {
mapperMethod = new MapperMethod(mapperInterface, method, sqlSession.getConfiguration());
methodCache.put(method, mapperMethod);
}
return mapperMethod;
}
private Map getParamMap(Method m, Object[] a) {
Annotation[][] parameterAnnotations = m.getParameterAnnotations();
Map map = new HashMap();
for (int i = 0; i < parameterAnnotations.length; i++) {
for (Annotation annotation : parameterAnnotations[i]) {
if (annotation instanceof Param) {
Param myAnnotation = (Param) annotation;
map.put(myAnnotation.value(), a[i]);
}
}
}
return map;
}
public Map getParamsMap() {
return paramsMap;
}
public Configuration getConfiguration() {
return sqlSession.getConfiguration();
}
public Object[] getArgs() {
return args;
}
public Class> getMapperInterface() {
return mapperInterface;
}
public Method getMethod() {
return method;
}
public int getBatchIndex() {
return batchIndex;
}
public void batchIndexIncrease() {
batchIndex++;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy