๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Lang/โ˜• Java

โ˜• Java * [Mybatis] foreach ๊ตฌ๋ฌธ์—์„œ List<Map<>> ์‚ฌ์šฉ๋ฒ•

ํด๋ผ์ด์–ธํŠธ์—์„œ JSON ํ˜•์‹์œผ๋กœ ํ”ผ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋‚ ์•„์™”์„ ๋•Œ ๊ตฌ์„ฑ์ด List<Map<>> ํ˜•์‹์ธ ๊ฒฝ์šฐ

์ž๋ฐ”์—์„œ ์‚ฌ์šฉํ•˜๋“ฏ์ด ๊ตฌ์„ฑ์„ ํ•˜๋ฉด ๋œ๋‹ค.

 

ํ˜น์‹œ๋‚˜ ์‹ถ์–ด ํด๋ผ์ด์–ธํŠธ์—์„œ ๋„˜๊ฒจ์ฃผ๋Š” JSON ๊ณผ ์„œ๋ฒ„์˜ Controller ๋ถ€๋ถ„๋„ ํ•จ๊ป˜ ์ž‘์„ฑํ•ด ๋†“์•˜๋‹ค!

ํ•ด๋‹น ๋ถ€๋ถ„์ด ํ•„์š” ์—†๋Š” ์‚ฌ๋žŒ์€ 3๋ฒˆ XML์—์„œ! ๋ถ€๋ถ„์„ ์ฐธ๊ณ ํ•˜์ž!

 

 

ํ•ด๋ณด์ž

1. JSON ๊ตฌ์„ฑ๋ฐฉ๋ฒ•

"body": {
    "list": [
        {
            "ํ•ญ๋ชฉ": "๊ฐ’"
        }
    ]
}

2. Controller ์—์„œ ๋ฐ›์„ ๋•Œ

Map<String, Obejct> reqBody = (Map<String, Object>) ~~~~
๋”๋ณด๊ธฐ

XML์€ ์•„๋ž˜ ๊ทธ๋Œ€๋กœ ์ง„ํ–‰ํ•˜๊ณ  JSON ๊ณผ Controller ๋ถ€๋ถ„๋งŒ ์ˆ˜์ •ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

1. JSON

"body": [
    {
        "ํ•ญ๋ชฉ": "๊ฐ’"
    }
]

  

2. Controller

List<Map<String, Obejct>> reqBody = (List<Map<String, Object>>) ~~~~

3. XML ์—์„œ! (= MyBatis ๊ตฌ์„ฑ)

<foreach collection="๋ณ€์ˆ˜์ด๋ฆ„" item="์•„์ดํ…œ์ด๋ฆ„">
	#{์•„์ดํ…œ์ด๋ฆ„.Mapํ•ญ๋ชฉ์ด๋ฆ„}
</foreach>

์‹คํ–‰ ์˜ˆ์‹œ ์ถ”๊ฐ€ํ•˜๊ธฐ

collection์— Type์„ ์ง€์ •ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค๊ณ  ํ•˜๋Š”๋ฐ, ๋ณ€์ˆ˜๋ฅผ ๋„ฃ์œผ๋ฉด ํƒ€์ž…์„ ์•Œ์•„์„œ ์žก๋Š” ๊ฒƒ ๊ฐ™๋‹ค.

(์ •ํ™•ํ•œ ๋‚ด์šฉ์€ ์ธํ„ฐ๋„ท์„ ์ข€ ๋” ์‚ดํŽด๋ณด๋„๋ก ํ•˜์ž)

 

 

๋ช‡ ๊ฐ€์ง€ ์˜ต์…˜๋“ค์— ๋Œ€ํ•ด์„œ ๋” ์•Œ์•„๋ณด์ž

open ์ธ์ž๋กœ ์‹œ์ž‘ํ•˜์—ฌ close ๋กœ ๋งˆ๊ฐํ•˜๊ณ  separator ๋กœ ๊ฐ ํ•ญ๋ชฉ์„ ๊ตฌ๋ถ„ํ•œ๋‹ค.

<foreach collection="๋ณ€์ˆ˜์ด๋ฆ„" item="์•„์ดํ…œ์ด๋ฆ„" open="(" close=")" separator=",">
	#{๋ณ€์ˆ˜์ด๋ฆ„.Mapํ•ญ๋ชฉ์ด๋ฆ„}
</foreach>

์‹คํ–‰ ์˜ˆ์‹œ ์ถ”๊ฐ€ํ•˜๊ธฐ

 

 

 


Thanks for ๐Ÿ™ˆ