mybatis-mapper / mapper

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

orderBy 方法期望能添加支持传入字符串以满足一些变态的业务需求 #33

Closed ydq closed 2 years ago

ydq commented 2 years ago

rt : orderBy 方法期望能添加支持传入字符串,以满足一些变态的业务需求(example/wrapper)

非现有的 example.setOrderByClause,因为 setOrderByClause 会覆盖整个已有的排序

有的时候我们可能需要满足一些“变态”的业务需求:

-- 场景1:mysql 按照指定的字段指定的入参顺序来排序
SELECT * 
FROM table 
WHERE /** some condition **/   
ORDER BY 
    /** other sort **/   
    field(`weight` , 5 , 3 , 1 , 2 , 4),  -- 根据传入的 weight 字段的指定顺序来排序
    /** other sort **/   

-- 场景2:如  要求把身份证号不为空的排前面,相同状态下 status = 2 提到前面,其他的按照其他规则排序
SELECT * 
FROM table 
WHERE /** some  condition **/  
ORDER BY 
    /** other sort **/   
    id_card_no IS NOT NULL DESC, -- 指定字段不为空的在前面
    status = 2 DESC ,     -- 指定值的排在前面
    /** other sort **/   

如果使用 example 的 setOrderByClause 会将前面使用 orderBy 设置的排序条件给统一覆盖掉,遇到这种情况现在的做法就是必须 orderBy 条件全部手写,所以期望添加 支持传入自定义字符串排序的方法

不知道是否有说服力

ydq commented 2 years ago

已提了PR