Mybatis 时间排序让空值NULL排在后面

有一张通知(notification)表;

需求:

1.状态(status)为发布“on”要排在未发布“off”前面;

2.先按发布时间排序再按创建时间排序,发布时间为空的要排在后面。

select * from notification;
idcontentstatuscreate_timepubliish_time
1aaaon2018-10-17 12:45:002018-10-17 12:45:00
2bbboff2018-10-17 12:45:00 
3cccon2018-10-17 12:45:002018-10-17 12:45:00
4dddon2018-10-17 12:45:002018-10-17 12:45:00
5eeeoff2018-10-17 12:45:00 

 

 

 

 

 

 

 

<select id="getResultList" parameterType="com.hekliu.NotificationQry" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM notification
        WHERE
        1 = 1
        <if test="content != null and content !=''">
            AND content = #{content}
        </if>
        <if test="status != null and status != ''">
            AND status = #{status}
        </if>
        <if test="startPublishDate != null and startPublishDate != ''">
            AND  <![CDATA[DATE_FORMAT(publish_time,'%Y-%m-%d %H:%i:%s') >= #{startPublishDate}]]>
        </if>
        <if test="endPublishDate != null and endPublishDate != ''">
            AND <![CDATA[DATE_FORMAT(publish_time,'%Y-%m-%d %H:%i:%s') <= #{endPublishDate}]]>
        </if>
        ORDER BY
        status = "off", status = "on", create_time DESC, publish_time is NULL, publish_time DESC
    </select>

原文链接:https://blog.csdn.net/liu59412/article/details/83141566

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyBatis-Plus 中,你可以使用 PageHelper 插件来实现分页排序和将 null 值排最后的功能。 首先,确保你已经正确地配置了 MyBatis-Plus 和 PageHelper 插件。 然后,在你的查询方法中,使用 PageHelper.startPage 方法来启动分页功能,并使用 PageHelper.orderBy 方法来指定排序字段和排序方式。例如: ```java import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.PageHelper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired private UserMapper userMapper; public IPage<User> getUsersWithPaginationAndSort() { // 使用 PageHelper.startPage 方法开启分页功能 Page<User> page = PageHelper.startPage(1, 10); // 使用 PageHelper.orderBy 方法指定排序字段和排序方式 PageHelper.orderBy("name ASC NULLS LAST"); // 执行查询 List<User> userList = userMapper.selectList(null); // 将查询结果封装到 Page 对象中 return page.setRecords(userList); } } ``` 上述代码示例中,我们使用了 `name ASC NULLS LAST` 来指定按照 name 字段升序排序,并且将 null 值排最后。 请根据你的实际需求调整排序字段和排序方式。注意,在不同的数据库中,对于 null 值的处理方式可能会有所不同,可以根据实际情况来调整排序语句。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hekliu

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值