코딩 관련/MyBatis
[MyBatis] 배열 파라미터 / list 사용하는 쿼리 / 반복문 쿼리
메리짱123
2020. 10. 20. 15:11
반응형
boardType 변수는 String[] 타입
String[] 타입을 map에 넣었다.
String[] boardType; //checkbox 선택지 가져올 배열 생성
boardType=request.getParameterValues("boardType"); //checkbox 선택지 가져오기
map.put("boardType", boardType);
이 map을 파라미터로 받아서 select 쿼리를 하려고 함
<select id="boardTypeList" parameterType="hashmap" resultMap="boardVo">
select
board_type
,board_num
,board_title
,board_comment
,total_cnt
from (select
board_num
,board_type
,board_title
,board_comment
,row_number() over(order by board_num desc) as numrow
,count(*) over() as total_cnt
from board
<if test="boardType!=null">
where
<foreach collection="boardType" item="arr" separator="or">
board_type= #{arr}
</foreach>
</if>
)
where NUMROW BETWEEN (#{pageVo.pageNo}-1)*10 + 1 AND (#{pageVo.pageNo})*10
</select>
<foreach collection = "boardType" item="arr" separator="or">
collection : 전달받은 인자. List or Array 형태만 가능
item : 전달받은 인자 값을 alias 명으로 대체
open : 구문이 시작될때 삽입할 문자열
close : 구문이 종료될때 삽입할 문자열
separator : 반복 되는 사이에 출력할 문자열
index : 반복되는 구문 번호이다. 0부터 순차적으로 증가
아래 블로그의 글을 참고하였음!
[MyBatis] 동적 쿼리 foreach문 문법 총 정리
시작하기에 앞서 참고 자료 *ibatis iterate문 지원 태그 property : 파라미터명 prepend : 쿼리로 쓰일 문자 open : 구문이 시작될때 삽입할 문자열 close : 구문이 종료될때 삽입할 문자열 conjunction :..
java119.tistory.com
반응형