MybatisPlus版本
com.baomidou mybatis-plus-boot-starter 3.4.2 com.baomidou mybatis-plus-extension 3.4.2
定义MybatisPlus工具类
import com.baomidou.mybatisplus.core.conditions.ISqlSegment;import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;import com.baomidou.mybatisplus.core.metadata.OrderItem;import com.xnt.product.common.core.core.domain.OrderBy;import com.xnt.product.common.core.exception.CustomException;import java.util.List;import static com.baomidou.mybatisplus.core.enums.SqlKeyword.*;public class MybatisPlusUtil { public static void setOrderOne(LambdaQueryWrapper queryWrapper, String column, boolean asc) { try { ISqlSegment[] sqlSegments = {ORDER_BY, () -> column, asc ? ASC : DESC}; queryWrapper.getExpression().add(sqlSegments); } catch (Exception e) { e.printStackTrace(); throw new CustomException("设置排序出错:" + e.getMessage()); } } public static void setOrder(LambdaQueryWrapper queryWrapper, OrderBy order) { setOrderOne(queryWrapper, order.getColumn(), order.isAsc()); } public static void setOrder(LambdaQueryWrapper queryWrapper, List list) { for (OrderBy order : list) { setOrder(queryWrapper, order); } } public static void setOrderItem(LambdaQueryWrapper queryWrapper, OrderItem orderItem) { setOrderOne(queryWrapper, orderItem.getColumn(), orderItem.isAsc()); } public static void setOrderItem(LambdaQueryWrapper queryWrapper, List list) { for (OrderItem order : list) { setOrderItem(queryWrapper, order); } }}
定义OrderBy实体类
@Datapublic class OrderBy { @NotEmpty private String column; private boolean asc = true; private boolean toUnder = false; public String getColumn() { return isToUnder() ? StrUtil.toUnderlineCase(column) : column; } public String getColumn(boolean toUnder) { return toUnder ? StrUtil.toUnderlineCase(column) : column; } public OrderItem getOrderItem() { return getOrderItem(isAsc(), isToUnder()); } public OrderItem getOrderItem(boolean isAsc) { return getOrderItem(isAsc, isToUnder()); } public OrderItem getOrderItemToUnder(boolean isToUnder) { return getOrderItem(isAsc(), isToUnder); } public OrderItem getOrderItem(String sort, boolean isToUnder) { return getOrderItem(isAsc(), isToUnder); } public OrderItem getOrderItem(boolean isAsc, boolean isToUnder) { String col = isToUnder ? StrUtil.toUnderlineCase(column) : column; return OrderItemUtils.getOrder(col, isAsc); } public String getSql(){ return (isToUnder() ? StrUtil.toUnderlineCase(column) : column) + (this.isAsc()? " asc":" desc"); } }
使用自定义排序
1、获取前段参数
public List getOrderBys() { String order = ServletUtils.getParameter(PageConstants.ORDER); if (ObjectUtil.isEmpty(order)) { return null; } List orderByList = new ArrayList<>(); if (JsonUtil.isTypeJSONArray(order)) { orderByList.addAll(JsonUtil.toList(order, OrderBy.class)); } else { orderByList.add(JsonUtil.parse(order, OrderBy.class)); } return orderByList; }
2、调用工具类
public List getList(SysOperLog operLog) { LambdaQueryWrapper lambda = getLambda(); List list = getOrderItems(); if (ObjectUtils.isNotEmpty(list)) { MybatisPlusUtil.setOrderItem(lambda, list); } else { lambda.orderByDesc(SysOperLog::getOperTime); } return super.list(lambda); }
来源地址:https://blog.csdn.net/qq_43040552/article/details/129276021