ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • spring boot
    [Spring boot] mybatis join ํ…Œ์ด๋ธ” jsp ์ถœ๋ ฅํ•˜๊ธฐ
    2023. 10. 21. 18:39
    728x90

    ๐Ÿ“Œ ํ™˜๊ฒฝ

    - framework : spring boot

    - Language : java 11

    - database : oracle 12c

     

    ๊ทธ๋™์•ˆ ์ทจ์ค€ํ•˜๋ฉด์„œ ๊ฒŒ์‹œํŒ์„ ๋ช‡ ๋ฒˆ ๋งŒ๋“ค์–ด๋ดค์ง€๋งŒ joinํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฟŒ๋ฆด์ผ์ด ์—†์—ˆ๊ธฐ์— ํฐ ๋ฌธ์ œ๊ฐ€ ์—†์—ˆ๋Š”๋ฐ

    ๋ง‰์ƒ ํšŒ์‚ฌ์— ๋“ค์–ด์™€์„œ ๋ณด๋‹ˆ join ํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฟŒ๋ ค์ค˜์•ผ ํ•˜๋Š” ์ƒํ™ฉ์— ์ง๋ฉดํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ... ๋‘๋‘ฅํƒ๐Ÿฅ๐Ÿฅ

    ๊ฒ€์ƒ‰ํ•ด๋ณด๋‹ˆ mapper.xml์— join์— ํ•ด๋‹นํ•˜๋Š” resultMap์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•, ์กฐ์ธํ•  ์ปฌ๋Ÿผ์„ ๋„ฃ์€ DTO๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์—ˆ์ง€๋งŒ ๋ญ”๊ฐ€ ์‰ฌ์šด ๋ฐฉ๋ฒ•์ด ์žˆ์ง€ ์•Š์„๊นŒ ์‹ถ์–ด ์ฐพ์•„๋ณด๋˜ ์ค‘์— hashMap์„ ์ด์šฉํ•˜๋ฉด ๋œ๋‹ค๋Š” ๊ธ€์„ ๋ด์„œ ์‹œ๋„ํ•ด๋ณด๋‹ˆ ์—„์ฒญ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ•ด๊ฒฐ๋์Šต๋‹ˆ๋‹ค.

     

    ๐Ÿ“Œ mapper ๋ณ€๊ฒฝ ์ „

    <select id="testList" parameterType="SearchDto" resultType="testDto">
        SELECT
            m.id, m.nm,
            f.APLY_DT, 
            q.APRV_YN,o
        FROM
            TB_Q q, TB_M m, TB_F f
        ORDER BY
            q.s_no
        DESC
    </select>

    ๐Ÿ“Œ mapper ๋ณ€๊ฒฝ ํ›„

    <select id="testList" parameterType="SearchDto" resultType="HashMap">
        SELECT
            m.id, m.nm,
            f.APLY_DT, 
            q.APRV_YN,o
        FROM
            TB_Q q, TB_M m, TB_F f
        ORDER BY
            q.s_no
        DESC
    </select>

    mapper์— resultType์„ DTO class์—์„œ HashMap์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์—ˆ๋Š”๋ฐ์š”.

    ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋ณ„ ๋‹ค๋ฅธ ์„ธํŒ… ์—†์ด๋„ joinํ•œ ํ…Œ์ด๋ธ”์„ jsp์— ๋ฟŒ๋ฆด์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ํ•˜์ง€๋งŒ HashMap ์œผ๋กœ ๋ฐ˜ํ™˜๋˜๋ฉด์„œ key ๊ฐ’์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ปฌ๋Ÿผ ๋ช…๊ณผ ๋™์ผํ•ด์ง€๊ธฐ ๋•Œ๋ฌธ์—
    HashMap์œผ๋กœ ๊ฐ€์ ธ์˜จ ๊ฐ’์„ jsp ์—์„œ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ key๊ฐ’์„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ปฌ๋Ÿผ๋ช…๊ณผ ๋˜‘๊ฐ™์ด ์ž…๋ ฅํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

    (๋Œ€๋ฌธ์ž์ธ ๊ฒฝ์šฐ ๋Œ€๋ฌธ์ž๋กœ ์ž…๋ ฅ)

     

    ๐Ÿ“Œ jsp ๋ณ€๊ฒฝ์ „

    <c:forEach items="${object.list}" var="list">
        <tr>
            <td>${list.aprvYn}</td>
            <td>${list.id}</td>
            <td>${list.nm}</td>
        </tr>
    </c:forEach>

    ๐Ÿ“Œ jsp ๋ณ€๊ฒฝํ›„

    <c:forEach items="${object.list}" var="list">
        <tr>
            <td>${list.APRV_YN}</td>
            <td>${list.ID}</td>
            <td>${list.NM}</td>
        </tr>
    </c:forEach>

    ๊ทธ๋Ÿฐ๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ตฌํ˜„์€ ๊ฐ„๋‹จํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ Database๊ฐ€ ๋ณ€๊ฒฝ๋  ๊ฒฝ์šฐ ๊ทธ์— ๋งž์ถฐ์„œ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ผ์ผ์ด ์ˆ˜์ •ํ•ด์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด์Šˆ ๋ฐœ์ƒ ํ™•๋ฅ ์ด ๋†’๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜ฅ

    ํ–‰๋ณต ์ฝ”๋”ฉ ๊ฐˆ๊ธธ์ด ๋ฉ€๋‹คใ… 

     

    * ๊ฒฐ๊ตญ์—” ๊ฒฐ๊ณผ๊ฐ’์— ๋งž๋Š” DTO๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋งตํ•‘ํ•˜๊ธฐ๋กœ ํ–ˆ์Šต๋‹ˆ๋‹ค!

    728x90
Designed by Tistory.