0

When using mybatis I have a question about reading data which is selected into one given time interval. In the xml file, I write like:

<select id="getRecord" parameterType="java.util.Map" resultType="com.domain.record">
    select METHOD method, TIME_STAMP timeStamp 
    from RECORD 
    where TIME_STAMP BETWEEN #{startTime} AND #{endTime}
</select>     

in another file, I write like this:

public List<Record> getRecord(String starttime, String endtime) {
    Map<String, String> map = new LinkedHashMap<String, String>();
    String startTime= "\'"+starttime + "\'";
    String endTime= "\'"+endtime + "\'";
    map.put("startTime",startTime);
    map.put("endTime", endTime);
    List<APIServerRecord> listRes=apiServerRecordMapper.getAPIServerRecord(map);
    return listRes;
}

It does not work. The size of listRes is zero and I'sure that there are many records and it should not be zero. And I'm sure that other sql statements in xml file is work. Meanwhile, if I give the time directly in xml file like the following code, it works:

 <select id="getRecord" parameterType="java.util.Map" resultType="com.domain.record">
      select METHOD method, TIME_STAMP timeStamp 
      from RECORD 
      where TIME_STAMP BETWEEN '2013-06-18 12:11' AND '2013-06-18 12:16'
</select>

Could you tell me how to do it?

4

1 回答 1

0

It is recommended to convert to the data type that MyBatis handled correctly (the handlers classes extend of org.apache.ibatis.type.TypeHandler).

If the format of the string is the same, you can try:

private static DateFormat dateFormat =new SimpleDateFormat("yyyy-MM-dd HH:mm");

public static Timestamp convert(String str) {
    try {
        Date date = dateFormat.parse(str);
        return new Timestamp(date.getTime());
    } catch (ParseException e) {
        throw new RuntimeException(e);
    }
}

public List<Record> getRecord(String starttime, String endtime) {
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    map.put("startTime", convert(startTime));
    map.put("endTime", convert(endTime));
    List<APIServerRecord> listRes=apiServerRecordMapper.getAPIServerRecord(map);
    return listRes;
}
于 2013-06-18T21:47:57.767 回答