中国免费网站服务器2020,seo专员是指什么意思,淘宝网站建设的目的,做网站自己不会维护怎么办1用户需求
查询结果#xff0c;按照某些字段进行排序#xff0c;将为null的值放到最后。按照更新时间排序#xff0c;但是更新时间可能为null#xff0c;因此将null的数据放到最后。
2解决方案
最简单的方式#xff0c;当然是下面这种直接在SQL最后面 NULLS LAST …1用户需求
查询结果按照某些字段进行排序将为null的值放到最后。按照更新时间排序但是更新时间可能为null因此将null的数据放到最后。
2解决方案
最简单的方式当然是下面这种直接在SQL最后面 NULLS LAST 但是问题是我都用MybatisPlus下面的这种SQL那肯定不会写了啊要是用MybatisPlus还写下面这种单表SQL的查询的我建议可以放弃MybatisPlus了
SELECT * FROM users ORDER BY OPERATE_DATE ASC NULLS LAST 先说最终解决方案用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_2017aliyun.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;Overridepublic 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:hutao1epri.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 {Beanpublic LastNullInterceptor nullsLastInterceptor() {return new LastNullInterceptor();}
}4结果展示
打印mybatis-plus的sql我们可以发现已经将ASC替换成 ASC NULLS LAST了