개발/JAVA

[Mybatis] SQL에서 WHERE 절 안에 in 처리하기

여름청춘 2023. 11. 14. 15:22

SQL in 조건이니 넘기는 변수는 List 

 

 

Service

  public List<Goods> findGoodsByIds(List<Long> goodsIds) {
    return lookupRepository.findGoodsByIds(goodsIds);
  }

 

 

 

Mybatis Mapper.xml

foreach 돌리기 !!

   <select id="findGoodsByIds"
            parameterType="java.lang.Long"
            resultType="com.musinsa.domain.stock.entity.Goods"
            resultMap="goodsResultMap">
        SELECT
            goods_number,
            goods_name,
            style_number,
            company_id,
            company_name,
            brand_id,
            brand_name
        FROM goods
        WHERE goods_number in
        <foreach collection="goodsIds" item="goodsId" open="(" close=")" separator=",">
            #{goodsId}
        </foreach>
        ;
    </select>