-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ccf945
commit be1213d
Showing
9 changed files
with
164 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.apache.ibatis.plugin; | ||
|
||
import org.apache.ibatis.session.Configuration; | ||
|
||
/** | ||
* @author furious 2024/4/19 | ||
*/ | ||
public interface Interceptor { | ||
|
||
String pointcut(); | ||
|
||
Object intercept(Invocation invocation) throws Throwable; | ||
|
||
default Object plugin(Object target, Configuration configuration) { | ||
return Plugin.wrap(target, configuration, this); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/org/apache/ibatis/plugin/InterceptorChain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.apache.ibatis.plugin; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.apache.ibatis.session.Configuration; | ||
|
||
/** | ||
* @author furious 2024/4/19 | ||
*/ | ||
public class InterceptorChain { | ||
|
||
private final Configuration configuration; | ||
private final List<Interceptor> interceptors = new ArrayList<>(); | ||
|
||
public InterceptorChain(Configuration configuration) { | ||
this.configuration = configuration; | ||
} | ||
|
||
public <T> T pluginAll(Object target) { | ||
for (Interceptor interceptor : interceptors) { | ||
target = interceptor.plugin(target, configuration); | ||
} | ||
return (T) target; | ||
} | ||
|
||
public void addInterceptor(Interceptor interceptor) { | ||
interceptors.add(interceptor); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.apache.ibatis.plugin; | ||
|
||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
import java.util.Objects; | ||
|
||
import org.apache.ibatis.session.Configuration; | ||
|
||
/** | ||
* @author furious 2024/4/19 | ||
*/ | ||
public class Plugin implements InvocationHandler { | ||
|
||
private final Object target; | ||
private final Configuration configuration; | ||
private final Interceptor interceptor; | ||
|
||
public Plugin(Object target, Configuration configuration, Interceptor interceptor) { | ||
this.target = target; | ||
this.configuration = configuration; | ||
this.interceptor = interceptor; | ||
} | ||
|
||
public static Object wrap(Object target, Configuration configuration, Interceptor interceptor) { | ||
String pointcut = interceptor.pointcut(); | ||
PointcutDefinition definition = configuration.getPointcutRegistry().getDefinition(pointcut); | ||
//不是当前接口类型不代理 | ||
if (!definition.getType().isAssignableFrom(target.getClass())) { | ||
return target; | ||
} | ||
return Proxy.newProxyInstance(target.getClass().getClassLoader(), new Class<?>[]{definition.getType()}, new Plugin(target, configuration, interceptor)); | ||
} | ||
|
||
@Override | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
PointcutDefinition definition = configuration.getPointcutRegistry().getDefinition(interceptor.pointcut()); | ||
if (!Objects.equals(definition.getId(), PointcutDefinition.id(method))) { | ||
//没有代理该方法,直接返回 | ||
return method.invoke(target, args); | ||
} | ||
return interceptor.intercept(new Invocation(target, method, args)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
src/test/java/org/apache/ibatis/plugin/PageInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.apache.ibatis.plugin; | ||
|
||
import java.sql.Connection; | ||
|
||
import org.apache.ibatis.executor.statement.StatementHandler; | ||
import org.apache.ibatis.mapping.BoundSql; | ||
|
||
/** | ||
* @author furious 2024/4/19 | ||
*/ | ||
public class PageInterceptor implements Interceptor { | ||
@Override | ||
public String pointcut() { | ||
return PointcutRegistry.StatementHandler_prepare_Connection_Integer; | ||
} | ||
|
||
@Override | ||
public Object intercept(Invocation invocation) throws Throwable { | ||
StatementHandler statementHandler = invocation.getTarget(); | ||
BoundSql original = statementHandler.getBoundSql(); | ||
Object[] args = invocation.getArgs(); | ||
Connection connection = (Connection) args[0]; | ||
//不判读是否为select语句,limit仅为示范参数 | ||
String sql = original.getSql(); | ||
sql += " LIMIT 1,1"; | ||
return connection.prepareStatement(sql); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters