当前位置:   article > 正文

mybatis-plus 指定列指定值排序_baomidou 单字段从小到大排序

baomidou 单字段从小到大排序

问题:由于使用mybatis-plus的查询功能,是通过封装querywrapper实现的,SQL可控性不高,有部分逻辑不好控制,例如排序。

方案:使用last方法补充尾部逻辑(https://baomidou.com/guide/wrapper.html#last

样例1:

通过更新时间和创建时间倒序排序

  1. QueryWrapper<Employee> queryWrapper = QueryGenerator.initQueryWrapper(employee, req.getParameterMap());
  2. queryWrapper.eq("ORG_ID",sysUser.getOrgCode());
  3. queryWrapper.last("order by nvl2(update_time, update_time, create_time) desc");

样例2:

通过字段状态和创建时间排序(状态为在职的员工置顶,且按更新时间升序排序)

  1. QueryWrapper<Employee> queryWrapper = QueryGenerator.initQueryWrapper(employee, req.getParameterMap());
  2. queryWrapper.last("order by case when staff_status = '1' then 1 else 2 end, update_date asc"); // staff_status 1是在职 2是离职

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/804934
推荐阅读
相关标签
  

闽ICP备14008679号