** ๐โ 1.1๋จ๊ณ: MyBatis๋? & ๋์ ํ๋ฆ **
๐ก MyBatis๋?
SQL์ XML์ ์์ฑํด์ Java ์ฝ๋์ ๋ถ๋ฆฌํ๊ณ , ์คํ ๊ฒฐ๊ณผ๋ฅผ Java ๊ฐ์ฒด์ ์๋ ๋งคํํด์ฃผ๋ ํ๋ ์์ํฌ์ผ.
๐ ํต์ฌ ํค์๋:
- ์ง์ SQL ์์ฑ โ ์์ ๋ ๋งค์ฐ ๋์
- resultType/resultMap โ Java ๊ฐ์ฒด์ ์๋ ๋งคํ
- JDBC๋ณด๋ค ์ฝ๋ ์ ์ ๊ณ ์ ์ง๋ณด์ ์ฌ์
๐งฑ ์ ์ฒด ํ๋ฆ: ๋ค ํ๋ก์ ํธ ๊ธฐ์ค ๋์ ์์
์คํ ํ์ผ์ธ Test1_A.java
์์ ์๋ํ๋ MyBatis์ ์ ์ฒด ์ฒ๋ฆฌ ํ๋ฆ์ ์ ๋ฆฌํ๋ฉด ๋ค์๊ณผ ๊ฐ์:
1๏ธโฃ mybatis-config.xml ์ค์ ์ฝ๊ธฐ
โ
2๏ธโฃ SqlSessionFactory ์์ฑ (์ค์ ํ์ผ ๊ธฐ๋ฐ)
โ
3๏ธโฃ SqlSession ์์ฑ (์ฟผ๋ฆฌ ์คํ ๊ฐ์ฒด)
โ
4๏ธโฃ Mapper XML(SQL) ํธ์ถ
โ
5๏ธโฃ ๊ฒฐ๊ณผ๋ฅผ Student ๊ฐ์ฒด๋ก ์๋ ๋งคํ
โ
6๏ธโฃ ์ธ์
๋ซ๊ธฐ
๐ ์ค์ ์ฝ๋ ๋ถ์: Test1_A.java
package test0415;
import java.io.Reader;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import main.Student;
public class Test1_A {
public static void main(String[] args) throws Exception {
// 1๏ธโฃ ์ค์ ํ์ผ(xml) ์ฝ๊ธฐ
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
// 2๏ธโฃ ์ค์ ๊ธฐ๋ฐ์ผ๋ก SqlSessionFactory ์์ฑ
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
// 3๏ธโฃ SqlSession (DB ์ฐ๊ฒฐ + SQL ์คํ ๋ด๋น ๊ฐ์ฒด) ์์ฑ
SqlSession session = factory.openSession();
// 4๏ธโฃ Mapper ํธ์ถ โ SQL ์คํ (๋จ์ผ ๊ฐ ๋ฐํ)
int count = session.selectOne("student.getStudentCount");
System.out.println("์ ์ฒด ํ์ ์ : " + count);
// 5๏ธโฃ Mapper ํธ์ถ โ SQL ์คํ (๋ฆฌ์คํธ ๋ฐํ + DTO ๋งคํ)
List<Student> list = session.selectList("student.getAllStudents");
for (Student s : list) {
System.out.println(s.getHakbun() + " / " + s.getIrum() + " / " + s.getHakgwa());
}
// 6๏ธโฃ ์ธ์
๋ซ๊ธฐ
session.close();
}
}
๐ ์ค์ ํ์ผ: mybatis-config.xml
<configuration> <!-- MyBatis ์ ์ฒด ์ค์ ์์ -->
<environments default="development"> <!-- DB ํ๊ฒฝ ๋ชฉ๋ก, ๊ธฐ๋ณธ์ development -->
<environment id="development"> <!-- ๊ฐ๋ฐ์ฉ DB ์ค์ -->
<transactionManager type="JDBC"/> <!-- JDBC ๋ฐฉ์ ํธ๋์ญ์
-->
<dataSource type="POOLED"> <!-- ์ปค๋ฅ์
ํ ์ฌ์ฉ -->
<property name="driver" value="org.mariadb.jdbc.Driver"/> <!-- ๋๋ผ์ด๋ฒ ํด๋์ค -->
<property name="url" value="jdbc:mariadb://localhost:3306/mybatisdb"/> <!-- DB ์ฃผ์ -->
<property name="username" value="root"/> <!-- DB ๊ณ์ -->
<property name="password" value="1234"/> <!-- DB ๋น๋ฐ๋ฒํธ -->
</dataSource>
</environment>
</environments>
<mappers> <!-- Mapper ํ์ผ ๋ฑ๋ก -->
<mapper resource="StudentMapper1.xml"/> <!-- SQL ์ ์๋ XML -->
</mappers>
</configuration> <!-- ์ค์ ๋ -->
๐ ํต์ฌ ํฌ์ธํธ:
- DB ์ ์ ์ ๋ณด:
driver
,url
,username
,password
- Mapper ๋ฑ๋ก:
StudentMapper1.xml
๐งพ SQL Mapper ํ์ผ: StudentMapper1.xml
<!-- โ
์ด ํ์ผ์ MyBatis์ Mapper XML๋ก, SQL ๋ฌธ์ฅ์ ์ ์ํ๊ณ Java ๊ฐ์ฒด์ ๋งคํํ๋ ์ญํ ์ ํจ -->
<mapper namespace="student">
<!-- โ
namespace: ์ด ๋งคํผ๋ฅผ Java์์ ์๋ณํ ์ ์๋ ๊ณ ์ ์ด๋ฆ -->
<!-- ์: sqlSession.selectList("student.getAllStudents")์ฒ๋ผ ์ฌ์ฉ๋จ -->
<!-- ๋ฐ๋์ Java ์ฝ๋์์ ์ฌ์ฉํ ์ด๋ฆ๊ณผ ์ผ์นํด์ผ ํจ -->
<!-- โ
์ฒซ ๋ฒ์งธ SQL ์ ์ ์์ญ ์์ -->
<select id="getStudentCount" resultType="int">
<!-- โ
id="getStudentCount": ์ด SQL์ Java์์ ํธ์ถํ ๋ ์ฌ์ฉํ ์ด๋ฆ -->
<!-- โ
resultType="int": ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๊ฐ ์ ์(int)ํ์ผ๋ก ๋ฐํ๋๋ค๋ ๋ป -->
SELECT COUNT(*) FROM student
<!-- โ
SQL ๋ด์ฉ: student ํ
์ด๋ธ์ ์ ์ฒด ํ ์(๋ ์ฝ๋ ์)๋ฅผ ๊ณ์ฐํ๋ ์ฟผ๋ฆฌ -->
<!-- ์: SELECT COUNT(*)๋ ์ด ๋ช ๋ช
์ ํ์์ด ์๋์ง ์ซ์ ํ๋๋ฅผ ๋ฐํํจ -->
</select>
<!-- โ
๋ ๋ฒ์งธ SQL ์ ์ ์์ญ ์์ -->
<select id="getAllStudents" resultType="main.Student">
<!-- โ
id="getAllStudents": ์ด SQL์ Java์์ ํธ์ถํ ๋ ์ฌ์ฉํ ์ด๋ฆ -->
<!-- โ
resultType="main.Student": ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๊ฐ main ํจํค์ง์ Student ํด๋์ค์ ์๋ ๋งคํ๋จ -->
SELECT * FROM student
<!-- โ
SQL ๋ด์ฉ: student ํ
์ด๋ธ์ ๋ชจ๋ ์ปฌ๋ผ๊ณผ ๋ชจ๋ ๋ ์ฝ๋๋ฅผ ์กฐํํ๋ ์ฟผ๋ฆฌ -->
<!-- โ
์ด ๊ฒฐ๊ณผ๋ ์ฌ๋ฌ ๊ฐ์ ํ์ ์ ๋ณด๋ฅผ ๋ด๊ณ ์์ผ๋ฉฐ, ๊ฐ ํ์ Student ๊ฐ์ฒด๋ก ์๋ ๋ณํ๋จ -->
<!-- โ
์๋ ๋งคํ ์กฐ๊ฑด: DB ์ปฌ๋ผ๋ช
๊ณผ Student ํด๋์ค์ ๋ณ์๋ช
์ด ๋์ผํด์ผ ์๋์ผ๋ก ํ๋๊ฐ ์ฑ์์ง -->
<!-- ์: DB์ name โ Student ํด๋์ค์ setName() ๋ฉ์๋ ํธ์ถ๋จ -->
</select>
</mapper>
ํญ๋ชฉ | ์ค๋ช |
---|---|
namespace="student" |
ํธ์ถ ์ "student.getStudentCount" ํํ๋ก ์ฌ์ฉ๋จ |
resultType="int" |
์ฒซ ์ฟผ๋ฆฌ๋ ๋จ์ผ ์ซ์ ๋ฐํ (ํ์ ์) |
resultType="main.Student" |
๋ ๋ฒ์งธ ์ฟผ๋ฆฌ๋ Student ๊ฐ์ฒด๋ก ์๋ ๋งคํ |
๐ฆ DTO ํด๋์ค: main.Student.java
package main;
public class Student {
private int hakbun;
private String irum;
private String hakgwa;
private String addr;
private String phone;
private String jumin;
private int grade;
// Getter/Setter ์๋ต
}
๐ DB ์ปฌ๋ผ๋ช ๊ณผ ํ๋๋ช ์ด ๊ฐ์ผ๋ฉด ์๋์ผ๋ก ๋งคํ๋จ
์: SELECT * FROM student
โ hakbun
, irum
, hakgwa
โ ์๋ ์ฃผ์
๐ง ๊ตฌ์กฐ ์๊ฐํ
๐ Test1_A.java
โ
๐ mybatis-config.xml ๋ก๋ฉ
โ
โ SqlSessionFactory ์์ฑ
โ
๐ SqlSession ์ด๊ธฐ
โ
๐งพ StudentMapper1.xml์ SQL ์คํ
โ
๐ฆ ๊ฒฐ๊ณผ๋ฅผ Student ๊ฐ์ฒด๋ก ๋ณํ
โ
๐ ์ฝ์ ์ถ๋ ฅ ํ ์ข
๋ฃ
โ ์คํ ๊ฒฐ๊ณผ ์์
์ฝ์ ์ถ๋ ฅ ์์ (DB์ 6๋ช ์๋ค๊ณ ๊ฐ์ ):
์ ์ฒด ํ์ ์ : 6
1001 / ๊น๋ฏผ์ / ์ปดํจํฐ๊ณตํ๊ณผ
1002 / ์ด์ํฌ / ์ ์๊ณตํ๊ณผ
...
โ ์ ๋ฆฌ ์์ฝ
์์ | ์ญํ |
---|---|
mybatis-config.xml |
DB ์ฐ๊ฒฐ + Mapper ๋ฑ๋ก |
StudentMapper1.xml |
์ค์ SQL์ด ์ ์๋ XML |
Test1_A.java |
MyBatis ์คํ ์ฝ๋ |
Student.java |
SQL ๊ฒฐ๊ณผ๋ฅผ ๋ด์ ๊ฐ์ฒด (DTO) |
โ 1.2๋จ๊ณ: SqlSessionFactoryBuilder + ์ค์ ๋ก๋ฉ ๊ตฌ์กฐ (๐ Test1_A.java ๊ธฐ์ค)
๐ ํต์ฌ ํ๋ฆ ์์ฝ (๋์ ์ฝ๋ ๊ธฐ์ค)
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = factory.openSession();
์์ | ์ค์ ๋์ | ๊ด๋ จ ํ์ผ |
---|---|---|
1๏ธโฃ | ์ค์ ํ์ผ ๋ก๋ฉ (Reader) | mybatis-config.xml |
2๏ธโฃ | ์ค์ ์ ํ์ฑํ๊ณ ํฉํ ๋ฆฌ ์์ฑ | ๋ด๋ถ์ ์ผ๋ก XML์ ์ฝ์ด SqlSessionFactory ์์ฑ |
3๏ธโฃ | ํฉํ ๋ฆฌ์์ ์ธ์ ์ ์ด์ด DB ์ฐ๊ฒฐ | session ๊ฐ์ฒด๋ ์ค์ DB ์ฐ๊ฒฐ๋ ์ํ |
๐ ์ค์ ์ฝ๋ ๋ถ์: Test1_A.java
package test0415;
import java.io.Reader;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import main.Student;
public class Test1_A {
public static void main(String[] args) throws Exception {
// ์ค์ ํ์ผ(mybatis-config.xml) ์ฝ๊ธฐ
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
// SqlSessionFactory ์์ฑ
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
// SqlSession ์ด๊ธฐ (DB ์ฐ๊ฒฐ)
SqlSession session = factory.openSession();
// ํ์ ์ ์กฐํ (์ ์ 1๊ฐ ๋ฐํ)
int count = session.selectOne("student.getStudentCount");
System.out.println("์ ์ฒด ํ์ ์ : " + count);
// ์ ์ฒด ํ์ ๋ชฉ๋ก ์กฐํ
List<Student> list = session.selectList("student.getAllStudents");
// ๊ฒฐ๊ณผ ์ถ๋ ฅ
for (Student s : list) {
System.out.println(s.getHakbun() + " / " + s.getIrum() + " / " + s.getHakgwa());
}
// ์ธ์
์ข
๋ฃ
session.close();
}
}
๐ ์ค์ ํ์ผ ๋ถ์: mybatis-config.xml
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="org.mariadb.jdbc.Driver"/>
<property name="url" value="jdbc:mariadb://localhost:3306/mybatisdb"/>
<property name="username" value="root"/>
<property name="password" value="1234"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="StudentMapper1.xml"/>
</mappers>
</configuration>
๐ง ์ค์ โ ์คํ ํ๋ฆ ๊ตฌ์กฐ๋
๐ mybatis-config.xml
โโ DB ์ ์ ์ค์
โโ Mapper ๋ฑ๋ก
โ
๐ Resources.getResourceAsReader() โ XML ํ์ผ ์ฝ๊ธฐ
โ
๐ SqlSessionFactoryBuilder.build() โ Reader โ XML ํ์ฑ
โ
โ SqlSessionFactory โ DB ์ธ์
์์ฑ ๊ณต์ฅ
โ
๐ factory.openSession() โ ์ค์ DB ์ฐ๊ฒฐ ๊ฐ์ฒด(SqlSession)
โ
๐งพ session.selectOne() / selectList()
โ ํต์ฌ ๊ฐ๋ ์์ฝ
๊ตฌ์ฑ ์์ | ์ค๋ช | ์ค์ ์ฌ์ฉ ํ์ผ |
---|---|---|
Resources.getResourceAsReader |
์ค์ ํ์ผ(xml)์ ์ฝ์ด์ Reader ๊ฐ์ฒด๋ก ๋ณํ |
mybatis-config.xml |
SqlSessionFactoryBuilder |
์ค์ ์ ๋ฐํ์ผ๋ก MyBatis ์ด๊ธฐํ ๊ฐ์ฒด ์์ฑ | Test1_A.java |
SqlSessionFactory |
์ธ์ (SqlSession)์ ๋ง๋๋ ๊ณต์ฅ ๊ฐ์ฒด | ๋ด๋ถ ์ฌ์ฉ |
SqlSession |
์ฟผ๋ฆฌ ์คํ ๊ฐ์ฒด (select, insert ๋ฑ) | Test1_A.java |
๐งช ์ค์ต ๊ฒฐ๊ณผ ์์
์คํ ์ ๋ค์๊ณผ ๊ฐ์ ์ฝ์ ์ถ๋ ฅ์ด ๋์์ผ ์ ์ ์๋:
์ ์ฒด ํ์ ์ : 6
1001 / ๊น๋ฏผ์ / ์ปดํจํฐ๊ณตํ๊ณผ
1002 / ์ด์ํฌ / ์ ์๊ณตํ๊ณผ
...
๐งฏ ์์ฃผ ๋ฐ์ํ๋ ์ค๋ฅ
์ค๋ฅ ๋ฉ์์ง | ์์ธ | ํด๊ฒฐ ๋ฐฉ๋ฒ |
---|---|---|
IOException: Could not find resource |
mybatis-config.xml ๊ฒฝ๋ก ์ค๋ฅ |
src ํ์ ๋๋ classpath ํ์ธ |
ClassNotFoundException: org.mariadb.jdbc.Driver |
JAR ํ์ผ ๋๋ฝ | WEB-INF/lib ํ์ธ + Build Path ๋ฑ๋ก |
SQLException: Access denied |
DB ๊ณ์ /๋น๋ฐ๋ฒํธ ์ค๋ฅ | root ๋น๋ฒ ํ์ธ, ์ฌ์ฉ์ ๊ถํ ๋ถ์ฌ |
โ ์ต์ข ์ ๋ฆฌ
SqlSessionFactoryBuilder๋ MyBatis์์ ์ค์ ํ์ผ์ ๋ถ์ํด์ ์ฟผ๋ฆฌ๋ฅผ ์คํํ ์ ์๋ ์ธ์ ํฉํ ๋ฆฌ(SqlSessionFactory) ๋ฅผ ๋ง๋ค์ด์ฃผ๋ ๋ถํธ์คํธ๋ฉ ์์ง์ด์ผ.
์ด ์ค์ ์ ๋ฐ๋ผ ๋์ค์
selectOne
,selectList
๊ฐ์ ๋ฉ์๋๊ฐ ์ ์ ๋์ํ ์ ์์ด.
โ 1.3๋จ๊ณ: selectOne vs selectList ์ค์ ์ฌ์ฉ๋ฒ (๐ mybatisstudy ๊ธฐ๋ฐ)
๐ ๊ธฐ์ค ์คํํ์ผ: Test1_A.java
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = factory.openSession();
// ๐ธ selectOne: 1๊ฐ์ ๊ฒฐ๊ณผ๋ง ๋ฐํ
int count = session.selectOne("student.getStudentCount");
System.out.println("์ ์ฒด ํ์ ์ : " + count);
// ๐ธ selectList: ์ฌ๋ฌ ๊ฐ์ ๋ ์ฝ๋๋ฅผ ๋ฆฌ์คํธ๋ก ๋ฐํ
List<Student> list = session.selectList("student.getAllStudents");
for (Student s : list) {
System.out.println(s.getHakbun() + " / " + s.getIrum() + " / " + s.getHakgwa());
}
โ selectOne()
์ฌ์ฉ๋ฒ
๐ ์ฌ์ฉ๋ ์ฟผ๋ฆฌ: StudentMapper1.xml
<select id="getStudentCount" resultType="int">
SELECT COUNT(*) FROM student
</select>
๐ ๋ถ์
ํญ๋ชฉ | ์ค๋ช |
---|---|
id="getStudentCount" |
Java์์ student.getStudentCount ๋ก ํธ์ถ |
resultType="int" |
๊ฒฐ๊ณผ๋ ์ซ์ ํ๋ (ํ์ ์) |
์คํ ์ฝ๋ | session.selectOne("student.getStudentCount") |
โ ํน์ง ์ ๋ฆฌ
ํญ๋ชฉ | ์ค๋ช |
---|---|
๋ฆฌํด ํ์ | ๋จ์ผ ๊ฐ (int , String , DTO ํ ๊ฐ ๋ฑ) |
๊ฒฐ๊ณผ | 1ํ 1์ด๋ง ๋ฐํํด์ผ ํจ |
์์ธ | 2๊ฐ ์ด์ ๊ฒฐ๊ณผ ๋ฐํ ์ TooManyResultsException ๋ฐ์ |
โก selectList()
์ฌ์ฉ๋ฒ
๐ ์ฌ์ฉ๋ ์ฟผ๋ฆฌ: StudentMapper1.xml
<select id="getAllStudents" resultType="main.Student">
SELECT * FROM student
</select>
๐ ๋ถ์
ํญ๋ชฉ | ์ค๋ช |
---|---|
id="getAllStudents" |
Java์์ student.getAllStudents ๋ก ํธ์ถ |
resultType="main.Student" |
๊ฒฐ๊ณผ๋ฅผ Student ๊ฐ์ฒด๋ก ์๋ ๋งคํ |
์คํ ์ฝ๋ | session.selectList("student.getAllStudents") |
โ main.Student ๊ฒฝ๋ก๋ DTO ํด๋์ค๊ฐ src/main/Student.java์ ์๊ธฐ ๋๋ฌธ!
๐ฆ DTO ๋งคํ: Student.java
public class Student {
private int hakbun;
private String irum;
private String hakgwa;
private String addr;
private String phone;
private String jumin;
private int grade;
// Getter/Setter ์๋ต
}
๐ ์๋ ๋งคํ ์๋ฆฌ
-
MyBatis๋ DB ์ปฌ๋ผ๋ช ๊ณผ DTO ํ๋๋ช ์ด ๋์ผํ๋ฉด ์๋ ๋งคํํด ์ค
์:
hakbun
,irum
,hakgwa
โ Student ๊ฐ์ฒด์ ์๋ ์ฃผ์
โ
selectOne
vs selectList
์ฐจ์ด ์์ฝ
ํญ๋ชฉ | selectOne() |
selectList() |
---|---|---|
๋ฆฌํด ํ์ | ๋จ์ผ ๊ฐ์ฒด or ๊ธฐ๋ณธํ | List<๊ฐ์ฒด> |
์์ ์ฟผ๋ฆฌ | SELECT COUNT(*) FROM ... |
SELECT * FROM ... |
๋งคํ ๋์ | ๊ธฐ๋ณธํ or DTO ํ๋ | DTO ๋ชฉ๋ก |
์์ธ ๋ฐ์ ์กฐ๊ฑด | ๊ฒฐ๊ณผ 2๊ฐ ์ด์์ด๋ฉด ์ค๋ฅ | 0๊ฐ์ฌ๋ ์ ์ ์๋ |
๐งช ์ฝ์ ์ถ๋ ฅ ์์
์ ์ฒด ํ์ ์ : 6
1001 / ๊น๋ฏผ์ / ์ปดํจํฐ๊ณตํ๊ณผ
1002 / ์ด์ํฌ / ์ ์๊ณตํ๊ณผ
...
๐ ํ์ผ ๊ธฐ์ค ์ฌ์ฉ ์ ๋ฆฌ
์ฌ์ฉ๋ XML ํ์ผ | SQL ID | Java ํธ์ถ ์ฝ๋ | ๋ฆฌํด ํ์ |
---|---|---|---|
StudentMapper1.xml |
getStudentCount |
selectOne("student.getStudentCount") |
int |
StudentMapper1.xml |
getAllStudents |
selectList("student.getAllStudents") |
List<Student> |
โ ์ค์ต ํ์ธ ์ฒดํฌ๋ฆฌ์คํธ
ํญ๋ชฉ | ํ์ธ ์ฌ๋ถ |
---|---|
selectOne() โ COUNT ์ฟผ๋ฆฌ์์ ์ ์ ์๋ |
โ |
selectList() โ DTO ๋ฆฌ์คํธ๋ก ์ ์ ๋งคํ |
โ |
์ฝ์ ์ถ๋ ฅ ํ์ธ | โ |
DB ์ปฌ๋ผ๋ช โ DTO ํ๋๋ช ์ผ์น ์ฌ๋ถ | โ |
โ ์ ๋ฆฌ ์์ฝ
selectOne()์ ๋จ์ผ ๊ฐ ๋ฐํ์๋ง ์ฌ์ฉํ๊ณ ,
selectList()
๋ ์ฌ๋ฌ ํ์List<DTO>
๋ก ๋ฐํํด์ for๋ฌธ ๋ฑ์ผ๋ก ๋ฐ๋ณต ์ฒ๋ฆฌํ ์ ์์ด.
๋ ๋ค Mapper XML์ id
์ ์ ํํ ์ผ์นํด์ผ ํ๋ฉฐ,
resultType์ด ์ ํํ DTO ๋๋ ๊ธฐ๋ณธํ๊ณผ ๋งค์นญ๋๋๋ก ํด์ผ ์ค๋ฅ๊ฐ ์ ๋.
โ 1.4๋จ๊ณ: ๋งคํผ ํ์ผ ๊ตฌ์ฑ
โ ๋จผ์ ๋งคํผ ํ์ผ์ด๋?
MyBatis์์ SQL์ ์ ์ํด๋๋ XML ํ์ผ์ด๋ฉฐ,
Java ์ฝ๋์์ ์ค์ ๋ก ์คํํ ์ ์๋๋ก ์ฟผ๋ฆฌ ID, ๊ฒฐ๊ณผ ๋งคํ ๋ฐฉ์, ํ๋ผ๋ฏธํฐ ์ฒ๋ฆฌ ๋ฐฉ์ ๋ฑ์ ์์ฑํ๋ ๊ณณ์ด์ผ.
๐ ์ค์ ๋งคํผ ํ์ผ: StudentMapper1.xml
(ํ์ผ์์ ์ถ์ถ)
<?xml version="1.0" encoding="UTF-8" ?>
<!-- โ
์ด XML์ MyBatis 3.0์ฉ ๋งคํผ ๋ฌธ์์์ ๋ช
์ -->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- โ
์ด ๋งคํผ์ ์๋ณ ์ด๋ฆ(namespace). Java์์ ํธ์ถ ์ ์ฌ์ฉ -->
<mapper namespace="student">
<!-- โ
1. ์ ์ฒด ํ์ ์๋ฅผ ์กฐํํ๋ SQL -->
<!-- id="getStudentCount": Java ์ฝ๋์์ ์ฌ์ฉํ ์ฟผ๋ฆฌ ์ด๋ฆ -->
<!-- resultType="int": ๊ฒฐ๊ณผ๊ฐ ์ ์ ํ๋ (COUNT) -->
<select id="getStudentCount" resultType="int">
SELECT COUNT(*) FROM student
<!-- โ
student ํ
์ด๋ธ์ ์๋ ๋ชจ๋ ํ์ ์(ํ์ ์)๋ฅผ ๊ฐ์ ธ์ด -->
</select>
<!-- โ
2. ์ ์ฒด ํ์ ๋ชฉ๋ก์ ์กฐํํ๋ SQL -->
<!-- resultType="main.Student": ๊ฐ ํ์ด Student ๊ฐ์ฒด๋ก ๋งคํ๋จ -->
<select id="getAllStudents" resultType="main.Student">
SELECT * FROM student
<!-- โ
๋ชจ๋ ํ์ ์ ๋ณด๋ฅผ ์กฐํํ์ฌ List<Student> ํํ๋ก ๋ฐํ -->
</select>
<!-- โ
3. 1ํ๋
ํ์๋ง ์กฐํํ๋ SQL -->
<select id="getGrade1Students" resultType="main.Student">
SELECT * FROM student WHERE grade = 1
<!-- โ
grade ์ปฌ๋ผ์ด 1์ธ (์ฆ, 1ํ๋
) ํ์๋ง ๊ฐ์ ธ์ด -->
</select>
<!-- โ
4. ์ด๋ฆ์ด '๊น'์ผ๋ก ์์ํ๋ ํ์๋ง ์กฐํํ๋ SQL -->
<select id="getKimStudents" resultType="main.Student">
SELECT * FROM student WHERE irum LIKE '๊น%'
<!-- โ
irum(์ด๋ฆ) ์ปฌ๋ผ์ด '๊น'์ผ๋ก ์์ํ๋ ๊ฒฝ์ฐ๋ง ์กฐํ -->
<!-- โ
'๊น%'๋ '๊นOO', '๊น์ฒ ์', '๊นํ๋' ๋ฑ ๋ชจ๋ ๊น์จ๋ฅผ ์๋ฏธํจ -->
</select>
<!-- โ
5. ์ฃผ๋ฏผ๋ฒํธ๋ก ์ฌํ์ ํ๋ณํด์ ์กฐํํ๋ SQL -->
<select id="getFemaleStudents" resultType="main.Student">
SELECT * FROM student
WHERE SUBSTR(jumin, 8, 1) = '2' OR SUBSTR(jumin, 8, 1) = '4'
<!-- โ
jumin(์ฃผ๋ฏผ๋ฒํธ)์์ 8๋ฒ์งธ ์๋ฆฌ๊ฐ ์ฑ๋ณ ์ฝ๋ -->
<!-- โ
'2' ๋๋ '4'๋ ์ฌ์ (1980๋
๋ ์ดํ ์ฃผ๋ฏผ๋ฒํธ ๊ธฐ์ค) -->
<!-- โ
SUBSTR(jumin, 8, 1)์ jumin์์ 8๋ฒ์งธ ๊ธ์ ํ๋๋ฅผ ์ถ์ถ -->
</select>
</mapper>
๐ ๊ตฌ์ฑ ์์๋ณ ์ ๋ฆฌ
๊ตฌ์ฑ ์์ | ์์ | ์ค๋ช |
---|---|---|
namespace |
"student" |
Java ์ฝ๋์์ ํธ์ถ ์ ์ฌ์ฉํ๋ ์ ๋์ฌ |
<select> |
id="getAllStudents" |
Java์์ ํธ์ถํ SQL ์๋ณ์ |
resultType |
"main.Student" |
SQL ์คํ ํ ๊ฒฐ๊ณผ๋ฅผ ์๋ ๋งคํํ DTO ํ์ |
parameterType (์๋ต๋จ) |
์์ โ ํ๋ผ๋ฏธํฐ ์์ด ์คํ๋๋ ์ฟผ๋ฆฌ๋ค | ย |
โ
1. namespace="student"
- ์ด ๋ค์์คํ์ด์ค๋ Java ์ฝ๋์์ ์ฟผ๋ฆฌ๋ฅผ ํธ์ถํ ๋ prefix ์ญํ ์ ํด.
session.selectList("student.getAllStudents");
๐ ์ฌ๊ธฐ์ student
๋ XML์ <mapper namespace="student">
์ ๋งค์นญ๋๋ ๊ฒ!
โ
2. <select>
ํ๊ทธ
๊ฐ SQL ๊ตฌ๋ฌธ์ ์ ์ํ๋ ํต์ฌ ํ๊ทธ๋ก, ์ต์ id
์ resultType
์ ํฌํจํด์ผ ํด.
์: getAllStudents
<select id="getAllStudents" resultType="main.Student">
SELECT * FROM student
</select>
ํญ๋ชฉ | ์ค๋ช |
---|---|
id |
ํธ์ถํ ๋ ์ฌ์ฉํ SQL ์๋ณ์ |
resultType |
๊ฒฐ๊ณผ๋ฅผ ๋งคํํ Java ํด๋์ค (Student ) |
parameterType (์๋ต) |
ํ๋ผ๋ฏธํฐ ์๋ ์ฟผ๋ฆฌ์ด๊ธฐ ๋๋ฌธ์ ์๋ต ๊ฐ๋ฅ |
โ 3. ์ ์ฒด ๋งคํผ ๊ตฌ์กฐ ์๊ฐํ
StudentMapper1.xml
โโโ <mapper namespace="student">
โโโ <select id="getStudentCount" resultType="int">
โโโ <select id="getAllStudents" resultType="main.Student">
โโโ <select id="getGrade1Students" resultType="main.Student">
โโโ ...
โ
4. resultType="main.Student"
์๋ ๋งคํ ์๋ ๋ฐฉ์
- ๊ฒฐ๊ณผ ์ฟผ๋ฆฌ์ ์ปฌ๋ผ๋ช ๊ณผ DTO ํด๋์ค์ ํ๋๋ช ์ด ๋์ผํ ๊ฒฝ์ฐ
- MyBatis๊ฐ ์๋์ผ๋ก
setXXX()
๋ฉ์๋๋ฅผ ํธ์ถํด์ ๊ฐ์ ์ฃผ์ ํด ์ค
SELECT * FROM student
โ DB ๊ฒฐ๊ณผ: hakbun = 1001 โ student.setHakbun(1001)
๐ก ๋์ main.Student.java
ํด๋์ค ๊ตฌ์กฐ๋ ์ด๋ฏธ ๋ค์๊ณผ ๊ฐ์:
public class Student {
private int hakbun;
private String irum;
private String hakgwa;
...
}
์ฆ, resultType="main.Student"
์ ์๋ฒฝํ๊ฒ ๋งคํ๋จ.
โ ์ค์ต ์ ๋ฆฌ: ์ฟผ๋ฆฌ ID๋ณ ๋งคํผ ๊ตฌ์กฐ
SQL ID | ์ค๋ช | ๋ฐํ ํ์ |
---|---|---|
getStudentCount |
์ ์ฒด ํ์ ์ ์กฐํ | int |
getAllStudents |
์ ์ฒด ํ์ ๋ฆฌ์คํธ | List<Student> |
getGrade1Students |
1ํ๋ ํ์ ํํฐ | List<Student> |
getKimStudents |
์ฑ์ด โ๊นโ์จ | List<Student> |
getFemaleStudents |
์ฌํ์ ํ๋ณ | List<Student> |
ย | ย | ย |
โ ๋งคํผ XML ์ ๋ฆฌ ์์ฝํ
ํ๊ทธ | ์ญํ |
---|---|
<!DOCTYPE mapper ...> |
XML ๋ฌธ๋ฒ ์ ํจ์ฑ ์ ์ธ |
<mapper namespace="..."> |
ํธ์ถ ์๋ณ์ ๋ค์์คํ์ด์ค ์ ์ |
<select id="..." resultType="..."> |
SQL ์ ์ + ๊ฒฐ๊ณผ ๋งคํ ํ์ ์ง์ |
โ 1.5๋จ๊ณ: resultType vs resultMap ๋น๊ต (์ค์ ์ฝ๋ ์ค์ฌ)
๐ธ ๋จผ์ ์์ฝ ๋น๊ต๋ถํฐ
๊ตฌ๋ถ | resultType | resultMap |
---|---|---|
ํต์ฌ ์ญํ | DB ๊ฒฐ๊ณผ๋ฅผ Java ๊ฐ์ฒด๋ก ์๋ ๋งคํ | ๋ณต์กํ ๋งคํ, ๋ณ๋ช (alias), ์กฐ์ธ ๊ฒฐ๊ณผ ์๋ ๋งคํ |
์ค์ ๋ฐฉ์ | resultType="main.Student" |
resultMap="studentResultMap" |
์ฌ์ฉ ์กฐ๊ฑด | DB ์ปฌ๋ผ๋ช == Java ํ๋๋ช ์ผ ๋ ์ถ์ฒ | ์ปฌ๋ผ๋ช โ ํ๋๋ช ์ผ ๋ ํ์ |
์ฅ์ | ๋งค์ฐ ๊ฐ๋จํจ (์๋ ๋งคํ) | ๋งคํ ๊ตฌ์กฐ๋ฅผ ์์ธํ ์ ์ ๊ฐ๋ฅ |
๋จ์ | ์ปฌ๋ผ๋ช ๊ณผ ํ๋๋ช ์ด ๋ค๋ฅด๋ฉด ๋งคํ ์คํจ | ์ค์ ์ด ์กฐ๊ธ ๋ ๋ณต์กํจ |
โ
์ค์ต 1: ํ์ฌ ๋์ ๋งคํผ๋ ์ ๋ถ resultType
์ฌ์ฉ ์ค
์: StudentMapper1.xml
<select id="getAllStudents" resultType="main.Student">
SELECT * FROM student
</select>
โ ์ด ๊ฒฝ์ฐ๋ DB ํ ์ด๋ธ ์ปฌ๋ผ๋ช (hakbun, irum, hakgwa, โฆ)๊ณผ
Java DTO ํ๋๋ช
(main.Student
)์ด ์๋ฒฝํ๊ฒ ๋์ผํ๊ธฐ ๋๋ฌธ์ resultType
์ผ๋ก ์ถฉ๋ถํจ.
โ
์ค์ต 2: ์ปฌ๋ผ๋ช
์ด ๋ค๋ฅผ ๊ฒฝ์ฐ โ resultMap
์ด ํ์ํจ
๐ ์์ ์ํฉ: ๋ณ์นญ(alias)์ ์ฐ๊ฑฐ๋, ์กฐ์ธ ๊ฒฐ๊ณผ๋ฅผ ๋งคํํ ๋
SELECT hakbun AS stu_no, irum AS name FROM student
์ด ๊ฒฐ๊ณผ๋ Java์ Student
ํด๋์ค์ ํ๋๋ช
์ด ๋ค๋ฆ
โ ์๋ ๋งคํ ์คํจ! โ resultMap
ํ์
โ ์ค์ต ์ฝ๋ ์์ (ํ์ผ ํ์ฅ์ฉ)
๐ง 1) ๋งคํผ์ resultMap ์ ์ ์ถ๊ฐ
<resultMap id="studentResultMap" type="main.Student">
<result property="hakbun" column="stu_no"/>
<result property="irum" column="name"/>
</resultMap>
resultMap
: ๋งคํ ๊ท์น์ ์ง์ ์ ์property
: Java ํด๋์ค์ ํ๋๋ชcolumn
: SQL ๊ฒฐ๊ณผ์ ์ปฌ๋ผ๋ช ๋๋ ๋ณ์นญ
๐ง 2) select์ resultMap ์ ์ฉ
<select id="getStudentsWithAlias" resultMap="studentResultMap">
SELECT hakbun AS stu_no, irum AS name FROM student
</select>
โ ์ด ์ฟผ๋ฆฌ๋ resultType์ผ๋ก๋ ๋งคํ์ด ๋ถ๊ฐ๋ฅํ๋ฏ๋ก resultMap์ผ๋ก๋ง ๊ฐ๋ฅ!
๐ง 3) Java์์ ํธ์ถํ๋ ์ฝ๋ ์์
List<Student> list = session.selectList("student.getStudentsWithAlias");
for (Student s : list) {
System.out.println(s.getHakbun() + " / " + s.getIrum());
}
โ ์๊ฐ์ ๋น๊ต ์์ฝ
โ resultType ๋ฐฉ์ (ํ์ฌ ์ฌ์ฉ ์ค)
<select id="getAllStudents" resultType="main.Student">
SELECT * FROM student
</select>
- ์๋ ๋งคํ
- ๊ฐ๋จํ์ง๋ง ์ปฌ๋ผ๋ช โ ํ๋๋ช ์ผ ๊ฒฝ์ฐ ์ฌ์ฉ ๋ถ๊ฐ
โ resultMap ๋ฐฉ์ (๋ณต์กํ ๋งคํ์ ํ์)
<resultMap id="studentResultMap" type="main.Student">
<result property="hakbun" column="stu_no"/>
<result property="irum" column="name"/>
</resultMap>
<select id="getStudentsWithAlias" resultMap="studentResultMap">
SELECT hakbun AS stu_no, irum AS name FROM student
</select>
โ ์ ๋ฆฌ ์์ฝ
ํญ๋ชฉ | resultType | resultMap |
---|---|---|
๊ธฐ๋ณธ ๋งคํ ๋ฐฉ์ | ์๋ ๋งคํ | ์๋ ๋ช ์ ๋งคํ |
์ฌ์ฉ ์กฐ๊ฑด | ์ปฌ๋ผ๋ช ๊ณผ DTO ํ๋๋ช ์ด ๊ฐ์ ๋ | ์ปฌ๋ผ๋ช โ ํ๋๋ช / JOIN / alias |
์ฌ์ฉ๋ฒ | ๊ฐ๋จ | ๋ช ์์ ์ค์ ํ์ |
์์ ์ํฉ | SELECT * FROM student | SELECT โฆ AS alias, JOIN ๋ฑ |
โ ์ค์ต ํ์ฅ ๊ณผ์ (๋ค ํ๋ก์ ํธ์ ๋ฐ์ํด๋ณผ ์ ์์)
โ ๋์ ์ค์ต:
StudentMapper1.xml
์ ๋ณ์นญ ์ฟผ๋ฆฌ ์ถ๊ฐresultMap
์์ฑ- Java์์ selectList๋ก ํธ์ถ
- ์ฝ์ ์ถ๋ ฅ๊น์ง ํ์ธ