springboot 查询 mysql date 字段的正确姿势
在 springboot 中查询 mysql 的 date 字段时,常会遇到日期类型不匹配的问题,导致查询结果不正确。为了解决这个问题,我们通常采用以下做法:
在前端将日期格式化为 "yyyy-mm-dd" 字符串,通过 post 请求提交。
@datetimeformat(pattern = "yyyy-mm-dd") private string lessondate;
在后端使用 string 类型接收日期参数。
@requestparam(value = "lessondate") string lessondate;
在 mybatis-plus 查询时,使用 ${} 连接符将字符串参数转换为 date 类型。
<select id="selectByDate" resultType="java.util.Map"> SELECT * FROM table_name WHERE lessonDate = ${lessonDate} </select>
通过这种方式,后端接收到的日期参数始终为字符串类型,在查询时再转换为 date 类型,与 mysql 的 date 字段保持一致。
以上就是SpringBoot 中查询 MySQL Date 字段的正确姿势是什么?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号