ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java 根据时间筛选查询

2022-04-14 08:34:50  阅读:307  来源: 互联网

标签:java String 查询 startTime time Date 筛选 endTime login


目录

根据时间筛选查询

文件配置

Controller层

//Controller层接收数据
//Date类型
@RequestParam(value = "startTime", required = false) Date startTime,
@RequestParam(value = "endTime", required = false) Date endTime

//String 类型
@RequestParam(value = "startTime", required = false) String startTime,
@RequestParam(value = "endTime", required = false) String endTime

//调用service方法
List<EmsLoginLog> emsLoginLogList = emsLoginLogService.findAllLogByStrTime(startTime,endTime);

Dao层/Service层

//根据Date类型的时间筛选查询
List<EmsLoginLog> findAllLogByTime(
            @Param("startTime") Date startTime,
            @Param("endTime") Date endTime
    );
//根据String类型的时间筛选查询
    List<EmsLoginLog> findAllLogByStrTime(
            @Param("startTime") String startTime,
            @Param("endTime") String endTime
    );

服务实现层

//根据Date类型的时间筛选查询
    @Override
    public List<EmsLoginLog> findAllLogByTime(Date startTime, Date endTime) {
        return emsLoginLogDao.findAllLogByTime(startTime,endTime);
    }
//根据String类型的时间筛选查询
    @Override
    public List<EmsLoginLog> findAllLogByStrTime(String startTime, String endTime) {
        return emsLoginLogDao.findAllLogByStrTime(startTime,endTime);
    }

Dao.xml文件

<!-- 根据Date类型的时间筛选查询-->
<select id="findAllLogByTime" resultMap="EmsLoginLogMap">
        select info_id, user_name, ipaddr, status, msg, login_time from ems_logininfor
--         WHERE date_format(login_time,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
--           and date_format(login_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
-- and login_time &gt;= #{startTime} and login_time &lt;= #{endTime}, jdbcType = TIMESTAMP
        <where>
            <if test="startTime !=null">
                and login_time &gt;=#{startTime}
            </if>
            <if test="endTime != null">
                and login_time &lt;=#{endTime}
            </if>
        </where>
    </select>
<!-- 根据String类型的时间筛选查询-->
    <select id="findAllLogByStrTime" resultMap="EmsLoginLogMap">
        select info_id, user_name, ipaddr, status, msg, login_time from ems_logininfor
        <where>
            <if test="startTime !=null">
                and login_time &gt;=to_date(#{startTime}, 'yyyy-MM-DD')
            </if>
            <if test="endTime != null">
                and login_time &lt;=to_date(#{endTime}, 'yyyy-MM-DD')
            </if>
        </where>
    </select>

前端访问

http://localhost:8181/treps/a/ems/findAllLog?startTime=2022-04-11&endTime=2022-04-12

Date类型返回结果

采用Date类型接收数据如下:
在这里插入图片描述

mybatis错误如下:
随后就会报错

在这里插入图片描述

解决方案:

将xml文件中使用idea的注释快捷键的ctrl+/部分删除或者选择使用xml的注释

<!-- 根据Date类型的时间筛选查询-->
<select id="findAllLogByTime" resultMap="EmsLoginLogMap">
        select info_id, user_name, ipaddr, status, msg, login_time from ems_logininfor
<!--
将以下字段更换为xml专用注释或者删除,问题解决。-->
--         WHERE date_format(login_time,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
--           and date_format(login_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
-- and login_time &gt;= #{startTime} and login_time &lt;= #{endTime}, jdbcType = TIMESTAMP
        <where>
            <if test="startTime !=null">
                and login_time &gt;=#{startTime}
            </if>
            <if test="endTime != null">
                and login_time &lt;=#{endTime}
            </if>
        </where>
    </select>

结果应为:

<select id="findAllLogByTime" resultMap="EmsLoginLogMap">
        select info_id, user_name, ipaddr, status, msg, login_time from ems_logininfor
        <where>
            <if test="startTime !=null">
                and login_time &gt;=#{startTime}
            </if>
            <if test="endTime != null">
                and login_time &lt;=#{endTime}
            </if>
        </where>
    </select>

或者

<!--
        WHERE date_format(login_time,'%y%m%d') &gt;= date_format(#{startTime},'%y%m%d')
         and date_format(login_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
 and login_time &gt;= #{startTime} and login_time &lt;= #{endTime}, jdbcType = TIMESTAMP
-->
    <select id="findAllLogByTime" resultMap="EmsLoginLogMap">
        select info_id, user_name, ipaddr, status, msg, login_time from ems_logininfor
        <where>
            <if test="startTime !=null">
                and login_time &gt;=#{startTime}
            </if>
            <if test="endTime != null">
                and login_time &lt;=#{endTime}
            </if>
        </where>
    </select>

最终返回结果应和String类型返回结果类似。

String类型返回结果

在这里插入图片描述
String类型是接收String类型的数据之后,再从mybatis转换成Date 类型。

总结

此次根据时间筛选查询记录主要就是xml注释一定不要用快捷键,最终还是解决了问题,这下子用Date或者String类型接收都可以了。
在此记录自己的问题与大家分享。

我在CSDN上面的文章链接:https://blog.csdn.net/dwh19992018/article/details/124140137

标签:java,String,查询,startTime,time,Date,筛选,endTime,login
来源: https://www.cnblogs.com/LuoXia-youyu/p/16142967.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有