** ๐ 6.1๋จ๊ณ: ์ธํฐํ์ด์ค ๊ธฐ๋ฐ ๋งคํผ ๊ตฌ์กฐ๋? **
(๐ dao/StudentMapper.java
, mapper/StudentMapper.xml
๊ธฐ์ค)
๐ฏ ํ์ต ๋ชฉํ
ํญ๋ชฉ |
์ค๋ช
|
๋ชฉ์ |
XML + ์ธํฐํ์ด์ค๋ฅผ ์ฐ๊ฒฐํ์ฌ Mapper ID ๋์ ๋ฉ์๋ ํธ์ถ๋ก SQL ์คํ |
ํจ๊ณผ |
ํธ์ถ๋ถ ๊ฐ๊ฒฐํ, ์คํ ๋ฐฉ์ง, ์๋ ์์ฑ ๊ฐ๋ฅ |
ํต์ฌ ์์ |
@Mapper , StudentMapper.java , StudentMapper.xml , namespace ์ผ์น |
โ
1. ๊ธฐ์กด ๋ฐฉ์์ ๋จ์ (SqlSession ์ง์ ์ฌ์ฉ ๋ฐฉ์)
session.selectList("student.getAllStudents");
"student.getAllStudents"
๋ผ๋ ID ๋ฌธ์์ด์ ์๋ชป ์ฐ๋ฉด ์ค๋ฅ ๋ฐ์!
- ์ด๋ค ํ๋ผ๋ฏธํฐ์ธ์ง, ์ด๋ค ๋ฆฌํด๊ฐ์ธ์ง IDE์์ ์ ์ ์์
โ
2. ์ธํฐํ์ด์ค ๊ธฐ๋ฐ ๊ตฌ์กฐ์ ํต์ฌ
ํ์ผ๋ช
|
์ญํ |
StudentMapper.java |
์ธํฐํ์ด์ค๋ก SQL ๋ฉ์๋ ์๊ทธ๋์ฒ ์ ์ |
StudentMapper.xml |
XML๋ก ์ค์ ์ฟผ๋ฆฌ ์ ์ (namespace ๊ฐ ์ธํฐํ์ด์ค ๊ฒฝ๋ก์ ๊ฐ์์ผ ํจ) |
โ
3. ๊ตฌ์กฐ ๊ตฌํ ์ค์ต
๐น (1) Mapper ์ธํฐํ์ด์ค: dao/StudentMapper.java
package dao;
import java.util.List;
import main.Student;
public interface StudentMapper {
List<Student> getAllStudents(); // SELECT *
Student getStudentByHakbun(int hakbun); // SELECT WHERE
int insertStudent(Student s); // INSERT
int updateStudentGradeAndPhone(Student s); // UPDATE
int deleteStudentByHakbun(int hakbun); // DELETE
}
๐น (2) Mapper XML: mapper/StudentMapper.xml
<?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="dao.StudentMapper"> <!-- โ
์ธํฐํ์ด์ค์ ์๋ฒฝํ ์ผ์นํด์ผ ํจ -->
<select id="getAllStudents" resultType="main.Student">
SELECT * FROM student
</select>
<select id="getStudentByHakbun" parameterType="int" resultType="main.Student">
SELECT * FROM student WHERE hakbun = #{hakbun}
</select>
<insert id="insertStudent" parameterType="main.Student">
INSERT INTO student (hakbun, irum, hakgwa, addr, phone, jumin, grade)
VALUES (#{hakbun}, #{irum}, #{hakgwa}, #{addr}, #{phone}, #{jumin}, #{grade})
</insert>
<update id="updateStudentGradeAndPhone" parameterType="main.Student">
UPDATE student SET grade = #{grade}, phone = #{phone}
WHERE hakbun = #{hakbun}
</update>
<delete id="deleteStudentByHakbun" parameterType="int">
DELETE FROM student WHERE hakbun = #{hakbun}
</delete>
</mapper>
โ
4. ํธ์ถ ์์ (์ปจํธ๋กค๋ฌ/์๋น์ค ๋ฑ์์ ์ฌ์ฉ)
SqlSession session = factory.openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
List<Student> list = mapper.getAllStudents();
Student s = mapper.getStudentByHakbun(1001);
Student newS = new Student(2030, "์ ๋ค์ธ", "AI๊ณผ", "์์ธ", "010-2222-3333", "010101-4123456", 2);
mapper.insertStudent(newS);
session.commit();
session.close();
โ
5. ๋งคํผ ์ฐ๊ฒฐ ์กฐ๊ฑด ์์ฝ
ํญ๋ชฉ |
ํ์ ์กฐ๊ฑด |
mapper.xml ์ namespace |
์ธํฐํ์ด์ค FQCN (dao.StudentMapper )์ ์์ ์ผ์น |
๋ฉ์๋๋ช
|
XML์ <select id="..."> ์ ์ ํํ ์ผ์น |
ํ๋ผ๋ฏธํฐ ํ์
|
parameterType ๊ณผ ์ธํฐํ์ด์ค ํ๋ผ๋ฏธํฐ ๋์ผํด์ผ ํจ |
๋ฐํ ํ์
|
resultType ๋๋ resultMap ๊ณผ ์ผ์น |
โ
6. ์ค์ต ์ฒดํฌ๋ฆฌ์คํธ
ํญ๋ชฉ |
์ค๋ช
|
ํ์ธ |
StudentMapper.java ์ XML์ด ์ฐ๊ฒฐ๋์๋๊ฐ? |
โ
|
ย |
namespace ๊ฐ ์ธํฐํ์ด์ค ๊ฒฝ๋ก์ ์์ ์ผ์นํ๋๊ฐ? |
โ
|
ย |
๋ฉ์๋ ์ด๋ฆ = ID ์ด๋ฆ = ํธ์ถ ๋ฉ์๋ ์ด๋ฆ์ธ๊ฐ? |
โ
|
ย |
SqlSession์ผ๋ก getMapper() ๋ก ์ ์ ์๋๋๋๊ฐ? |
โ
|
ย |
โ
์ค๋ฌด ํ
ํญ๋ชฉ |
ํ |
์คํ๋ง ํ๊ฒฝ |
@Mapper , @Autowired StudentMapper mapper ๋ก DI ๊ฐ๋ฅ |
ํ
์คํธ ์ |
mapper = session.getMapper(...) ๊ตฌ์กฐ๋ก ๋จ์ ํ
์คํธ ๊ฐ๋ฅ |
์๋์์ฑ ์ง์ |
IDE์์ Mapper ๋ฉ์๋ ์๋์์ฑ + ํ์
์ถ๋ก ๊ฐ๋ฅ |
์ธํฐํ์ด์ค์ @Select , @Insert ๋ถ์ด๋ฉด XML ์์ด๋ ์ฌ์ฉ ๊ฐ๋ฅ (์ด๋
ธํ
์ด์
๋ฐฉ์) |
ย |
โ
6.2๋จ๊ณ: MyBatis Config ์ค์ ํ์ฅ โ ํ๊ทธ ์ค์ต
(๐ mybatis-config.xml
๊ธฐ์ค, ์ธํฐํ์ด์ค ๊ธฐ๋ฐ ๋งคํผ ๊ตฌ์กฐ์ ๋์)
๐ฏ ํ์ต ๋ชฉํ
ํญ๋ชฉ |
์ค๋ช
|
๋ชฉ์ |
์ธํฐํ์ด์ค ๊ธฐ๋ฐ Mapper๋ฅผ mybatis-config.xml ์ ๋ฑ๋ก |
์ฃผ์ ํ๊ทธ |
<mappers> , <mapper resource="..."> , <mapper class="..."> |
ํจ๊ณผ |
SqlSessionFactory ์์ฑ ์, Mapper XML & Java ์ธํฐํ์ด์ค ์๋ ์ฐ๊ฒฐ ๊ฐ๋ฅ |
โ
1. ์ค์ ์์น: mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- ๐ก ๋ค๋ฅธ ์ค์ (์: ํ๊ฒฝ์ค์ ๋ฑ)์ด ์ฌ ์๋ ์์ -->
<mappers>
<!-- โ
1) XML ๋ฐฉ์์ผ๋ก ๋ฑ๋ก -->
<mapper resource="mapper/StudentMapper.xml"/>
<!-- โ
2) ๋๋ ์ธํฐํ์ด์ค(Java) ๊ธฐ๋ฐ์ผ๋ก ๋ฑ๋ก (Spring ํ๊ฒฝ or MyBatis ์ง์ ์ฌ์ฉ ์) -->
<!-- <mapper class="dao.StudentMapper"/> -->
</mappers>
</configuration>
โ ๏ธ ๋ ๋ค ๋ฑ๋กํ๋ฉด ์ค๋ณต ์๋ฌ!
๋ณดํต์ resource ๋ฐฉ์๋ง ์ฌ์ฉ, ๋๋ ์คํ๋ง์์๋ @MapperScan
์ผ๋ก ๋์ฒด ๊ฐ๋ฅ.
โ
2. ๋ฑ๋ก ๋ฐฉ์ ๋น๊ต ์์ฝ
๋ฑ๋ก ๋ฐฉ์ |
์ค๋ช
|
์ฌ์ฉ ์ |
resource="..." |
XML ๊ธฐ๋ฐ ๋งคํผ ๋ฑ๋ก |
mapper/StudentMapper.xml |
class="..." |
์ธํฐํ์ด์ค ๋ฑ๋ก (์ด๋
ธํ
์ด์
๊ธฐ๋ฐ ๋๋ Spring์ฉ) |
dao.StudentMapper |
โ
3. ํ์ฌ ๊ตฌ์กฐ ๊ธฐ์ค ์์ (mapper
ํด๋ ์์ XML ์์)
โ
์ด ๊ตฌ์กฐ์ ๋ง๋ ์ค์ ์:
๐ฆ mybatisstudy
โฃ ๐ dao
โ โ ๐ StudentMapper.java
โฃ ๐ mapper
โ โ ๐ StudentMapper.xml
โฃ ๐ mybatis-config.xml
<mappers>
<mapper resource="mapper/StudentMapper.xml"/>
</mappers>
โ
4. ์ค์ต ์ฒดํฌ๋ฆฌ์คํธ
ํญ๋ชฉ |
์ค๋ช
|
ํ์ธ |
XML ๋๋ ์ธํฐํ์ด์ค ๊ธฐ๋ฐ์ผ๋ก <mapper> ๊ฐ ๋ฑ๋ก๋์๋๊ฐ |
โ
|
ย |
XML ๊ฒฝ๋ก๊ฐ resource ๊ฒฝ๋ก ๊ธฐ์ค์ผ๋ก ์ ํํ๊ฐ |
โ
|
ย |
mybatis-config.xml ์ด SqlSessionFactory์ ๋ก๋ฉ๋๊ณ ์๋๊ฐ |
โ
|
ย |
์ค๋ณต ๋ฑ๋ก ์์ด 1ํ๋ง ์ ์ธ๋์๋๊ฐ |
โ
|
ย |
โ
์ค๋ฌด ํ
์ํฉ |
ํ |
XML ๊ธฐ๋ฐ ํ๋ก์ ํธ |
<mapper resource="..."/> ๋ฐฉ์ ์ฌ์ฉ |
์คํ๋ง ํ๋ก์ ํธ |
@Mapper + @MapperScan(basePackages = "...") |
์ด๋
ธํ
์ด์
๋ฐฉ์ ์ฌ์ฉ ์ |
<mapper class="..."/> ๋ก๋ ๋์ |
์๋ฌ ๋ฐ์ ์ |
์ค๋ณต ๋ฑ๋ก ๋๋ XML ์์น ์คํ ๊ฐ๋ฅ์ฑ ๋์ |
โ
6.3๋จ๊ณ: ์๋ ๋งคํผ ์ค์บ โ @Mapper ์ด๋
ธํ
์ด์
ํ์ฉ
(๐ Spring ๊ธฐ๋ฐ ํ๋ก์ ํธ๋ฅผ ์ํ ๊ตฌ์กฐ, StudentMapper.java
๊ธฐ์ค)
๐ฏ ํ์ต ๋ชฉํ
ํญ๋ชฉ |
์ค๋ช
|
๋ชฉ์ |
MyBatis Mapper ์ธํฐํ์ด์ค๋ฅผ XML ๋ฑ๋ก ์์ด ์๋ ์ธ์ํ๊ฒ ๋ง๋ค๊ธฐ |
์ฃผ์ ๊ธฐ์ |
@Mapper , @MapperScan , XML ์์ด๋ ์๋ ๊ฐ๋ฅํ ๊ตฌ์กฐ |
์ฅ์ |
๊ฐ๊ฒฐํ ์ค์ , IDE ์๋ ์์ฑ, ์ปดํ์ผ ์์ ์ค๋ฅ ๊ฐ์ง |
โ
1. ๊ธฐ์กด ๋ฐฉ์ vs ์ด๋
ธํ
์ด์
๋ฐฉ์ ๋น๊ต
๋ฐฉ์ |
ํน์ง |
๋ฑ๋ก ์์น |
XML ๋ฐฉ์ (<mappers> ) |
mapper/StudentMapper.xml + ์ค์ ํ์ |
mybatis-config.xml |
์ด๋
ธํ
์ด์
๋ฐฉ์ (@Mapper ) |
์๋ฐ ์ฝ๋๋ก ์ง์ ๋งคํ |
Spring ์ค์ ํด๋์ค ๋๋ @MapperScan |
โ
2. ์๋ ๋งคํผ ์ค์บ ๋ฐฉ์ ์ฌ์ฉ ์
๐น (1) ๋งคํผ ์ธํฐํ์ด์ค์ @Mapper
์ถ๊ฐ
package dao;
import java.util.List;
import main.Student;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface StudentMapper {
List<Student> getAllStudents();
Student getStudentByHakbun(int hakbun);
int insertStudent(Student s);
int updateStudentGradeAndPhone(Student s);
int deleteStudentByHakbun(int hakbun);
}
@Mapper๊ฐ ๋ถ์ ์ธํฐํ์ด์ค๋ Spring์ด ์๋์ผ๋ก ๋งคํผ ๊ฐ์ฒด๋ก ์์ฑํจ.
๐ก XML์ ํจ๊ป ์ฌ์ฉํ๋ ค๋ฉด namespace์ id๊ฐ ์ผ์นํด์ผ ํจ.
๐น (2) ์ค์ ํด๋์ค ๋๋ ๋ฉ์ธ ํด๋์ค์ @MapperScan
๋ฑ๋ก
@SpringBootApplication
@MapperScan(basePackages = "dao") // ๋๋ @Mapper๊ฐ ๋ถ์ ํจํค์ง ์ ์ฒด
public class MyBatisSpringBootApp {
public static void main(String[] args) {
SpringApplication.run(MyBatisSpringBootApp.class, args);
}
}
@MapperScan์ @Mapper๊ฐ ๋ถ์ ์ธํฐํ์ด์ค๋ค์ ์๋ ๋ฑ๋กํด์ฃผ๋ ์ญํ !
โ
3. ์ด๋
ธํ
์ด์
๋ง์ผ๋ก SQL ์์ฑ๋ ๊ฐ๋ฅ (XML ์์ด!)
@Mapper
public interface StudentMapper {
@Select("SELECT * FROM student")
List<Student> getAllStudents();
@Insert("INSERT INTO student (hakbun, irum, hakgwa, grade) VALUES (#{hakbun}, #{irum}, #{hakgwa}, #{grade})")
int insertStudent(Student s);
}
โ ๏ธ ๋จ์ : SQL์ด ์ฝ๋์ ์์ด๊ธฐ ๋๋ฌธ์ ๋ณต์กํ ์ฟผ๋ฆฌ๋ ๊ฐ๋
์ฑ ์ธก๋ฉด์์๋ XML์ด ๋ ์ ๋ฆฌํจ
โ
4. ์ค์ต ์ฒดํฌ๋ฆฌ์คํธ
ํญ๋ชฉ |
์ค๋ช
|
ํ์ธ |
@Mapper ๊ฐ ์ธํฐํ์ด์ค์ ๋ถ์ด ์๋๊ฐ |
โ
|
ย |
@MapperScan ์ด Spring ์ค์ ์ ๋ฑ๋ก๋์๋๊ฐ |
โ
|
ย |
XML ์์ด ์ด๋
ธํ
์ด์
๊ธฐ๋ฐ ์ฟผ๋ฆฌ๊ฐ ์๋ํ๋๊ฐ |
โ
|
ย |
IDE์์ ์๋ ์์ฑ, ์๋ฌ ๊ฐ์ง๊ฐ ๊ฐ๋ฅํ๊ฐ |
โ
|
ย |
โ
์ค๋ฌด ํ
ํญ๋ชฉ |
ํ |
๋จ์ํ ์ฟผ๋ฆฌ |
@Select , @Insert ๋ฑ์ผ๋ก ์ด๋
ธํ
์ด์
๋ฐฉ์ ์ฌ์ฉ ๊ฐ๋ฅ |
๋ณต์กํ SQL, ์กฐ์ธ |
XML์ ์ ํธํจ (resultMap , <sql> , <include> , <foreach> ) |
@MapperScan ์ Application ํด๋์ค ๋๋ ์ค์ ํด๋์ค์ 1ํ ์ ์ธํ๋ฉด ๋จ |
ย |
@Mapper ๋ง ๋ถ์ด๊ณ @MapperScan ์์ผ๋ฉด ์๋ ์ ๋จ (Spring Boot ๊ธฐ์ค) |
ย |