3 个回答

今天、昨天、最近7天等等,后端接口统一接收startTime和endTime,让前端传参数就好了

首先保证项目mysql及服务器mysql时区配置正确,不要出现传时间到数据库+8 -8h情况

image.png
举个例子,跟前端约定好传入的搜寻开始结束时间格式:yyyy-MM-dd 我这边比较灵活 都不传就是全部时间 只传from就是传入from时间以后

if (!StringUtils.isEmpty(from)) {
            begin = cn.hutool.core.date.DateUtil.beginOfDay(cn.hutool.core.date.DateUtil.parse(from, "yyyy-MM-dd"));}
if (!StringUtils.isEmpty(to)) {
            end = cn.hutool.core.date.DateUtil.endOfDay(cn.hutool.core.date.DateUtil.parse(to, "yyyy-MM-dd"));}

我这里用hutool的包处理传入的字符串时间起止时间,不方便导第三方包就自己写个dateUtil处理
拿from的00:00:00 to的23:59:59
mapper.java

@Param("from") Date begin,@Param("to") Date end

mapper.xml

<if test="from!= null">
    AND a.create_time &gt; #{from}
</if>
<if test="to!= null">
    AND a.create_time &lt; #{to}
</if>

希望能帮到你

如果想省事就在接口入参直接接受date型,不过需要前端处理好格式

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题