jsp 에서
var cList = new Array();
cList.push("5");
cList.push("6");
해서 값을 넘기고 mybatis에서 쓸려고 했는데
<if test="cList != null"> AND AA in
<foreach collection="cList" item="C_MM" index="index" separator="," open="(" close=")"> #{C_MM,jdbcType=VARCHAR} </foreach>
</if>
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.builder.BuilderException: Error evaluating expression
'cList'. Return value (["5","6"]) was not iterable.
이런 에러가 났다.
흠.. 구글링 해보니 mybatis에서 foreach를 사용하려면 리스트같이 순회할 수 있는 데이터 형이어야 한다고 한다.
단순 String으로 map에 넣으면 쿼리 실행할 때 Return value was not iterable. 오류가 난다고 한다.
그래서
public static List<String> getStringListValue(Object obj) throws Exception {
if(obj == null){
return null;
}
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue((String) obj, List.class);
}
위와 같은 메소드를 만들고
cList = CommonUtil.getStringListValue(requestParam.get("cList"));
sMap.put("cList", cList);
이런식으로 했더니 되었다.
즉,
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue((String) obj, List.class);
이런식으로 자바스크립트 배열 스트링을 자바 리스트로 변경하였다.
뭐가 다른지 찍어보니
cList={["5","6"]}
cList={[5, 6]}
이렇게 스트링 형식은 ["5","6"] 이렇게 되어 있고
리스트 형식은 [5, 6] 이렇게 되어 있다.