MyBatis时间timestamp做条件进行查询

首先要将条件 转换为 时间戳

  1. long startTime = TimeUtil.parseTimestamp(start);
  2. long endTime = TimeUtil.parseTimestamp(end);
  3. /*对应工具类*/
  4. public static long parseTimestamp(String datetime){
  5. try{
  6. SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  7. Date date = dateformat.parse(datetime);
  8. return date.getTime()/1000;
  9. }catch(Exception e){
  10. e.printStackTrace();
  11. }
  12. return 0;
  13. }

然后Mapper.xml中  使用BETWEEN and  和 to_timestamp

  1. <if test="startDate !=null and startDate != and endDate !=null and endDate !=">
  2. AND tdnm.create_time BETWEEN to_timestamp(#{startDate}) AND to_timestamp(#{endDate})
  3. </if>

作者:terry,如若转载,请注明出处:https://www.web176.com/mybatis/12522.html

(0)
打赏 支付宝 支付宝 微信 微信
terryterry
上一篇 2023年3月7日
下一篇 2023年3月8日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注