메뉴 건너뛰기

Cloudera, BigData, Semantic IoT, Hadoop, NoSQL

Cloudera CDH/CDP 및 Hadoop EcoSystem, Semantic IoT등의 개발/운영 기술을 정리합니다. gooper@gooper.com로 문의 주세요.


Mybatis foreach 문법

 

<foreach collection="param.place" item="place"   open="(" close=")" separator="or">
      sh.SNA_SHOW_KIND_IDX = ${place}
</foreach>
collection = 전달받은 인자값
item   = 전달받은 인자값을 다른이름으로 대체
open 해당 구문이 시작할떄 (
close 해당구문이 끝날떄
separator  한번 이상 반복할때 반복되는 사이에  해당 문을 넣어줌
 
note: "collection"파라미터 객체로 MyBatis 에 List 인스턴스나 배열을 전달 할 수 있다. 그렇게 하면 MyBatis는 Map으로 자동으로 감싸고 이름을 키로 사용한다. List 인스턴스는 'LIST' 를 키로 사용하고, 배열 인스턴스는 'array'를 키로 사용한다.
<select id="..." parameterType="..." resultType="Map">
    select * from test
    <where>
        name in
        <foreach collection="list" item="item" index="index" separator="," open="(" close=")">
            #{item.value}
        </foreach>
    </where>
</select>

mybatis user guide를 보면 foreach의 경우 list나 array 타입을 collection으로 설정할 수 있으며 이때 list나 array 데이터는 map으로 타입이 변환되어 저장된다고 함

list를 사용할 경우 (변수 이름은 list가 아니어도 됨)


List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");

 

<!-- collection 속성 값은 반드시 list나 array만 설정해야 함 -->
<foreach collection="list" item="item" index="index" separator="," open="(" close=")">
    #{item.value}
</foreach>

value 속성을 사용한 것으로 보아 Map.Entry.getValue 메서드를 호출하는 것이 아닐까 하는데...


array를 사용할 경우(변수 이름은 array가 아니어도 됨)

String[] array = new String[] { "a", "b", "c" };

<!-- collection 속성 값은 반드시 list나 array만 설정해야 함 -->
<foreach collection="array" item="item" index="index" separator="," open="(" close=")">
    ${array[index]}
</foreach>
 

select의 parameterType이 map이나 java 오브젝트라면 collection 값에 속성 이름을 설정하면 됨

parameterType="Map"인경우


Map<String, Object> map = new HashMap<String, Object>();
map.put("friendList', list);

<select id="..." parameterType="Map" ...>
    <foreach collection="friendList" .../>
</select>

 

parameterType="{Java object}"인 경우


SomeJavaClass pojo = new SomeJavaClass();
pojo.setEnemyList(list);

<select id="..." parameterType="SomeJavaClass" ...>
    <foreach collection="enemyList" .../>
</select>

 

번호 제목 날짜 조회 수
» Mybatis foreach 문법정리(상황에 따른 사용법) 2015.11.10 2624
146 DB별 JDBC 드라이버 2015.10.02 2026
145 root계정으로 MariaDB설치후 mysql -u root -p로 db에 접근하여 바로 해줘야 하는일..(케릭터셑은 utf8) 2015.10.02 2010
144 SQL문장과 Mongo에서 사용하는 명령어를 비교한 것입니다. 2015.09.30 1822
143 mongodb 2.6.6 설치(64bit) 2015.09.30 1239
142 pom.xml에서 build.gradle로 변환 2015.09.14 1378
141 부팅을 외장하드에서 하도록 변경하는 방법 2015.07.28 1669
140 바나나 파이의 /tmp폴더를 외장하드로 변경하기 2015.07.24 1360
139 Tracking URL = N/A 가발생하는 경우 - 환경설정값을 잘못설정하는 경우에 발생함 2015.06.17 2040
138 java.lang.RuntimeException: org.apache.hadoop.hive.ql.metadata.HiveException: Hive Runtime Error: Unable to deserialize reduce input key from...오류해결방법 2015.06.16 3040
137 hortonworks에서 제공하는 메모리 설정값 계산기 사용법 file 2015.06.14 1938
136 Exception in thread "main" java.lang.NoSuchMethodError: org.apache.hadoop.http.HttpConfig.getSchemePrefix()Ljava/lang/String; 해결->실패 2015.06.14 1239
135 hadoop 2.6.0에 sqoop2 (1.99.5) server및 client설치 == fail 2015.06.11 2771
134 "File /user/hadoop/share/lib does not exist" 오류 해결방법 2015.06.07 2201
133 Error: E0501 : E0501: Could not perform authorization operation, User: hadoop is not allowed to impersonate hadoop 해결하는 방법 2015.06.07 2160
132 Error: Could not find or load main class nodemnager 가 발생할때 해결하는 방법 2015.06.05 2376
131 센서테스트 file 2015.05.25 1141
130 apk 파일 위치 file 2015.05.25 2983
129 HAX is not working and emulator runs in emulation mode 메세지가 나오는 경우 file 2015.05.25 1000
128 hbase shell 필드 검색 방법 2015.05.24 3416
위로