根据时间筛选查询

文件配置

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类型接收都可以了。
在此记录自己的问题与大家分享。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐