MybatisPlus的LambdaQueryWrapper多字段排序并设置不同排序方式

MybatisPlus版本

com.baomidou:mybatis-plus-extension:3.4.3.1

定义工具类PageUtilPlus


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.ISqlSegment;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.support.ColumnCache;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geniuses.common.entity.vo.PageResultVO;
import com.geniuses.common.entity.Pageable;
import com.geniuses.common.util.PageUtil;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

import static com.baomidou.mybatisplus.core.enums.SqlKeyword.*;

public class PageUtilPlus{

		
    private static Method tryInitCache;
    private static Method getColumnCache;
    private static Method appendSqlSegments;

    static {
        LambdaQueryWrapper<Object> queryWrapper = new LambdaQueryWrapper<>();
        try {
            Class<?> superclass = queryWrapper.getClass().getSuperclass();
            tryInitCache = superclass.getDeclaredMethod("tryInitCache", Class.class);
            getColumnCache = superclass.getDeclaredMethod("getColumnCache", String.class, Class.class);
            appendSqlSegments = superclass.getSuperclass().getDeclaredMethod("appendSqlSegments", ISqlSegment[].class);
            tryInitCache.setAccessible(true);
            getColumnCache.setAccessible(true);
            appendSqlSegments.setAccessible(true);


        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }

    /**
     * 设置排序字段,如果设置了排序字段,实体类中的orderBy注解将失效
     *
     * @param queryWrapper {@link LambdaQueryWrapper}
     * @param pageable     {@link Pageable}
     * @param clazz        排序的对象
     * @param           泛型
     */
    public static <T> void setOrderBy(LambdaQueryWrapper<T> queryWrapper, Pageable pageable, Class<?> clazz) {
        Map<String, String> sortMap = pageable.getSortMap();
        if (null == sortMap || sortMap.size() == 0) {
            return;
        }

        sortMap.forEach(
                (key, value) -> {
                    setOrderOne(queryWrapper,key,StrUtil.equalsIgnoreCase("desc",value),true,clazz);
                }
        );
    }   

    /**
     * @param queryWrapper {@link LambdaQueryWrapper}
     * @param column       排序字段
     * @param desc         降序
     * @param add          是否增加
     * @param clazz        排序的对象
     * @param           泛型
     */
    public static <T> void setOrderOne(LambdaQueryWrapper<T> queryWrapper, String column, boolean desc, boolean add, Class<?> clazz) {
        try {
            tryInitCache.invoke(queryWrapper, clazz);
            ColumnCache columnCache = (ColumnCache) getColumnCache.invoke(queryWrapper, column, clazz);
            if (columnCache == null) {
                throw new RuntimeException("数据库中找不到 " + column + " 映射字段");
            }
            String sqlColumn = columnCache.getColumn();

            ISqlSegment[] sqlSegments = {ORDER_BY, () -> sqlColumn, desc ? DESC : ASC};
            appendSqlSegments.invoke(queryWrapper, (Object) sqlSegments);
        } catch (IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

你可能感兴趣的:(spring,boot,docker,postgresql,数据库)