โโ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 |
์คํ๊ฒฐ๊ณผ :
๋๊ธ