REQ-2010: 尝试解决limit重复的问题
This commit is contained in:
parent
d1ed8d556d
commit
7de5f7cebf
@ -0,0 +1,33 @@
|
||||
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.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
/**
|
||||
* node_user表拦截器
|
||||
*/
|
||||
@Order(-2)
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,5 @@
|
||||
package cn.axzo.msg.center.notices.client.config;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@ -14,18 +13,6 @@ import org.springframework.context.annotation.Configuration;
|
||||
@Configuration
|
||||
public class MybatisPlusConfig {
|
||||
|
||||
/**
|
||||
* 默认不配置分页插件的,会使用人RowBound进行分页,实际上是逻辑分页,物理上不会分页,也就是查询出来
|
||||
* 缓存中再分页;这样对于数据量比较多的情况,不合适,因此需要配置这个分页拦截器来实现物理的分页;
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public PaginationInterceptor paginationInterceptor() {
|
||||
PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
|
||||
// 设置分页的最大条数
|
||||
return paginationInterceptor;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public EntityMetaObjectHandler EntityMetaObjectHandler() {
|
||||
return new EntityMetaObjectHandler();
|
||||
|
||||
@ -0,0 +1,66 @@
|
||||
package cn.axzo.msg.center.notices.client.config;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
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]);
|
||||
SQLStatement stmt = new MySqlStatementParser(boundSql.getSql()).parseSelect();
|
||||
StaticSqlSource newSqlSource = new StaticSqlSource(
|
||||
ms.getConfiguration(), stmt.toString(), 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();
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user