MyBtis foreach / selectKey

agnusdei·2023년 7월 5일
0
<!-- 엑셀 업로드 시 기존 레코드 삭제 -->
<update id="deleteContents" parameterType="stcVO">
    /* Stc.deleteContents */
    <![CDATA[
        DELETE	
          FROM	t_stc  
    ]]>
</update>

<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>
  1. selectKey 활용하여 조회된 결과 값을 keyProperty 변수에 저장

  2. foreach

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

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

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

0개의 댓글