Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MyBatis Timestamp does conditions for queries


May 16, 2021 MyBatis



The first step is to convert the condition to a 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. }

Then Mapper uses .xml and in the 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>