MybatisPlus版本
com.baomidoumybatis-plus-boot-starter3.4.2com.baomidoumybatis-plus-extension3.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.*;/** * @author wxm */public class MybatisPlusUtil {/** * @param queryWrapper {@link LambdaQueryWrapper} * @param column 排序字段 * @param asc是否升序 * @param 泛型 */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());}}/** * @param queryWrapper {@link LambdaQueryWrapper} * @param order{@link OrderBy} 排序 * @param 泛型 */public static void setOrder(LambdaQueryWrapper queryWrapper, OrderBy order) {setOrderOne(queryWrapper, order.getColumn(), order.isAsc());}/** * @param queryWrapper {@link LambdaQueryWrapper} * @param list {@link OrderBy} 排序 * @param 泛型 */public static void setOrder(LambdaQueryWrapper queryWrapper, List list) {for (OrderBy order : list) {setOrder(queryWrapper, order);}}/** * @param queryWrapper {@link LambdaQueryWrapper} * @param orderItem{@link OrderItem} 排序 * @param 泛型 */public static void setOrderItem(LambdaQueryWrapper queryWrapper, OrderItem orderItem) {setOrderOne(queryWrapper, orderItem.getColumn(), orderItem.isAsc());}/** * @param queryWrapper {@link LambdaQueryWrapper} * @param list {@link OrderItem} 排序 * @param 泛型 */public static void setOrderItem(LambdaQueryWrapper queryWrapper, List list) {for (OrderItem order : list) {setOrderItem(queryWrapper, order);}}}
定义OrderBy实体类
@Datapublic class OrderBy {/** * 字段名称 */@NotEmptyprivate String column;/** * 排序类型 升序:asc 降序:desc * 默认升序 */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;}/** * 获取 OrderItem * * @return */public OrderItem getOrderItem() {return getOrderItem(isAsc(), isToUnder());}/** * 获取 OrderItem * * @param isAsc 是否升序 * @return */public OrderItem getOrderItem(boolean isAsc) {return getOrderItem(isAsc, isToUnder());}/** * 获取 OrderItem * * @param isToUnder 是否进行驼峰转下划线 * @return */public OrderItem getOrderItemToUnder(boolean isToUnder) {return getOrderItem(isAsc(), isToUnder);}/** * 获取 OrderItem * * @param sort排序方式升序:asc 降序:desc * @param isToUnder 是否进行驼峰转下划线 * @return */public OrderItem getOrderItem(String sort, boolean isToUnder) {return getOrderItem(isAsc(), isToUnder);}/** * 获取 OrderItem * * @param isAsc 是否升序 * @param isToUnder 是否进行驼峰转下划线 * @return */public OrderItem getOrderItem(boolean isAsc, boolean isToUnder) {String col = isToUnder ? StrUtil.toUnderlineCase(column) : column;return OrderItemUtils.getOrder(col, isAsc);}/** * 获取sql * @return */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);}