REQ-3502: fix反射问题

This commit is contained in:
yanglin 2025-01-09 17:44:08 +08:00
parent 8f0b1bf685
commit fc3f568be1

View File

@ -18,6 +18,7 @@ import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.reflection.DefaultReflectorFactory;
import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject; import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
@ -36,12 +37,13 @@ import java.util.concurrent.atomic.AtomicBoolean;
args = {Connection.class, Integer.class})}) args = {Connection.class, Integer.class})})
public class BeautifulPaginationInterceptor implements Interceptor, Ordered { public class BeautifulPaginationInterceptor implements Interceptor, Ordered {
private static final DefaultReflectorFactory REFLECTOR_FACTORY = new DefaultReflectorFactory();
private final PaginationInterceptor delegate = new CustomPaginationInterceptor(); private final PaginationInterceptor delegate = new CustomPaginationInterceptor();
@Override @Override
public Object intercept(Invocation invocation) throws Throwable { public Object intercept(Invocation invocation) throws Throwable {
StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget()); StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
MetaObject metaObject = SystemMetaObject.forObject(statementHandler); MetaObject metaObject = systemMetaForObject(statementHandler);
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement"); MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
if (SqlCommandType.SELECT != mappedStatement.getSqlCommandType() if (SqlCommandType.SELECT != mappedStatement.getSqlCommandType()
|| StatementType.CALLABLE == mappedStatement.getStatementType()) { || StatementType.CALLABLE == mappedStatement.getStatementType()) {
@ -70,7 +72,7 @@ public class BeautifulPaginationInterceptor implements Interceptor, Ordered {
private static String tryBuildSql(Invocation invocation) { private static String tryBuildSql(Invocation invocation) {
Connection connection = (Connection) (invocation.getArgs()[0]); Connection connection = (Connection) (invocation.getArgs()[0]);
StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget()); StatementHandler statementHandler = PluginUtils.realTarget(invocation.getTarget());
MetaObject metaObject = SystemMetaObject.forObject(statementHandler); MetaObject metaObject = systemMetaForObject(statementHandler);
MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement"); MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql"); BoundSql boundSql = (BoundSql) metaObject.getValue("delegate.boundSql");
try (PreparedStatement statement = connection.prepareStatement(boundSql.getSql())) { try (PreparedStatement statement = connection.prepareStatement(boundSql.getSql())) {
@ -86,6 +88,13 @@ public class BeautifulPaginationInterceptor implements Interceptor, Ordered {
} }
} }
public static MetaObject systemMetaForObject(Object object) {
return MetaObject.forObject(object,
SystemMetaObject.DEFAULT_OBJECT_FACTORY,
SystemMetaObject.DEFAULT_OBJECT_WRAPPER_FACTORY,
REFLECTOR_FACTORY);
}
private static class CustomPaginationInterceptor extends PaginationInterceptor { private static class CustomPaginationInterceptor extends PaginationInterceptor {
public CustomPaginationInterceptor() { public CustomPaginationInterceptor() {