2013-06-18 2 views
0

mybatis를 사용할 때 하나의 주어진 시간 간격으로 선택된 데이터를 읽는 것에 대한 질문이 있습니다. 다른 파일에SQL 매퍼에서 datetime 형식의 값을 할당하는 방법

<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>  

, I는 다음과 같이 쓰기 :

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; 
} 

그것은 작동하지 않습니다 xml 파일에서, 내가 좋아하는 물품. listRes의 크기는 0이며 많은 레코드가 있으므로 0이 아니어야합니다. 그리고 나는 xml 파일의 다른 sql 문이 작동한다고 확신한다. 한편, 다음 코드와 같이 xml 파일에서 직접 시간을 지정하면 작동합니다.

<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> 

수행 방법을 알려주시겠습니까?

답변

0

MyBatis가 올바르게 처리 한 데이터 유형 (핸들러 클래스는 org.apache.ibatis.type.TypeHandler)으로 변환하는 것이 좋습니다. 문자열의 형식이 동일한 경우

, 당신은 시도 할 수 있습니다 :

는 는
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; 
} 
+0

정말 감사합니다, 그것은 작동 – user2256235

관련 문제