java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > LambdaQueryWrapper设置自定义排序

Java中LambdaQueryWrapper设置自定义排序代码示例

作者:程序猴老王

这篇文章主要给大家介绍了关于Java中LambdaQueryWrapper设置自定义排序的相关资料,lambdaquerywrapper是MyBatis-Plus框架中的一个查询条件构造器,它可以用于构建自定义的查询条件,需要的朋友可以参考下

MybatisPlus版本

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.4.2</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-extension</artifactId>
    <version>3.4.2</version>
</dependency>

定义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 <T>          泛型
     */
    public static <T> void setOrderOne(LambdaQueryWrapper<T> 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 <T>          泛型
     */
    public static <T> void setOrder(LambdaQueryWrapper<T> queryWrapper, OrderBy order) {
        setOrderOne(queryWrapper, order.getColumn(), order.isAsc());
    }

    /**
     * @param queryWrapper {@link LambdaQueryWrapper}
     * @param list         {@link OrderBy} 排序
     * @param <T>          泛型
     */
    public static <T> void setOrder(LambdaQueryWrapper<T> queryWrapper, List<OrderBy> list) {
        for (OrderBy order : list) {
            setOrder(queryWrapper, order);
        }
    }

    /**
     * @param queryWrapper {@link LambdaQueryWrapper}
     * @param orderItem    {@link OrderItem} 排序
     * @param <T>          泛型
     */
    public static <T> void setOrderItem(LambdaQueryWrapper<T> queryWrapper, OrderItem orderItem) {
        setOrderOne(queryWrapper, orderItem.getColumn(), orderItem.isAsc());
    }

    /**
     * @param queryWrapper {@link LambdaQueryWrapper}
     * @param list         {@link OrderItem} 排序
     * @param <T>          泛型
     */
    public static <T> void setOrderItem(LambdaQueryWrapper<T> queryWrapper, List<OrderItem> list) {
        for (OrderItem order : list) {
            setOrderItem(queryWrapper, order);
        }
    }
}

定义OrderBy实体类

@Data
public class OrderBy {

    /**
     * 字段名称
     */
    @NotEmpty
    private 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<OrderBy> getOrderBys() {
        String order = ServletUtils.getParameter(PageConstants.ORDER);
        if (ObjectUtil.isEmpty(order)) {
            return null;
        }
        List<OrderBy> 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<SysOperLog> getList(SysOperLog operLog) {
        LambdaQueryWrapper<SysOperLog> lambda = getLambda();
   
        List<OrderItem> list = getOrderItems();
        if (ObjectUtils.isNotEmpty(list)) {
            MybatisPlusUtil.setOrderItem(lambda, list);
        } else {
            lambda.orderByDesc(SysOperLog::getOperTime);
        }
        return super.list(lambda);
    }

总结 

到此这篇关于Java中LambdaQueryWrapper设置自定义排序的文章就介绍到这了,更多相关LambdaQueryWrapper设置自定义排序内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文