解决 mybatis给日期时间赋NULL值

需求:将mysql中类型为timestamp的日期时间赋值为空,代表没有启动过。

mybatis:

<update id="update">
    UPDATE dev
        <set>
            <if test="devNo != null">`dev_no` = #{devNo}, </if>
            
            <!-- 如果有传入有时间,则正常赋值 -->
            <if test="startDate != null and startDate != ''">`start_date` = #{startDate}, </if>
            <!-- 如果传入时间为空,则赋NULL值 -->
            <if test="startDate != null and startDate == ''">`start_date` = NULL, </if>
        </set>
    WHERE
        id = #{id}
</update>

前端页面用的laydate,有个清空按钮,作用清空当前日期框的时间,点击保存,会把  '' (双单引号)值传给mybatis,引起报错。在mybatis中使用

<if test="startDate != null and startDate == ''">`start_date` = NULL </if>

给日期时间赋空值

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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值