MyBatis List 파라미터 foreach 사용
1. Json
1
2
3
4
5
6
7
8
9
10
{
"compCd": "50",
"custCdList": [{
"custCd": "001"
}, {
"custCd": "002"
}, {
"custCd": "003"
}]
}
2. Controller 설정
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping(path = "/master")
public class CenResource {
private final InfoService infoService;
@PostMapping(path = "/info")
public ResponseEntity<Result> findInfo(@RequestBody InfoDTO infoDTO) {
Result result = new Result();
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(new MediaType("application", "json", Charset.forName("UTF-8")));
List<InfoDTO> rs = infoService.findInfo(infoDTO);
result.setStatus(StatusEnum.OK);
result.setData(rs);
result.setMessage("success");
return new ResponseEntity<>(result, httpHeaders, HttpStatus.OK);
}
}
3. DTO 설정
1
2
3
4
5
6
7
8
9
10
11
12
@Getter
@Setter
@NoArgsConstructor
public class InfoDTO {
private List<Info> custCdList;
private String compCd;
private String custCd;
private String custLoclNm;
private String custEngNm;
private String rprsvNm;
}
4. SQL 설정
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<select id="selectInfo" parameterType="com.example.info.InfoDTO" resultType="com.example.info.InfoDTO">
select tmch.cust_cd
, tmch.cust_locl_nm
, tmch.cust_eng_nm
, tmch.rprsv_nm
from tb_mm_cust_h tmch
inner join tb_mm_cust_m tmcm
on tmch.cust_cd = tmcm.cust_cd
<if test="@org.apache.commons.lang3.StringUtils@isNotBlank(compCd)">
and tmcm.comp_cd = #{compCd}
</if>
<if test="@org.apache.commons.collections4.CollectionUtils@isNotEmpty(custCdList)">
and tmcm.cust_cd in
<foreach collection="custCdList" item="item" index="index" separator="," open="(" close=")">
#{item.custCd}
</foreach>
</if>
</select>
[출처 및 참고]
This post is licensed under CC BY 4.0 by the author.