前言
工作中写的一段代码,备个份,以后兴许能直接用
功能描述:如果前端传入了排序规则,则优先按传入的字段进行排序,SQL原有的排序规则追加到末尾
注:我们项目里的分页查询,是基于XML的SQL执行的,没有直接使用mybatis-plus的 IPage
正文
定义拦截器
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrPool;
import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
import com.baomidou.mybatisplus.extension.parser.JsqlParserSupport;
import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
import lombok.extern.slf4j.Slf4j;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.FromItem;
import net.sf.jsqlparser.statement.select.OrderByElement;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import org.apache.ibatis.binding.MapperMethod;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlCommandType;
import org.jetbrains.annotations.Nullable;
import org.springframework.util.ObjectUtils;
import java

文章介绍了如何在Mybatis项目中通过定义拦截器,处理前端传入的排序规则,确保排序优先级,同时保持SQL执行的安全性。作者提到该解决方案不依赖于字符串拼接,而是利用PreparedStatement进行防SQL注入的执行。
最低0.47元/天 解锁文章
3380

被折叠的 条评论
为什么被折叠?



