ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [MyBatis] foreach 문 정리 (List, Array)
    리뷰노트/MyBatis 2022. 8. 12. 17:40

    [MyBatis] foreach 문 정리

    < foreach 태그 구성 > 

    collection은 꼭! 넘겨주는 배열 변수명과 동일하게 작성해야 합니다.

    collection : 전달받은 인자. List or Array 만 전달 받을 수 있음

    item : 전달받은 인자 값을 alias 명으로 대체

    open : 구문이 시작될때 삽입할 문자열

    close : 구문이 종료될때 삽입할 문자열

    separator : 반복 되는 사이에 출력할 문자열

    index : 반복되는 구문 번호이다. 0부터 순차적으로 증가




    < 문법 >

     <foreach collection="List or Array" item="alias" ></foreach>

     

    - 배열 예시

    ex) int[] intArray = {1,2,3,4};


    - 리스트 예시

    ex) List<String> listA = new ArrayList<String>(); 
    or List<Sample> chkList = sampleService.getSampleList(); //SELECT * FROM sample 결과값


    - 사용 예시

    <select id="selectId" resultType="com.project.dto">
      SELECT *
      FROM tableSomething
      WHERE ID in
      <foreach item="item" index="index" collection="list"
          open="(" separator="," close=")">
            #{item}
      </foreach>
    </select>

     



    < Array 배열을 직접 보낼 때 >

    public List<Sample> getSampleList(int[] intArray) {
          return sqlSession.selectList("getSampleList", intArray);
    }


    ⬇️⬇️⬇️  **collection은 꼭! "array"로 작성해야 합니다.

    <select id="getSampleList"  resultType="sample">
       SELECT name FROM sample
       WHERE id IN
       <foreach collection="array" item="arr" open="(" close=")" separator=",">
          #{arr}
       </foreach>
       ORDER BY something;
    </select>

     



    < Array 배열을 Map에 넣어서 보냈을 때 >

    public List<Sample> getSampleList(int[] intArray) {
          HashMap<String, Object> map = new HashMap<String, Object>();
          map.put("intArray",intArray);
          return sqlSession.selectList("getSampleList", map);
    }


    ⬇️⬇️⬇️ collection은 꼭! 넘겨주는 배열 변수 값과 동일하게 작성해야 합니다.

    <select id="getSampleList"  resultType="sample">
       SELECT name FROM sample
       WHERE id IN
       <foreach collection="intArray" item="item" open="(" close=")" separator=",">
          #{item}
       </foreach>
       ORDER BY something;
    </select>

     

     



    < List 리스트를 직접 보낼 때 >

    public List<Sample> getSampleList(List<Sample> sampleList) {
          return sqlSession.selectList("getSampleList", sampleList);
    }


    ⬇️⬇️⬇️ collection을 꼭! "list"로 작성 후 List 리스트 안에 원하는 데이터를 {key.value} 형태로 작성하시면 됩니다.

    <select id="getSampleList"  resultType="sample">
       SELECT name FROM sample
       WHERE id IN
       <foreach collection="list" item="item" open="(" close=")" separator=",">
          #{item.name}
       </foreach>
       ORDER BY something;
    </select>

     

     



     < List 리스트를 Map에 넣어서 보낼 때 >

    public List<Sample> getSampleList(List<Sample> sampleList) {
          HashMap<String, Object> map = new HashMap<String, Object>();
          map.put("sampleList", sampleList);
          return sqlSession.selectList("getSampleList", map);
    }


    ⬇️⬇️⬇️ collection을 꼭! 넘겨주는 리스트 변수명과 똑같이 작성 후 List 리스트 안에 원하는 데이터를 {key.value} 형태로 작성하시면 됩니다.

    <select id="getSampleList"  resultType="sample">
       SELECT name FROM sample
       WHERE id IN
       <foreach collection="sampleList" item="item" open="(" close=")" separator=",">
          #{item.name}
       </foreach>
       ORDER BY something;
    </select>
Designed by Tistory.