MybatisPlus使用排序查詢時將null值放到最后
1用戶需求
查詢結(jié)果,按照某些字段進行排序,將為null的值放到最后。按照更新時間排序,但是更新時間可能為null,因此將null的數(shù)據(jù)放到最后。
2解決方案
最簡單的方式,當然是下面這種直接在SQL最后面 NULLS LAST ,但是問題是,我都用MybatisPlus,下面的這種SQL那肯定不會寫了啊,要是用MybatisPlus還寫下面這種單表SQL的查詢的,我建議可以放棄MybatisPlus了
SELECT * FROM users ORDER BY OPERATE_DATE ASC NULLS LAST
先說最終解決方案,用mybatis攔截器修改最終執(zhí)行的sql語句
思路就是將queryWrapper構(gòu)造的SQL語句中的ASC替換成ASC NULLS LAST
即使用queryWrapper的orderBy時,mybatis-plus會生成這個SQL語句
SELECT * FROM users ORDER BY OPERATE_DATE ASC
而我們要做的就是在mybatis-plus執(zhí)行之前,將ASC變成 ASC NULLS LAST
下面是我們進行排序的代碼。目前來看,我們只能改這里,不過查找了一圈,都沒有解決方案,因此放棄,用另外攔截器的方式實現(xiàn)。
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一本正經(jīng)的胡說八道,看著挺像回事的,可惜mybait-plus沒有這個方法,所以看看就好。
orderByAscWithNullsLast()
3攔截器代碼
這里開始,就是最后的代碼實現(xiàn)了
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怎么實現(xiàn)的 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結(jié)果展示
打印mybatis-plus的sql,我們可以發(fā)現(xiàn),已經(jīng)將ASC替換成 ASC NULLS LAST了
到此這篇關(guān)于MybatisPlus使用排序查詢時將null值放到最后的文章就介紹到這了,更多相關(guān)MybatisPlus將null值放到最后內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
阿里巴巴 Sentinel + InfluxDB + Chronograf 實現(xiàn)監(jiān)控大屏
這篇文章主要介紹了阿里巴巴 Sentinel + InfluxDB + Chronograf 實現(xiàn)監(jiān)控大屏,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-09-09前端如何調(diào)用后端接口進行數(shù)據(jù)交互詳解(axios和SpringBoot)
一般來講前端不會給后端接口,而是后端給前端接口的情況比較普遍,下面這篇文章主要給大家介紹了關(guān)于前端如何調(diào)用后端接口進行數(shù)據(jù)交互的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-03-03Spring Cloud-Feign服務(wù)調(diào)用的問題及處理方法
Feign 是一個聲明式的 REST 客戶端,它用了基于接口的注解方式,很方便實現(xiàn)客戶端配置。接下來通過本文給大家介紹Spring Cloud-Feign服務(wù)調(diào)用,需要的朋友可以參考下2021-10-10SpringBoot開發(fā)案例 分布式集群共享Session詳解
這篇文章主要介紹了SpringBoot開發(fā)案例 分布式集群共享Session詳解,在分布式系統(tǒng)中,為了提升系統(tǒng)性能,通常會對單體項目進行拆分,分解成多個基于功能的微服務(wù),可能還會對單個微服務(wù)進行水平擴展,保證服務(wù)高可用,需要的朋友可以參考下2019-07-07