ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JAVA ์ž๋ฐ” ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค mybatis ๋™์ SQL
    JAVA 2020. 8. 23. 15:43

    ๐ŸŽฏ ์ž๋ฐ” ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ๋™์  SQL๋ฌธ์„ ์•Œ์•„๋ณธ๋‹ค.

     

     

     

     

    ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ํ™ˆํŽ˜์ด์ง€์—์„œ ๋™์ sql ์„ค๋ช…์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

    ์ง€๋‚œ๋ฒˆ ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค ํ”„๋กœ์ ํŠธ๋ฅผ ์‚ฌ์šฉํ•ด ์—ฐ์Šตํ•ด๋ณธ๋‹ค. ์•„๋ž˜์˜ ์ฝ”๋“œ๋ธ”๋Ÿญ ์ˆœ์„œ๋Š” Mapper.xml -> Main -> Service -> DAO ํด๋ž˜์Šค๋กœ ์ด์–ด์ง€๋ฉฐ, ์—ญ์ˆœ์œผ๋กœ ์‹คํ–‰๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ดํ•œ ๋’ค Main์—์„œ ์‹คํ–‰์„ ์ข…๋ฃŒํ•œ๋‹ค.

     

     

    selectDynamicDeptno

    <select id ="selectDynamicDeptno" parameterType = "HashMap" resultType="Dept">
        select deptno, dname, loc from dept
        <if test="deptno != null">
            where deptno= #{deptno}
        </if>
    </select>

    ์ฟผ๋ฆฌ๋ฌธ์— ifํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•ด ์กฐ๊ฑด์„ ๋ถ™์ผ ์ˆ˜ ์žˆ๋‹ค. ์กฐ๊ฑด์ด true์ผ ๊ฒฝ์šฐ์— where์ ˆ์— ๋”ฐ๋ผ ์ฟผ๋ฆฌ๋ฌธ์ด ์‹คํ–‰๋œ๋‹ค.

    selectDynamicDeptno๋ผ๋Š” id๋ฅผ ๊ฐ€์ง„ ์ด select์ฟผ๋ฆฌ๋Š” deptno๋ฅผ ํ•ด์‰ฌ๋งต์œผ๋กœ ๋ฐ›์•„์˜ค๊ณ , ๊ฒฐ๊ณผ๋ฅผ Dept ํด๋ž˜์Šค์— ์ €์žฅํ•  ๊ฒƒ์ด๋‹ค. ๋ฐ›์•„์˜จ deptno๊ฐ€ null์ธ ๊ฒฝ์šฐ์—” ์ € ์กฐ๊ฑด๋ฌธ์ด ์‹คํ–‰๋˜์ง€ ์•Š์•„์„œ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

    Main
    
        int deptno = 10;
        HashMap<String, Integer> map = new HashMap<String, Integer>();
    //    map.put("deptno", null);
        map.put("deptno", deptno);
        List<Dept> list = service.selectDynamicDeptno(map);
        for(Dept dept : list) {
        System.out.println(dept);
    }

    ๋ฉ”์ธ์—์„œ ๋ถ€์„œ๋ฒˆํ˜ธ๋Š” 10์œผ๋กœ ์ง€์ •ํ•ด์ฃผ๊ณ  ํ•ด์‰ฌ๋งต ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ƒ์„ฑ๋œ ๋งต์— ํ‚ค๊ฐ’๊ณผ ๋ฐธ๋ฅ˜๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ณ  ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“  ๋’ค ์„œ๋น„์Šค์˜ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋งŒ๋“ค์–ด์ง„ ๋งต์„ ์ „๋‹ฌํ•œ๋‹ค.

    Service
    
    public List<Dept> selectDynamicDeptno(HashMap<String, Integer> map){
        SqlSession session = MySqlSessionFactory.getSqlSession();
        List<Dept> list = null;
        try {
            list = dao.selectDynamicDeptno(session, map);
        } finally {
            session.close();
        }return list;
    }

    ์„œ๋น„์Šค์˜ ์…€๋ ‰ํ•จ์ˆ˜๋Š” ํ˜ธ์ถœ๋œ ๋ฉ”์ธ์—์„œ ๋งต์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์™”๋‹ค. ์ด ํ•จ์ˆ˜์˜ ๋ฆฌํ„ดํƒ€์ž…์€ ๋ฆฌ์ŠคํŠธ์ด๋‹ค.

    ์„ธ์…˜์„ ์—ฐ๊ฒฐํ•˜๊ณ  ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“  ๋’ค ๋ฆฌ์ŠคํŠธ์— dao์˜ ์…€๋ ‰ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ์„ธ์…˜๊ณผ ๋ฐ›์•„์˜จ ๋งต์„ ์ „๋‹ฌํ•ด์ฃผ๊ณ  session.close()๋กœ ์„ธ์…˜์„ ๋ฐ˜๋‚ฉํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๋ฆฌ์ŠคํŠธ๋ฅผ ํ˜ธ์ถœํ•œ ๊ณณ์œผ๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.

    ์ด ์„œ๋น„์Šค ํด๋ž˜์Šค๋Š” ์„ธ์…˜๋งŒ ์—ฐ๊ฒฐํ•˜๊ณ  ๋‹ซ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค๋Š” ๊ฑธ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

    DAO
    
    public List<Dept> selectDynamicDeptno(SqlSession session, HashMap<String, Integer> map) {
            List<Dept> list = session.selectList("com.dept.DeptMapper3.selectDynamicDeptno", map);
            return list;
        }

    dao์—์„  ์„ธ์…˜๊ณผ ๋งต(๋ฉ”์ธ์—์„œ ๋„˜์–ด์˜จ ํ‚ค-๋ฐธ๋ฅ˜๊ฐ’)์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์™”๋‹ค. ๋ฆฌํ„ดํƒ€์ž…์— ๋งž์ถฐ์„œ ๋ฆฌํ„ดํ•  ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค.

    SqlSession ์˜ selectList ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ์ฟผ๋ฆฌ ์‹คํ–‰๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ›์•„์˜ฌ ์ˆ˜ ์žˆ๋‹ค. ์ธ์ž์˜ ์ฒซ๋ฒˆ์งธ ์ž๋ฆฌ์—” Mapper.xml์˜ ์ฟผ๋ฆฌ์—์„œ ์ง€์ •ํ•œ id๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๋‘๋ฒˆ์žฌ ์ž๋ฆฌ์— ๋ฐ›์•„์˜จ ํ•ด์‰ฌ๋งต์„ ์ ๋Š”๋‹ค. ์ด ๋‘๊ฐœ๋ฅผ ์กฐํ•ฉํ•ด์„œ ๋ฆฌ์ŠคํŠธ๋กœ ๋„ฃ์—ˆ์œผ๋‹ˆ ํ˜ธ์ถœํ•œ ๊ณณ์— ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ด๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. dao๋Š” ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ ๋‹ด์€ ๋ฆฌ์ŠคํŠธ ๋“ค๊ณ  ์„œ๋น„์Šค๋กœ ๊ฐ€๊ณ , ์„œ๋น„์Šค๋Š” ๋ฆฌ์ŠคํŠธ ๋“ค๊ณ  ๋ฉ”์ธ์œผ๋กœ ๊ฐ„๋‹ค. ๋ฉ”์ธ์€ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ›์•„์„œ ๊ฒฐ๊ณผ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

    ์ด์ „์—” ์„œ๋น„์Šค ํด๋ž˜์Šค์—์„œ ์ปค๋„ฅ์…˜์„ ๋ฐ›์•„์˜ค๊ณ  dao์—์„œ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•œ ํ›„ ๋ฉ”์ธ์œผ๋กœ ๋ฆฌํ„ดํ•˜๋Š” ์—ญํ• ์„ ํ–ˆ๋Š”๋ฐ, ์ด์   ๋งคํผ๋กœ ์ฟผ๋ฆฌ๋ฅผ ๋ถ„๋ฆฌํ–ˆ์œผ๋‹ˆ dao๋Š” ์„ธ์…˜๊ณผ ์ฟผ๋ฆฌ๋ฅผ ๋ฐ›์•„์™€์„œ ๋ฉ”์ธ์œผ๋กœ ๋ฆฌํ„ด๋งŒ ํ•˜๋Š” ์—ญํ• ์„ ํ•˜๊ฒŒ ๋œ๋‹ค. ์—ญํ• ์ด ์ค„์–ด๋“ค์–ด์„œ ์ฝ”๋“œ๊ฐ€ ์งง์•„์กŒ๋‹ค.

     

     

    selectDynamicChoose

    <select id="selectDynamicChoose" parameterType = "HashMap" resultType="Dept">
        select deptno, dname, loc from dept
        <choose>
            <when test="dname != null">
                where dname = #{dname}
            </when>
            <when test="loc != null">
                where loc = #{loc}
            </when>
            <when test="dname != null and loc != null">
                where loc = #{loc} and dname #{dname}
            </when>
            <otherwise>
                where deptno = 10
            </otherwise>
        </choose>
    </select>

    ์ž๋ฐ”์˜ if-else ๋ฌธ๋ฒ•์ฒ˜๋Ÿผ ์กฐ๊ฑด์„ ์—ฌ๋Ÿฌ๊ฐœ ์‚ฌ์šฉํ•  ๋•Œ ๋งˆ์ด๋ฐ”ํ‹ฐ์Šค์˜ ํƒœ๊ทธ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. when์ ˆ์„ if๋กœ, otherwiser๋ฅผ else๋กœ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

    ์ด ์ฟผ๋ฆฌ๋ฌธ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…์€ ํ•ด์‰ฌ๋งต์œผ๋กœ ๋ฐ›๊ณ  ๊ฒฐ๊ณผ๋Š” Dept์— ์ €์žฅํ•œ๋‹ค. ์œ„์—์„œ ํ–ˆ๋˜๊ฑฐ์™€ ๋‹ฌ๋ฆฌ ์ด๋ฒˆ์—” ๊ฒ€์‚ฌํ•  ์กฐ๊ฑด์ด ์—ฌ๋Ÿฌ๊ฐœ๋‹ค. ์ด ๋ชจ๋“  ์กฐ๊ฑด์— ํ•ด๋‹นํ•˜์ง€ ์•Š์œผ๋ฉด 10๋ฒˆ๋ถ€์„œ ์ •๋ณด ์ถœ๋ ฅํ•œ๋‹ค.

    HashMap<String, String> map2 = new HashMap<String, String>();
            map2.put("deptno", "์ œ์ฃผ");
            map2.put("dname", "์˜์—…");
            List<Dept> list1 = service.selectDynamicChoose(map2);
            for(Dept dept : list) {
                System.out.println(dept);
            }

    dname๊ณผ loc๋ฅผ ์กฐ๊ฑด์œผ๋กœ ๊ฒ€์‚ฌํ•˜๊ธฐ๋กœ ํ–ˆ์œผ๋‹ˆ๊นŒ ํ•ด์‰ฌ๋งต ํƒ€์ž…์€ ๋ชจ๋‘ ์ŠคํŠธ๋ง์œผ๋กœ ๋ฐ›์•„์˜ค๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๋งต์— ํ‚ค ๋ฐธ๋ฅ˜ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ณ  ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์„œ๋น„์Šค์˜ ์…€๋ ‰ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ๋งต์„ ์ธ์ž๋กœ ์ „๋‹ฌํ•œ๋‹ค.

    HashMap<String, String> map2 = new HashMap<String, String>();
            map2.put("deptno", "์ œ์ฃผ");
            map2.put("dname", "์˜์—…");
            List<Dept> list1 = service.selectDynamicChoose(map2);
            for(Dept dept : list) {
                System.out.println(dept);
            }

    dname๊ณผ loc๋ฅผ ์กฐ๊ฑด์œผ๋กœ ๊ฒ€์‚ฌํ•˜๊ธฐ๋กœ ํ–ˆ์œผ๋‹ˆ๊นŒ ํ•ด์‰ฌ๋งต ํƒ€์ž…์€ ๋ชจ๋‘ ์ŠคํŠธ๋ง์œผ๋กœ ๋ฐ›์•„์˜ค๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ๋งต์— ํ‚ค ๋ฐธ๋ฅ˜ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ณ  ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์„œ๋น„์Šค์˜ ์…€๋ ‰ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ๋งต์„ ์ธ์ž๋กœ ์ „๋‹ฌํ•œ๋‹ค.

        public List<Dept> selectDynamicChoose(HashMap<String, String> map){
            SqlSession session = MySqlSessionFactory.getSqlSession();
            List<Dept> list = null;
            try {
                list = dao.selectDynamicChoose(session, map);
            } finally {
                session.close();
            }return list;
        }

    ์„œ๋น„์Šค์˜ ์…€๋ ‰ํ•จ์ˆ˜๋Š” ๋งต์„ ์ž˜ ๋ฐ›์•„์™”๋‹ค. ์„ธ์…˜์—ฐ๊ฒฐ ๋จผ์ € ํ•ด์ฃผ๊ณ  ๋ฆฌํ„ดํ•  ๋ฆฌ์ŠคํŠธ ๋งŒ๋“  ๋‹ค์Œ์— dao์˜ ์…€๋ ‰ํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜๋ฉด์„œ ์ธ์ž๋กœ ์ž์‹ ์ด ๋งŒ๋“  ์„ธ์…˜์ด๋ž‘ ๋ฉ”์ธ์—์„œ ๋ฐ›์•„์˜จ ๋งต์„ ๋‹ด์•„์„œ ๋ณด๋‚ด์ค€๋‹ค. ๊ทธ๋Ÿฌ๊ณ  ์„ธ์…˜๋‹ซ๊ณ  ๋ฐ›์•„์˜จ ๋ฆฌ์ŠคํŠธ ๋ฆฌํ„ดํ•˜๊ณ  ์ž๊ธฐ ํ• ์ผ์„ ๋๋ƒˆ๋‹ค.

    public List<Dept> selectDynamicChoose(SqlSession session, HashMap<String, String> map) {
            List<Dept> list = session.selectList("com.dept.DeptMapper3.selectDynamicChoose", map);
            return list;
        }

    dao๋กœ ๊ฐ€๋ณด์ž. dao๋Š” ์„œ๋น„์Šค์—์„œ ํ˜ธ์ถœ๋ฐ›์€ ๋Œ€๋กœ ์„ธ์…˜๊ณผ ๋งต์„ ์ธ์ž๋กœ ๋ฐ›์•„์™”๋‹ค. ์„œ๋น„์Šค๋กœ ๋ฆฌํ„ดํ•  ๋ฆฌ์ŠคํŠธ์— ์„ธ์…˜์˜ ์…€๋ ‰๋ฆฌ์ŠคํŠธ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ xml์˜ ์ฟผ๋ฆฌ๋ฌธ ์•„์ด๋””์™€ ๋ฐ›์•„์˜จ ๋งต์„ ์ „๋‹ฌํ•˜๊ณ  ์ฟผ๋ฆฌ๋ฌธ ์‹คํ–‰๋œ๊ฑธ ๋ฐ›์•„์™€์„œ ๋ฆฌ์ŠคํŠธ์— ๋‹ด๊ณ  ๊ทธ๊ฑธ ๋ฆฌํ„ดํ•œ๋‹ค. ์ด์ œ ์ด๊ฑธ ๋“ค๊ณ  ์„œ๋น„์Šค ๊ฐ€๊ณ  ๋ฉ”์ธ ๊ฐ€์„œ ๋ฉ”์ธ์—์„œ ๊ฒฐ๊ณผ์ถœ๋ ฅํ•˜๊ณ  ๋๋‚œ๋‹ค.

     

     

    multiSelect

    <select id="multiSelect" parameterType="java.util.List" resultType="Dept">
        select deptno, dname, loc from dept where deptno In
        <foreach item="item" index="index" collection="list" open="("
        separator="," close=")">
            #{item}
        </foreach>
    </select>

     

    foreach ํƒœ๊ทธ๋Š” ๋Š” ์ปฌ๋ ‰์…˜์— ๋‹ด์•„์˜จ ๋ฐ์ดํ„ฐ๋ฅผ in์ ˆ์— ๋ชจ๋‘ ์ถ”๊ฐ€ํ•  ๊ฒฝ์šฐ ์ฃผ๋กœ ์‚ฌ์šฉ๋œ๋‹ค. ๋ฐ˜๋ณต ๋Œ๋ฆด ๊ฐ ํ•ญ๋ชฉ๋“ค์˜ ๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„์€ item์ด๊ณ  ์ปฌ๋ ‰์…˜ ํƒ€์ž…, ๋ฐ˜ํ™˜๊ฐ’์˜ ์‹œ์ž‘๊ณผ ๋ ํ˜•ํƒœ, ๋ฐ˜๋ณต๋ฌธ ๋Œ ๋•Œ๋งˆ๋‹ค ๋ถ™์—ฌ์ค„ ๊ตฌ๋ถ„์ž๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. 

    deptno๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋„˜๋ฒ„๋ฅผ ๋ฐ›์•„์™€์„œ ํ•ด๋‹นํ•˜๋Š” ์ •๋ณด๋ฅผ ์…€๋ ‰ํ•˜๋Š” ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•œ๋‹ค. ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ›์•˜๊ธฐ ๋•Œ๋ฌธ์— ์ปฌ๋ ‰์…˜๋„ ๋ฆฌ์ŠคํŠธ๋กœ ์ง€์ •ํ–ˆ๋‹ค. in์—ฐ์‚ฐ์ž ๋’ค๋Š” (๊ด„ํ˜ธ)๋กœ ๋ฌถ์–ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์˜คํ”ˆ๊ณผ ํด๋กœ์ฆˆ๋ฅผ ๊ด„ํ˜ธ๋กœ ์ง€์ •ํ–ˆ๋‹ค.

    System.out.println("=========================");
        List<Integer> deptnoes = Arrays.asList(10,12,13); 
        List<Dept> list2 = service.multiSelect(deptnoes);
        for (Dept dept : list2) {
            System.out.println(dept);
        }

     

    ์ •์ˆ˜๋งŒ ๋ฐ›๋Š” ๋ฆฌ์ŠคํŠธ ํƒ€์ž…์˜ ๋ณ€์ˆ˜ deptnoes์— ๋ฐฐ์—ด์„ ๋„ฃ์„๊ฑฐ๋‹ค. ์ผ๋‹จ 10, 12, 13์ด ๋ฐฐ์—ด์ธ๋ฐ ์ด๊ฑธ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ํ•ด์„œ ๋ฆฌ์ŠคํŠธ์— ๋„ฃ์–ด์ค€๋‹ค. (Arrays.asList๋Š” ๋ฐฐ์—ด์„ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ํ•ด ์ค€๋‹ค.) ๊ทธ๋ฆฌ๊ณ  dept๋งŒ ๋ฐ›๋Š” ๋ฆฌ์ŠคํŠธ ํƒ€์ž…์˜ ๋ฆฌ์ŠคํŠธ2์— ์„œ๋น„์Šค์˜ ๋ฉ€ํ‹ฐ์…€๋ ‰ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์ธ์ž๋กœ ์œ„์—์„œ ๋„ฃ์€ deptnoes๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

    public List<Dept> multiSelect(List<Integer> deptnoes){
            SqlSession session = MySqlSessionFactory.getSqlSession();
            List<Dept> list = null;
            try {
                list = dao.multiSelect(session, deptnoes);
            } finally {
                session.close();
            }return list;
        }

    ๋ฉ€ํ‹ฐ์…€๋ ‰ํŠธ๋Š” ๋ฉ”์ธ์—์„œ deptnoes๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์•„์™”๋‹ค. ์„ธ์…˜์—ฐ๊ฒฐํ•˜๊ณ  ๋ฆฌ์ŠคํŠธ ๋งŒ๋“  ๋‹ค์Œ์— ๋ฆฌ์ŠคํŠธ์— dao์˜ ๋ฉ€ํ‹ฐ์…€๋ ‰ํ•จ์ˆ˜ ํ˜ธ์ถœํ•ด์„œ ์„ธ์…˜์ด๋ž‘ ๋ฐ›์•„์˜จ ๋ถ€์„œ๋„˜๋ฒ„๋ฆฌ์ŠคํŠธ ์ „๋‹ฌํ•˜๊ณ  ์„ธ์…˜๋‹ซ๊ณ  ๋ฐ›์•„์˜จ ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

    public List<Dept> multiSelect(SqlSession session, List<Integer> deptnoes) {
            List<Dept> list = session.selectList("com.dept.DeptMapper4.multiSelect", deptnoes);
            return list;
        }

    dao๋Š” ์„ธ์…˜์ด๋ž‘ ๋ถ€์„œ๋„˜๋ฒ„๋ฆฌ์ŠคํŠธ ์ž˜ ์ „๋‹ฌ ๋ฐ›์•„์„œ ๋ฆฌ์ŠคํŠธ ๋งŒ๋“ค๊ณ  ์ฟผ๋ฆฌ๋ฌธ์— ์ฟผ๋ฆฌ๋ฌธ์•„์ด๋””๋ž‘ ๋ถ€์„œ๋„˜๋ฒ„๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. ๋ฐ›์•„์˜จ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฆฌํ„ดํ•˜๊ณ  ๋ฉ”์ธ๊นŒ์ง€ ๋ฐ๋ ค๊ฐ€์„œ ๋ฉ”์ธ์—์„œ ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ ค ์ถœ๋ ฅํ•œ๋‹ค.

     

     

    multiDelte

    <delete id="multiDelte" parameterType="java.util.List">
        delete from dept where loc in
        <foreach  item="item" index="index" collection="list"
            open="(" separator="," close=")">
            #{item}
        </foreach>
    </delete>

    Delete๋ฅผ ์ˆ˜ํ–‰ํ•  ์ด ์ฟผ๋ฆฌ๋ฌธ์€ ๋ฆฌ์ŠคํŠธ ํƒ€์ž…์œผ๋กœ loc๋ฅผ ๋ฐ›์•„์˜ฌ๊ฑฐ๋‹ค.

    List<String> loc = Arrays.asList("ํŒ๊ต", "๊ฒฝ๊ธฐ");
    service.multiDelete(loc);

    ์—ฌ๋Ÿฌ๊ฐœ์˜ ์ง€์—ญ์ •๋ณด๋ฅผ ๋ฆฌ์ŠคํŠธ์— ๋‹ด๊ณ  ์„œ๋น„์Šค์˜ ๋”œ๋ฆฌํŠธ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด๋‚ด์ค€๋‹ค.

    public void multiDelete(List<String> loc) {
            SqlSession session = MySqlSessionFactory.getSqlSession();
            try {
                dao.multiDelete(session, loc);
                session.commit();
            } finally {
                session.close();
            }
        }

    DML์€ ์ˆ˜ํ–‰๋งŒ ํ•˜๊ณ  ๋ฐ˜ํ™˜๊ฐ’ ์—†์–ด๋„ ๋˜๋‹ˆ๊นŒ ๋ฆฌํ„ดํƒ€์ž…์€ void๋กœ ํ•˜๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ฐ›์•„์™”๋‹ค.

    ์„ธ์…˜์—ฐ๊ฒฐํ•˜๊ณ  dao์˜ ๋ฉ€ํ‹ฐ๋”œ๋ฆฟ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์„ธ์…˜, ๋ฆฌ์ŠคํŠธ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. DML์€ ํŠธ๋žœ์žญ์…˜ ์ž๋™๋ฐœ์ƒ์ด๋ผ ์ปค๋ฐ‹ ํ•ด์ฃผ๊ณ  ์ž์› ๋ฐ˜๋‚ฉํ•˜๊ณ  ๋๋‚ฌ๋‹ค.

    public void multiDelete(SqlSession session, List<String> loc) {
        System.out.println(loc);
        int num = session.delete("com.dept.DeptMapper4.multiDelte", loc);
        System.out.println(num);
    }

    dao๋„ ๋ฆฌํ„ดํƒ€์ž…์€ void๋กœ ์„ค์ •ํ•˜๊ณ  ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์„ธ์…˜, ๋ฆฌ์ŠคํŠธ ๋ฐ›์•„์™”๋‹ค. ์‹คํ–‰ํšŸ์ˆ˜ ์ถœ๋ ฅ์„ ์œ„ํ•ด num ๋ณ€์ˆ˜ ๋งŒ๋“ค์–ด์ฃผ๊ณ  ๋”œ๋ฆฌํŠธ ์ฟผ๋ฆฌ๋กœ ์•„์ด๋””๋ž‘ ๋ฆฌ์ŠคํŠธ ์ „๋‹ฌํ•ด์ฃผ๊ณ  ์ฟผ๋ฆฌ ์‹คํ–‰ํ•ด์˜จ ๊ฒฐ๊ณผ๋ฅผ num์— ๋„ฃ์–ด์„œ ํšŸ์ˆ˜ ์ถœ๋ ฅํ•œ๋‹ค. ๋ฉ”์ธ์œผ๋กœ ๊ฐ€์„œ ์‹คํ–‰ํ•˜๋ฉด ํŒ๊ต, ๊ฒฝ๊ธฐ ์ง€์—ญ์˜ ๋ถ€์„œ ๋ ˆ์ฝ”๋“œ๊ฐ€ ์‚ญ์ œ๋œ ๊ฑธ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

     

     

    multiDelte2

    <delete id="multiDelte2" parameterType="java.util.List">
        delete from dept where deptno in
        <foreach  item="item" index="index" collection="list"
            open="(" separator="," close=")">
            #{item.deptno} <!-- dept๊ฐ์ฒด์˜ deptno ์‚ฌ์šฉ -->
        </foreach>
    </delete>

    deptno๋ฅผ ๋ฐ›์•„์™€์„œ ๊ทธ ๋ถ€์„œ์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

    List<Dept> depts = Arrays.asList(new Dept(12, "", ""), new Dept(13, "", ""));
            service.multiDelete2(depts);
            List<Dept> list = service.selectAll();
            for (Dept dept : list) {
                System.out.println(dept);

    Dept ํƒ€์ž…๋งŒ ๋ฐ›๋Š” ๋ฆฌ์ŠคํŠธ ์•ˆ์— ๋ถ€์„œ ๋ฒˆํ˜ธ๋งŒ ๋‹ด์€ dept ์ƒ์„ฑ์ž ๋‘๊ฐœ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋„ฃ์–ด์ค€๋‹ค. ๊ทธ ๋‹ค์Œ ์„œ๋น„์Šค์˜ ๋”œ๋ฆฌํŠธ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค. ์‚ญ์ œ๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ๋‚˜์„œ ์ „์— ๋งŒ๋“ค์—ˆ๋˜ selectAll ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋‚จ์€ ๋ฐ์ดํ„ฐ๋“ค์„ ์ถœ๋ ฅํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค.

    public void multiDelete2(List<Dept> depts) {
            SqlSession session = MySqlSessionFactory.getSqlSession();
            try {
                dao.multiDelete2(session, depts);
                session.commit();
            } finally {
                session.close();
            }
        }

    ์„œ๋น„์Šค๋Š” ๋ฆฌ์ŠคํŠธ ์ „๋‹ฌ๋ฐ›์•„ ์™”๊ณ , ์„ธ์…˜์„ ์—ฐ๊ฒฐํ•œ๋‹ค. ์—ฐ๊ฒฐ๋œ ๊ฑฐ dao๋กœ ๋ฆฌ์ŠคํŠธ๋ž‘ ๋ณด๋‚ด๊ณ , ์ปค๋ฐ‹ํ•˜๊ณ , ์„ธ์…˜์„ ๋‹ซ๋Š”๋‹ค.

    public void multiDelete2(SqlSession session, List<Dept> depts) {
            int num = session.delete("com.dept.DeptMapper4.multiDelte", depts);
            System.out.println("์‚ญ์ œ๋œ ๊ฐฏ์ˆ˜"+num);
        }

    dao๋Š” ์„ธ์…˜์ด๋ž‘ ๋ฆฌ์ŠคํŠธ ๋ฐ›์•„์˜ค๊ณ , ๋ฐ›์€๊ฑฐ ์„ธ์…˜์œผ๋กœ ๋„˜๊ฒจ์„œ ์ฟผ๋ฆฌ๋ฌธ ์ˆ˜ํ–‰ํ•ด์˜ค๊ณ , ํšŸ์ˆ˜ ๋‹ด์•„์„œ ์ถœ๋ ฅํ•œ๋‹ค.

     

     

    multiDelte3

    <delete id="multiDelte3" parameterType="java.util.List">
        delete from dept where loc in
        <foreach  item="item" index="index" collection="list"
            open="(" separator="," close=")">
            #{item.loc}
        </foreach>
    </delete>

    loc๋ฅผ ๋ฐ›์•„์™€์„œ ๊ทธ ์ง€์—ญ์˜ ๋ ˆ์ฝ”๋“œ๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

    Dept dept1 = new Dept();
    Dept dept2 = new Dept();
    dept1.setLoc("๋ถ€์‚ฐ");
    dept2.setLoc("์ œ์ฃผ");
    List<Dept> depts = Arrays.asList(dept1, dept2);
    //List<Dept> depts = Arrays.asList(new Dept(12, "", ""), new Dept(13, "", ""));
    service.multiDelete3(depts);
    List<Dept> list = service.selectAll();
    for (Dept dept : list) {
        System.out.println(dept);
    }

    ์ด๋ฒˆ์—๋Š” dept ๊ฐ์ฒด๋ฅผ ๊ฐ๊ฐ ์ƒ์„ฑํ•˜๊ณ  ๊ฐ๊ฐ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ๊ณ  ๋ฆฌ์ŠคํŠธ์— ๋‹ด๋Š”๋‹ค. ์ฃผ์„์ฒ˜๋ฆฌ ์นœ ๋ถ€๋ถ„์„ ๋œฏ์–ด๋ณด๋ฉด ์ €๋ ‡๊ฒŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค. ์„œ๋น„์Šค์˜ delete ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

    public void multiDelete3(List<Dept> depts) {
            SqlSession session = MySqlSessionFactory.getSqlSession();
            try {
                dao.multiDelete3(session, depts);
                session.commit();
            } finally {
                session.close();
            }
        }

    ํ•˜๋Š” ์ผ์€ ์œ„์™€ ๋˜‘๊ฐ™๋‹ค.

    public void multiDelete3(SqlSession session, List<Dept> depts) {
            System.out.println(depts);
            int num = session.delete("com.dept.DeptMapper4.multiDelte3", depts);
            System.out.println("์‚ญ์ œ๋œ ๊ฐฏ์ˆ˜"+num);
        }

    delete๋ฅผ ์ˆ˜ํ–‰ํ•˜๊ณ  ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

     

     

    multiInsert

    <insert id="multiInsert" parameterType="arrayList">
        insert all 
        <foreach  item="item" index="index" collection="list">
                into dept (deptno, dname, loc) 
                values (#{item.deptno}, #{item.dname}, #{item.loc})
        </foreach>
        select * from dual
    </insert>

    ๋ฌด์กฐ๊ฑด insert all์„ ์‚ฌ์šฉํ•œ๋‹ค. ํŒŒ๋ผ๋ฏธํ„ฐ ํƒ€์ž…์€ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ›๋Š”๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ ธ์˜ฌ ๋ถ€๋ถ„์€ foreach๋กœ ๋ฌถ์–ด์ฃผ๊ณ  ๊ทธ ๋ฐ–์— ์„œ๋ธŒ์ฟผ๋ฆฌ๊ฐ€ ๋ถ™์–ด์žˆ๋‹ค. ์ž„์˜ ํ…Œ์ด๋ธ”์—์„œ ๊ฐ€์ ธ์˜จ ๋ชจ๋“  ์ •๋ณด๋ฅผ deptํ…Œ์ด๋ธ”์— ์‚ฝ์ž…ํ•œ๋‹ค.

    dept1 = new Dept (13,"์˜์—…","ํŒ๊ต");
    dept2 = new Dept(22, "๊ฐœ๋ฐœ", "๊ฒฝ๊ธฐ");
    depts = Arrays.asList(dept1, dept2);
    service.multiInsert(depts);

    ์ธ์„œํŠธํ•  ์ •๋ณด๋ฅผ ๋ฆฌ์ŠคํŠธ์— ๋‹ด๊ณ  ์„œ๋น„์Šค์— ์ „๋‹ฌํ•œ๋‹ค.

    public void multiInsert(List<Dept> depts) {
            SqlSession session = MySqlSessionFactory.getSqlSession();
            try {
                dao.multiInsert(session, depts);
                session.commit();
            } finally {
                session.close();
            }
        }

    ์„œ๋น„์Šค๋Š” ์„ธ์…˜์—ฐ๊ฒฐํ•˜๊ณ  ๋ฐ›์•„์˜จ ๊ฑฐ๋ž‘ ๊ฐ™์ด dao์— ๋„˜๊ธฐ๊ณ  ์ปค๋ฐ‹ํ•˜๊ณ  ํด๋กœ์ฆˆํ•œ๋‹ค.

    public void multiInsert(SqlSession session, List<Dept> depts) {
            int num = session.insert("com.dept.DeptMapper4.multiInsert", depts);
            System.out.println("์—…๋ฐ์ดํŠธ ๊ฐฏ์ˆ˜"+num);
        }

    dao๋Š” ๋ฐ›์•„์˜จ๊ฑฐ ์„ธ์…˜์— ์ „๋‹ฌํ•ด์„œ ์ฟผ๋ฆฌ ์ˆ˜ํ–‰ํ•˜๊ณ  ์‚ญ์ œ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

     

     

    multiUpdate

    <update id="multiUpdate" parameterType="java.util.List" >
        update dept set loc='์ œ์ฃผ' where deptno in
        <foreach  item="item" index="index" collection="list"
            open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>

    deptno๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ›์•„์™€์„œ ๊ทธ ๋ถ€์„œ์˜ ์œ„์น˜๋ฅผ "์ œ์ฃผ"๋กœ ์—…๋Žƒํ•œ๋‹ค.

    List<Integer> deptnoes = Arrays.asList(80, 88, 90);
    service.multiUpdate(deptnoes);

    ์ •์ˆ˜๋งŒ ๋ฐ›๋Š” ๋ฆฌ์ŠคํŠธ ํƒ€์ž…์œผ๋กœ ๋ถ€์„œ๋ฒˆํ˜ธ ์—ฌ๋Ÿฌ๊ฐœ ๋„ฃ์–ด์ฃผ๊ณ  ์„œ๋น„์Šค์˜ ์—…๋Žƒํ•จ์ˆ˜ ํ˜ธ์ถœํ•˜๊ณ  ์ธ์ž๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

    public void multiUpdate(List<Integer> deptnoes) {
            SqlSession session = MySqlSessionFactory.getSqlSession();
            try {
                dao.multiUpdate(session, deptnoes);
                session.commit();
            } finally {
                session.close();
            }
        }

    ์„ธ์…˜์—ฐ๊ฒฐํ•˜๊ณ  ์„ธ์…˜,๋ฆฌ์ŠคํŠธ ์ „๋‹ฌ. ์ปค๋ฐ‹. ํด๋กœ์ฆˆ.

    public void multiUpdate(SqlSession session, List<Integer> deptnoes) {
            int num = session.update("com.dept.DeptMapper4.multiUpdate", deptnoes);
            System.out.println("update ๊ฐฏ์ˆ˜"+num);
        }

    ์„ธ์…˜์— ๋ฐ›์•„์˜จ๊ฑฐ ์ „๋‹ฌํ•ด์„œ ์ฟผ๋ฆฌ ์‹คํ–‰ํ•˜๊ณ  ๋„˜์— ๋‹ด์•„์„œ ํšŸ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

     

     

     

    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

๊ฐœ๋ฐœ๊ณต๋ถ€