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

您所在的位置:网站首页 当月离职率公式 MybatisPlus排序查询时,将null值放到最后

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

2024-07-08 22:50| 来源: 网络整理| 查看: 265

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:[email protected] * @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:[email protected] * @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


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3