|
@@ -0,0 +1,58 @@
|
|
|
+package com.dgtly.apiframework.interceptor;
|
|
|
+
|
|
|
+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.*;
|
|
|
+import org.apache.ibatis.session.ResultHandler;
|
|
|
+import org.apache.ibatis.session.RowBounds;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.Properties;
|
|
|
+
|
|
|
+@Component
|
|
|
+@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 SwitchEnSqlInterceptor implements Interceptor {
|
|
|
+ private Logger log= LoggerFactory.getLogger(SwitchEnSqlInterceptor.class);
|
|
|
+ public SwitchEnSqlInterceptor() {
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object intercept(Invocation invocation) throws Throwable {
|
|
|
+ // 获取原始sql语句
|
|
|
+ MappedStatement mappedStatement = (MappedStatement) invocation.getArgs()[0];
|
|
|
+ Object parameter = invocation.getArgs()[1];
|
|
|
+ BoundSql boundSql = mappedStatement.getBoundSql(parameter);
|
|
|
+ String oldsql = boundSql.getSql();
|
|
|
+ // log.info("old:"+oldsql);
|
|
|
+ oldsql = oldsql.replaceAll("gi.name","gi.id name");
|
|
|
+ // 改变sql语句
|
|
|
+ BoundSql newBoundSql = new BoundSql(mappedStatement.getConfiguration(), oldsql ,
|
|
|
+ boundSql.getParameterMappings(), boundSql.getParameterObject());
|
|
|
+ invocation.getArgs()[5] = newBoundSql;
|
|
|
+ // 注解invocation.method.annotations()
|
|
|
+ // 继续执行
|
|
|
+ Object result = invocation.proceed();
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public Object plugin(Object target) {
|
|
|
+ // TODO Auto-generated method stub
|
|
|
+ return Plugin.wrap(target, this);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setProperties(Properties properties) {
|
|
|
+
|
|
|
+ }
|
|
|
+}
|