1.鎶奾ibernate涓嬬殑dialect鍖呭叏閮ㄦ嫹璐濆埌mybatis鍖呯殑jdbc鐩綍涓嬶紝濡備笅鍥炬墍紺猴細
2.瀹氫箟涓涓猂esultSetHandler Interceptor
package cn.machi.utils;
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
@Intercepts( {@Signature(type = ResultSetHandler.class, method = "handleResultSets", args = {Statement.class})})
public class DiclectResultSetHandlerInterceptor implements Interceptor
{
public Object intercept(Invocation invocation) throws Throwable
{
FastResultSetHandler resultSet = (FastResultSetHandler)invocation.getTarget();
RowBounds rowBounds = (RowBounds)ReflectUtil.getFieldValue(resultSet,
"rowBounds");
if (rowBounds.getLimit() > 0
&& rowBounds.getLimit() < RowBounds.NO_ROW_LIMIT)
{
ReflectUtil.setFieldValue(resultSet, "rowBounds", new RowBounds());
}
return invocation.proceed();
}
public Object plugin(Object target)
{
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties)
{
}
}
3.瀹氫箟涓涓猄tatementHandler鐨処nterceptor
package cn.machi.utils;
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
imp
@Intercepts( {@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class})})
public class DiclectStatementHandlerInterceptor implements Interceptor
{
private static final String DIALECT = "org.apache.ibatis.jdbc.dialect.OracleDialect";
public Object intercept(Invocation invocation) throws Throwable
{
RoutingStatementHandler statement = (RoutingStatementHandler)invocation.getTarget();
PreparedStatementHandler handler = (PreparedStatementHandler)ReflectUtil.getFieldValue(statement,
"delegate");
RowBounds rowBounds = (RowBounds)ReflectUtil.getFieldValue(handler,
"rowBounds");
if (rowBounds.getLimit() > 0
&& rowBounds.getLimit() < RowBounds.NO_ROW_LIMIT)
{
BoundSql boundSql = statement.getBoundSql();
String sql = boundSql.getSql();
OracleDialect dialect = (OracleDialect)Class.forName(DIALECT)
.newInstance();
sql = dialect.getLimitString(sql,
rowBounds.getOffset(),
rowBounds.getLimit());
ReflectUtil.setFieldValue(boundSql, "sql", sql);
}
return invocation.proceed();
}
public Object plugin(Object target)
{
return Plugin.wrap(target, this);
}
public void setProperties(Properties properties)
{
}
}
4.瀹氫箟宸ュ叿綾籖eflectUtil
package cn.machi.utils;
imp
imp
imp
imp
imp
public class ReflectUtil
{
private static Logger log = Logger.getLogger(ReflectUtil.class);
private static Object operate(Object obj, String fieldName,
Object fieldVal, String type)
{
Object ret = null;
try
{
// 鑾峰緱瀵硅薄綾誨瀷
Class<? extends Object> classType = obj.getClass();
// 鑾峰緱瀵硅薄鐨勬墍鏈夊睘鎬?nbsp;
Field fields[] = classType.getDeclaredFields();
for (int i = 0; i < fields.length; i++)
{
Field field = fields[i];
if (field.getName().equals(fieldName))
{
String firstLetter = fieldName.substring(0, 1)
.toUpperCase(); // 鑾峰緱鍜屽睘鎬у搴旂殑getXXX()鏂規(guī)硶鐨勫悕瀛?nbsp;
if ("set".equals(type))
{
String setMethodName = "set" + firstLetter
+ fieldName.substring(1); // 鑾峰緱鍜屽睘鎬у搴旂殑getXXX()鏂規(guī)硶
Method setMethod = classType.getMethod(setMethodName,
new Class[] {field.getType()}); // 璋冪敤鍘熷璞$殑getXXX()鏂規(guī)硶
ret = setMethod.invoke(obj, new Object[] {fieldVal});
}
if ("get".equals(type))
{
String getMethodName = "get" + firstLetter
+ fieldName.substring(1); // 鑾峰緱鍜屽睘鎬у搴旂殑setXXX()鏂規(guī)硶鐨勫悕瀛?nbsp;
Method getMethod = classType.getMethod(getMethodName,
new Class[] {});
ret = getMethod.invoke(obj, new Object[] {});
}
return ret;
}
}
}
catch (Exception e)
{
log.warn("reflect error:" + fieldName, e);
}
return ret;
}
public static Object getVal(Object obj, String fieldName)
{
return operate(obj, fieldName, null, "get");
}
public static void setVal(Object obj, String fieldName, Object fieldVal)
{
operate(obj, fieldName, fieldVal, "set");
}
private static Method getDeclaredMethod(Object object, String methodName,
Class<?>[] parameterTypes)
{
for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass())
{
try
{
//superClass.getMethod(methodName, parameterTypes);
return superClass.getDeclaredMethod(methodName, parameterTypes);
}
catch (NoSuchMethodException e)
{
//Method 涓嶅湪褰撳墠綾誨畾涔? 緇х畫鍚戜笂杞瀷
}
}
return null;
}
private static void makeAccessible(Field field)
{
if (!Modifier.isPublic(field.getModifiers()))
{
field.setAccessible(true);
}
}
private static Field getDeclaredField(Object object, String filedName)
{
for (Class<?> superClass = object.getClass(); superClass != Object.class; superClass = superClass.getSuperclass())
{
try
{
return superClass.getDeclaredField(filedName);
}
catch (NoSuchFieldException e)
{
//Field 涓嶅湪褰撳墠綾誨畾涔? 緇х畫鍚戜笂杞瀷
}
}
return null;
}
public static Object invokeMethod(Object object, String methodName,
Class<?>[] parameterTypes, Object[] parameters)
throws InvocationTargetException
{
Method method = getDeclaredMethod(object, methodName, parameterTypes);
if (method == null)
{
throw new IllegalArgumentException("Could not find method ["
+ methodName + "] on target [" + object + "]");
}
method.setAccessible(true);
try
{
return method.invoke(object, parameters);
}
catch (IllegalAccessException e)
{
}
return null;
}
public static void setFieldValue(Object object, String fieldName,
Object value)
{
Field field = getDeclaredField(object, fieldName);
if (field == null)
throw new IllegalArgumentException("Could not find field ["
+ fieldName + "] on target [" + object + "]");
makeAccessible(field);
try
{
field.set(object, value);
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
public static Object getFieldValue(Object object, String fieldName)
{
Field field = getDeclaredField(object, fieldName);
if (field == null)
throw new IllegalArgumentException("Could not find field ["
+ fieldName + "] on target [" + object + "]");
makeAccessible(field);
Object result = null;
try
{
result = field.get(object);
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
return result;
}
}
5.鏇存柊mapper configuration鏂囦歡錛屾坊鍔犲涓嬪嚑鏉★紝娉ㄦ剰plugins鍦ㄦ暣涓猚onfiguration鏂囦歡涓殑欏哄簭
<plugins>
<plugin interceptor="functionPoint.db.DiclectStatementHandlerInterceptor"/>
<plugin interceptor="functionPoint.db.DiclectResultSetHandlerInterceptor"/>
</plugins>
6.浣跨敤鏂規(guī)硶鍚宮ybatis閫昏緫鍒嗛〉錛屾嫤鎴櫒浼氳嚜鍔ㄦ嫤鎴墽琛孲QL鐨勫湴鏂癸紝鍔犱笂鍒嗛〉浠g爜錛?/strong>
getSqlSession().selectList(mapId, queryKey,new RowBounds(pageId, pageSize));