REQ-2010: 尝试解决limit重复的问题

This commit is contained in:
yanglin 2024-03-01 16:47:06 +08:00
parent 538386c5ab
commit a2ac53b21c
2 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,36 @@
package cn.axzo.msg.center.notices.client.config;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* node_user表拦截器
*/
@Component
// 这里使用 dependsOn 是因为要控制加载顺序(防止加解密失败) 需要 mybatisInterceptorInspect 加载之后在处理
// 这时是不是有的大聪明就想说 咋不用 构造器注入或者 set 注入
// 如果使用构造器注入或者set 注入之后 idea编译器会报黄(对象未使用或者参数未使用)
// 如果某些大聪明直接删除了代码会导致加解密失败所以用 dependsOn
@RequiredArgsConstructor
@DependsOn("mybatisInterceptorInspect")
public class MybatisNodeUserInterceptor {
private final SqlSessionFactory sessionFactory;
@PostConstruct
public void init() {
Configuration configuration = sessionFactory.getConfiguration();
if (configuration instanceof MybatisConfiguration) {
MybatisConfiguration mybatisConfiguration = (MybatisConfiguration) configuration;
mybatisConfiguration.addInterceptor(new PaginationInterceptor());
mybatisConfiguration.addInterceptor(new RemoveDuplicateLimitInterceptor());
}
}
}

View File

@ -0,0 +1,68 @@
package cn.axzo.msg.center.notices.client.config;
import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
/**
* 把select和update语句中mybatis-plus自动添加到where条件里面的is_delete条件去掉(替换成常量表达式)
*
* @author yanglin
*/
@Intercepts(value = {
@Signature(type = Executor.class, method = "query",
args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})
})
public class RemoveDuplicateLimitInterceptor implements Interceptor {
@Override
public Object intercept(Invocation invocation) throws Throwable {
final Object[] args = invocation.getArgs();
MappedStatement ms = (MappedStatement) args[0];
BoundSql boundSql = ms.getBoundSql(args[1]);
String sql = boundSql.getSql();
if (sql.endsWith("LIMIT ? LIMIT ?") || sql.endsWith("limit ? limit ?")) {
sql = sql.substring(0, sql.length() - 8);
}
StaticSqlSource newSqlSource = new StaticSqlSource(
ms.getConfiguration(), sql, boundSql.getParameterMappings());
args[0] = copyMappedStatement(ms, newSqlSource);
return invocation.proceed();
}
private MappedStatement copyMappedStatement(MappedStatement ms, SqlSource sqlSource) {
MappedStatement.Builder builder =
new MappedStatement.Builder(ms.getConfiguration(), ms.getId(), sqlSource, ms.getSqlCommandType());
builder.resource(ms.getResource());
builder.fetchSize(ms.getFetchSize());
builder.statementType(ms.getStatementType());
builder.keyGenerator(ms.getKeyGenerator());
if (ms.getKeyProperties() != null && ms.getKeyProperties().length != 0) {
StringBuilder keyProperties = new StringBuilder();
for (String keyProperty : ms.getKeyProperties()) {
keyProperties.append(keyProperty).append(",");
}
keyProperties.delete(keyProperties.length() - 1, keyProperties.length());
builder.keyProperty(keyProperties.toString());
}
builder.timeout(ms.getTimeout());
builder.parameterMap(ms.getParameterMap());
builder.resultMaps(ms.getResultMaps());
builder.resultSetType(ms.getResultSetType());
builder.cache(ms.getCache());
builder.flushCacheRequired(ms.isFlushCacheRequired());
builder.useCache(ms.isUseCache());
return builder.build();
}
}