Framework/Spring Framework

(22.12.21)GeneratedKeyHolder ์‚ฌ์šฉํ•˜์—ฌ PK ์ €์žฅํ•˜๊ธฐ, MyBatis ์‚ฌ์šฉํ•˜์—ฌ DB์— ์—ฐ๊ฒฐํ•˜๊ธฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ ์˜ค์›” 2022. 12. 21.

 

โ—โ—GeneratedKeyHolder ์‚ฌ์šฉํ•˜์—ฌ KEY๊ฐ’์„ ์ •ํ•˜์—ฌ KeyHolder์— ์ €์žฅํ•˜๊ธฐโ—โ—

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public boolean addAndGetPK(Emp emp) 
    {
        GeneratedKeyHolder kh = new GeneratedKeyHolder();
        int rows = jdbcTemplate.update((conn)->{
            PreparedStatement pstmt;
            String sql = "INSERT INTO emp2(empno, ename, deptno, sal, hiredate) " +
                        "VALUES(?,?,?,?,?)";
            pstmt = conn.prepareStatement(sql, new String[]{"empno"});
            pstmt.setInt(1, emp.getEmpno());
            pstmt.setString(2, emp.getEname());
            pstmt.setInt(3, emp.getDeptno());
            pstmt.setFloat(4, emp.getSal());
            pstmt.setDate(5, emp.getHiredate());
            return pstmt;
            }, kh);
        int insertedEmpno = kh.getKey().intValue();
        //๋‘๋ฒˆ์งธ sql์— ์œ„์˜ ์ˆซ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ FK๋กœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
        return rows>0 ? true : false;
    }
cs

 

 pstmt = conn.prepareStatement(sql, new String[]{"empno"});

์‚ฌ์›๋ฒˆํ˜ธ๋ฅผ ํ‚ค๋กœ์„œ ์‚ฌ์šฉํ•˜์—ฌ ์ €์žฅํ•œ๋‹ค.

 

๋…ธ๋ž‘์ƒ‰ ๋ถ€๋ถ„์ด ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค ๋ถ€๋ถ„์ด๋‹ค.

 

 


โ—โ—MyBatis ์‚ฌ์šฉํ•˜์—ฌ DB์— ์—ฐ๊ฒฐํ•˜๊ธฐโ—โ—

 

 

Spring ํ”„๋ ˆ์ž„์›Œํฌ์™€  DB๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” 4๊ฐ€์ง€ ๋ฐฉ๋ฒ•

โ‘ ์ €์ˆ˜์ค€์˜ ์ง์ ‘ ์ฝ”๋”ฉ
โ‘กJdbcTemplate(Boiler Plate Codes๋ฅผ ์•ˆ์จ๋„ ๋œ๋‹ค. )

โ‘ขMyBatis (ORM, SQL, xml, ์ฝ”๋“œ์™€ sql ๋ถ„๋ฆฌ)

โ‘ฃJPA

 

 

MyBatis ์‚ฌ์šฉํ•˜์—ฌ DB์— ์—ฐ๊ฒฐํ•˜๊ธฐ (ORM, SQL, xml, ์ฝ”๋“œ์™€ sql ๋ถ„๋ฆฌ)

-์ฝ”๋“œ :  interface

-sql : xml

 

MyBatis๋Š” interface ์™€ xml ์ด์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ถ€๋ถ„๊ณผ sql๋ถ€๋ถ„์„ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค.

sql ์ฝ”๋“œ๋ฅผ ์ œ์™ธํ•˜๋ฉด ๋ชจ๋“ ๊ฑธ Boiler Plate Codes๋กœ ๊ฐ„์ฃผํ•œ๋‹ค.

 

 

MyBatis๋ฅผ ์“ฐ๊ธธ ์œ„ํ•ด์„   Maven Repository ์—์„œ dependency๋ฅผ ๋‹ค์šด ๋ฐ›์•„ pom.xml์— ์„ค์ •ํ•ด์ค˜์•ผํ•œ๋‹ค.

 

 

MybatisEmpController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ezen.spring.web.mapper.EmpMapper;
import com.ezen.spring.web.vo.Emp;
 
@RestController
@RequestMapping("/mybatis/emp")
public class MybatisEmpController {
    @Autowired
    private EmpMapper dao;
    
    @GetMapping("/list")
    public String list() {
        return dao.getlist().toString();
    }
    @GetMapping("/listByDeptno/{deptno}")
    public String listByDeptno(@PathVariable(name="deptno"int deptno) {
        return dao.listByDeptno(deptno).toString();
    }
    @GetMapping("/deleteByEmpno/{empno}")
    public Boolean deleteByEmpno(@PathVariable(name="empno"int empno) {
        Emp emp = new Emp();
        emp.setEmpno(empno);
        return dao.deleteByEmpno(emp)>0?true:false;
    }
    /*
     * @GetMapping("/deleteByEmpno")   ์ฃผ์†Œ์ฐฝ์— /mybatis/emp/deleteByEmpno?empno=7369
        public Boolean deleteByEmpno(Emp emp) {
        return dao.deleteByEmpno(emp)>0?true:false;
    }
     */
    @GetMapping("/updateByDeptno/{deptno}/{sal}")
    public Boolean updateByDeptno(@PathVariable Map<String,Integer> map) {
        return dao.updateByDeptno(map)>0?true:false;
    }
    @GetMapping("/getListWithDeptno")
    public String getListWithDeptno() {
        return dao.getListWithDeptno().toString();
    }
}
cs

 

 

EmpMapper.java

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Mapper;
import com.ezen.spring.web.vo.Emp;
 
@Mapper
public interface EmpMapper {
    List<Emp> getlist();
    List<Emp> listByDeptno(int deptno);
    int deleteByEmpno(Emp emp);
    int updateByDeptno(Map map);
    List<Map<String,Object>> getListWithDeptno();
}
cs

 

 

 

EmpMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ezen.spring.web.mapper.EmpMapper">
    <select id="getlist" 
       resultType="com.ezen.spring.web.vo.Emp">
        SELECT * FROM emp2
    </select>
    <select id="listByDeptno" 
       resultType="com.ezen.spring.web.vo.Emp"
       parameterType="Integer">
        SELECT * FROM emp2 WHERE deptno =#{deptno}
    </select>
    <delete id="deleteByEmpno"
        parameterType="com.ezen.spring.web.vo.Emp">
         DELETE FROM emp2 WHERE empno=#{empno}
    </delete>
    <update id="updateByDeptno"
        parameterType="map">
        <!-- < ๋ถ€๋“ฑํ˜ธ ๋•Œ๋ฌธ์— c๋ฐ์ดํ„ฐ ํ˜•์‹์œผ๋กœ ๋งŒ๋“ค์–ด์ค˜์•ผํ•œ๋‹ค. -->
        <![CDATA[UPDATE emp2 SET sal=sal*1.05 
        WHERE deptno=#{deptno} AND sal<#{sal}]]>
    </update>
     <select id="getListWithDeptno" 
       resultType="map">
        SELECT empno, ename, e.deptno, d.dname 
        FROM emp2 e INNER JOIN dept d ON e.deptno=d.deptno
    </select>
</mapper>
cs

 

์‹คํ–‰๊ฒฐ๊ณผ : 

http://localhost/mybatis/emp/list

 

http://localhost/mybatis/emp/listByDeptno/30

 

http://localhost/mybatis/emp/deleteByEmpno/7369

 

http://localhost/mybatis/emp/updateByDeptno/30/2500

 

http://localhost/mybatis/emp/getListWithDeptno

 

๋Œ“๊ธ€