mybatis-mapper / mapper

MyBatis Mapper
https://mapper.mybatis.io
Apache License 2.0
325 stars 47 forks source link

example / wrapper 新增字符串形式的排序方法重载 #34

Closed ydq closed 2 years ago

ydq commented 2 years ago

https://github.com/mybatis-mapper/mapper/issues/33

使用示例:


// ORDER BY status = 5 DESC , FIELD(`weight`, 3, 1, 4, 2) , create_time DESC
userMapper.wrapper()
                    // 将 状态 等于5的 排序在最前面
                    .orderBy("status = 5 DESC")    
                    // 如果权重不为空 则将指定权重的值放前面
                    .orderBy(weight != null , () -> "`weight` = " + weight + " DESC")    
                    // 如果指定权重列表不为空,则按照指定的权重特顺序进行排序
                    .orderBy(CollectionUtils.isNotEmpty(assignWeightSort),() -> {
                        return assignWeightSort.stream()
                                        .map(Objects::toString)
                                        .collect(Collectors.joining(",", "FIELD(`weight`,", ")"));
                    })
                    // 相同权重的 按照时间倒序
                    .orderBy("create_time DESC");  //  等同于 .orderByDesc(User::getCreateTime);
abel533 commented 2 years ago

感谢PR :+1: