깜놀하는 해므찌로

MyBtis foreach / selectKey 활용 예시 본문

IT

MyBtis foreach / selectKey 활용 예시

agnusdei1207 2022. 7. 4. 16:19
반응형
SMALL

MyBtis 반복문 foreach / selectKey

<!-- 엑셀 업로드 시 기존 레코드 삭제 -->
<update id="deleteContents" parameterType="stcVO">
    /* Stc.deleteContents */
    <![CDATA[
        DELETE	
          FROM	t_stc  
    ]]>
</update>

1. 엑셀 업로드 시 중복 값을 제거하기 위해 일괄 삭제 처리

<insert id="excelInsertContents" parameterType="stcVO">
    <selectKey resultType="String" keyProperty="totalSeq" order="BEFORE">
        SELECT IFNULL(MAX(TS.SEQ)+1,1) FROM t_stc TS
    </selectKey>		
        INSERT INTO t_stc(
              SEQ
            , COM
            , AREA
            , ADDRESS
            , VISIT_DATE
        )
        VALUES  
        <foreach item="item" index="index" collection="excelList" separator=" , ">
            ( 
              #{totalSeq} + #{index}	
            , #{item.com}
            , #{item.area} 
            , #{item.address}
            , STR_TO_DATE(REGEXP_REPLACE(#{item.visitDate},'[^0-9]',''),'%Y%m%d')
            )
        </foreach>
</insert>

2. selectKey 활용하여 조회된 결과 값을 keyProperty 변수에 저장

3. foreach 활용

- item : 반복문 안에서 활용할 요소의 변수명

- collection : 반복할 리스트 혹은 배열 타입

- separator : 데이터를 구분할 구분자

 

 

반응형
LIST