Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

能否在QueryWrapper和LambdaQueryWrapper中也增加Page.addOrder那样直接传OrderItem的方法 #6082

Open
ltxiao opened this issue Apr 21, 2024 · 0 comments

Comments

@ltxiao
Copy link

ltxiao commented Apr 21, 2024

当前使用版本(必填,否则不予处理)

3.5.6

能否在QueryWrapper和LambdaQueryWrapper中也增加Page.addOrder那样直接传OrderItem的方法,
排序条件从前台传来是字符串,处理成OrderItem以后只有Page能直接用,LambdaQueryWrapper也不能直接传字符串字段名,
一个表的查询条件封装成一个对象后,在有的地方会用分页有的地方不需要分页,导致现在要写三种样子的排序处理

public Page<T> handlerPage() {
    Integer pageNum = ObjectUtils.defaultIfNull(this.pageNo, DEFAULT_PAGE_NUM);
    Integer pageSize = ObjectUtils.defaultIfNull(this.pageSize, DEFAULT_PAGE_SIZE);
    if (pageNum <= 0) {
        pageNum = DEFAULT_PAGE_NUM;
    }
    Page<T> page = new Page<>(pageNum, pageSize);
    // 这里拼接了排序条件 就不能在wrapper中再拼,会重复
    if (!sorted) {
        sorted = true;
        List<OrderItem> orderItems = handlerOrderItem();
        Opt.ofEmptyAble(orderItems).ifPresent(page::addOrder);
    }
    return page;
}
protected void handlerWrapperSort(QueryWrapper<T> queryWrapper) {
    if (!sorted) {
        sorted = true;
        List<OrderItem> orderItems = handlerOrderItem();
        Opt.ofEmptyAble(orderItems).ifPresent(items -> items.forEach(item -> {
                if (item.isAsc()) {
                    queryWrapper.orderByAsc(item.getColumn());
                } else {
                    queryWrapper.orderByDesc(item.getColumn());
                }
            })
        );
    }
}
protected void handlerWrapperSort(LambdaQueryWrapper<T> queryWrapper) {
    if (!sorted) {
        sorted = true;
        List<OrderItem> orderItems = handlerOrderItem();
        MergeSegments expression = queryWrapper.getExpression();
        Opt.ofEmptyAble(orderItems)
            .ifPresent(items ->
                items.forEach(item ->
                    expression.add(SqlKeyword.ORDER_BY, item::getColumn,
                        item.isAsc() ? SqlKeyword.ASC : SqlKeyword.DESC)
                )
            );
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants