MybatisPlus排序查询时,将null值放到最后

1用户需求

使用达梦数据库(Mysql同理),查询结果,按照某些字段进行排序,将为null的值放到最后。按照更新时间排序,但是更新时间可能为null,因此将null的数据放到最后。

2解决方案

最简单的方式,当然是下面这种直接在SQL最后面 NULLS LAST ,但是问题是,我都用MybatisPlus,下面的这种SQL那肯定不会写了啊,要是用MybatisPlus还写下面这种单表SQL的查询的,我建议可以放弃MybatisPlus了

--达梦数据库
SELECT * FROM users ORDER BY OPERATE_DATE ASC NULLS LAST 

-- mysql
SELECT * FROM users ORDER BY OPERATE_DATE IS NULL, OPERATE_DATE ASC

先说最终解决方案,用mybatis拦截器修改最终执行的sql语句

达梦数据库下如何解决嗯?
思路就是将queryWrapper构造的SQL语句中的ASC替换成ASC NULLS LAST

即使用queryWrapper的orderBy时,mybatis-plus会生成这个SQL语句
SELECT * FROM users ORDER BY OPERATE_DATE ASC

而我们要做的就是在mybatis-plus执行之前,将ASC变成 ASC NULLS LAST 

下面是我们进行排序的代码。目前来看,我们只能改这里,不过查找了一圈,都没有解决方案,因此放弃,用另外拦截器的方式实现。

if(!ObjectUtils.isEmpty(orderBy)) {
	if(orderBy instanceof Collection) {
		String[] array = ((Collection<?>) orderBy).toArray(new String[0]);
		queryWrapper.orderBy(true, isAsc, Arrays.asList(array));
	}else {
		queryWrapper.orderBy(true, isAsc, orderBy.toString());
	}
}

当然GPT一本正经的胡说八道,看着挺像回事的,可惜mybait-plus没有这个方法,所以看看就好。

orderByAscWithNullsLast()

在这里插入图片描述

3拦截器代码

这里开始,就是最后的代码实现了

3.1编写拦截器LastNullInterceptor

import java.lang.reflect.Field;
import java.sql.Connection;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.apache.ibatis.mapping.StatementType;
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.reflection.MetaObject;
import org.apache.ibatis.reflection.SystemMetaObject;
import org.springframework.util.ReflectionUtils;

import com.baomidou.mybatisplus.core.toolkit.PluginUtils;


/**
 * @description:拦截查询SQL,处理查询SQL中的排序
 * @author:hutao
 * @mail:hutao_2017@aliyun.com
 * @date:2023年7月25日 下午12:17:50
 */
@Intercepts(@Signature(type = StatementHandler.class, method = "prepare", args = {Connection.class, Integer.class}))
public class LastNullInterceptor implements Interceptor {
	//,有兴趣,可以看看MybatisPlusInterceptor怎么实现的
    private static final String DESC = "DESC";
    private static final String ASC = "ASC";
    private static final String REPLACE_DESC = "DESC NULLS LAST";
    private static final String REPLACE_ASC = "ASC NULLS LAST";
    
    @Override
    public Object intercept(Invocation invocation) throws Throwable {
    	StatementHandler handler = PluginUtils.realTarget(invocation.getTarget());
        MetaObject metaObject = SystemMetaObject.forObject(handler);
        // 判断是不是SELECT操作,跳过存储过程
        MappedStatement mappedStatement = (MappedStatement) metaObject.getValue("delegate.mappedStatement");
        if (SqlCommandType.SELECT != mappedStatement.getSqlCommandType()
                || StatementType.CALLABLE == mappedStatement.getStatementType()) {
            return invocation.proceed();
        }
        BoundSql boundSql = handler.getBoundSql();
        String sql = boundSql.getSql().toUpperCase();
        if(sql.contains("ORDER BY")) {
        	sql = this.replaceLast(sql, DESC, REPLACE_DESC);
        	sql = this.replaceLast(sql, ASC, REPLACE_ASC);
        	Field sqlField = boundSql.getClass().getDeclaredField("sql");
        	ReflectionUtils.makeAccessible(sqlField);
        	ReflectionUtils.setField(sqlField, boundSql, sql);
        }
        return invocation.proceed();
    }
    
    /**
     * @description:替换最后一个字符串
     * @author:hutao
     * @mail:hutao1@epri.sgcc.com.cn
     * @date:2023年7月25日 下午2:22:21
     */
    public String replaceLast(String str, String target, String replacement) {
    	if (str == null || target == null || replacement == null) {
            return str;
        }
        int lastIndex = str.lastIndexOf(target);
        if (lastIndex < 0) {
            return str;
        }
        return str.substring(0, lastIndex) + replacement + str.substring(lastIndex + target.length());
    }
}

3.2注入拦截器

@SpringBootConfiguration
public class MybatisConfig {
	@Bean
    public LastNullInterceptor nullsLastInterceptor() {
        return new LastNullInterceptor();
    }
}

4结果展示

打印mybatis-plus的sql,我们可以发现,已经将ASC替换成 ASC NULLS LAST了
在这里插入图片描述

备注:评论区有小伙伴说,使用

queryWrapper.last(" nulls last ")

这种方式是在sql的最后拼接上,所以,如果有类似分页的这种,还需要在最后加上limit 等关键字的就会报错。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值