java 根据时间筛选查询
根据时间筛选查询
文件配置
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') >= date_format(#{startTime},'%y%m%d')
-- and date_format(login_time,'%y%m%d') <= date_format(#{endTime},'%y%m%d')
-- and login_time >= #{startTime} and login_time <= #{endTime}, jdbcType = TIMESTAMP
<where>
<if test="startTime !=null">
and login_time >=#{startTime}
</if>
<if test="endTime != null">
and login_time <=#{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 >=to_date(#{startTime}, 'yyyy-MM-DD')
</if>
<if test="endTime != null">
and login_time <=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') >= date_format(#{startTime},'%y%m%d')
-- and date_format(login_time,'%y%m%d') <= date_format(#{endTime},'%y%m%d')
-- and login_time >= #{startTime} and login_time <= #{endTime}, jdbcType = TIMESTAMP
<where>
<if test="startTime !=null">
and login_time >=#{startTime}
</if>
<if test="endTime != null">
and login_time <=#{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 >=#{startTime}
</if>
<if test="endTime != null">
and login_time <=#{endTime}
</if>
</where>
</select>
或者
<!--
WHERE date_format(login_time,'%y%m%d') >= date_format(#{startTime},'%y%m%d')
and date_format(login_time,'%y%m%d') <= date_format(#{endTime},'%y%m%d')
and login_time >= #{startTime} and login_time <= #{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 >=#{startTime}
</if>
<if test="endTime != null">
and login_time <=#{endTime}
</if>
</where>
</select>
最终返回结果应和String类型返回结果类似。
String类型返回结果
String类型是接收String类型的数据之后,再从mybatis转换成Date 类型。
总结
此次根据时间筛选查询记录主要就是xml注释一定不要用快捷键,最终还是解决了问题,这下子用Date或者String类型接收都可以了。
在此记录自己的问题与大家分享。
我在CSDN上面的文章链接:https://blog.csdn.net/dwh19992018/article/details/124140137
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~