java 根据时间筛选查询
2022/4/14 9:12:35
本文主要是介绍java 根据时间筛选查询,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
目录- 根据时间筛选查询
- 文件配置
- Controller层
- Dao层/Service层
- 服务实现层
- Dao.xml文件
- 前端访问
- Date类型返回结果
- 解决方案:
- String类型返回结果
- 文件配置
- 总结
根据时间筛选查询
文件配置
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
这篇关于java 根据时间筛选查询的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?