Skip to content

Latest commit

ย 

History

History
99 lines (71 loc) ยท 6.12 KB

README.md

File metadata and controls

99 lines (71 loc) ยท 6.12 KB

SQL Mapper vs ORM

JDBC (Java Database Connectivity)

JDBC๋Š” DB์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋„๋ก Java์—์„œ ์ œ๊ณตํ•˜๋Š” API์ž…๋‹ˆ๋‹ค.
๋ชจ๋“  Java Data Access ๊ธฐ์ˆ ์˜ ๊ทผ๊ฐ„์œผ๋กœ, ๋ชจ๋“  Persistence Framework๋Š” ๋‚ด๋ถ€์ ์œผ๋กœ JDBC API๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.


Persistence Framework๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•˜๋Š” ๊ณผ์ •์„ ๋„์™€์ฃผ๊ณ  ์ž๋™ํ™”ํ•˜๋Š” ๋งค๊ฐœ ์†Œํ”„ํŠธ์›จ์–ด๋กœ,
๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ•˜๋Š” ์ž๋ฐ” ๊ฐ์ฒด ์ธต๊ณผ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์ธต ์‚ฌ์ด๋ฅผ ๋งค๋„๋Ÿฝ๊ฒŒ ์—ฐ๊ฒฐํ•˜๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
ํฌ๊ฒŒ SQL Mapper์™€ ORM์œผ๋กœ ๋‚˜๋‰ฉ๋‹ˆ๋‹ค.

๐Ÿ’ก ์˜์†์„ฑ Persistence
๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•œ ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋˜๋”๋ผ๋„ ์‚ฌ๋ฆฌ์ง€์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ์˜ ํŠน์„ฑ์œผ๋กœ,
์˜์†์„ฑ์„ ๊ฐ–์ง€ ์•Š๋Š” ๋ฐ์ดํ„ฐ๋Š” ๋‹จ์ง€ ๋ฉ”๋ชจ๋ฆฌ์—์„œ๋งŒ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋˜๋ฉด ๋ชจ๋‘ ์žƒ์–ด๋ฒ„๋ฆฌ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ํŒŒ์ผ ์‹œ์Šคํ…œ, ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ˜น์€ ๊ฐ์ฒด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋“ฑ์„ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์˜๊ตฌ์ ์œผ๋กœ ์ €์žฅํ•˜์—ฌ ์˜์†์„ฑ์„ ๋ถ€์—ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ก Persistence Layer
1๏ธโƒฃ ํ”„๋ ˆ์  ํ…Œ์ด์…˜ ๊ณ„์ธต(Presentation layer), UI๊ณ„์ธต
2๏ธโƒฃ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ณ„์ธต(Application layer), ์„œ๋น„์Šค๊ณ„์ธต
3๏ธโƒฃ ๋น„์ฆˆ๋‹ˆ์Šค ๋…ผ๋ฆฌ ๊ณ„์ธต(Business logic layer), ๋„๋ฉ”์ธ๊ณ„์ธต
4๏ธโƒฃ ๋ฐ์ดํ„ฐ ์ ‘๊ทผ ๊ณ„์ธต(Data access layer), ์˜์†๊ณ„์ธต

SQL Mapper - MyBatis

SQL Mapper๋ž€ Object์™€ SQL์˜ ํ•„๋“œ๋ฅผ ๋งคํ•‘ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒดํ™”ํ•˜๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค.
๊ฐ์ฒด์™€ ํ…Œ์ด๋ธ” ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ๋งคํ•‘ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, SQL๋ฌธ์„ ์ง์ ‘ ์ž‘์„ฑํ•˜๊ณ  ์ฟผ๋ฆฌ ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ๋ฅผ ์–ด๋– ํ•œ ๊ฐ์ฒด์— ๋งคํ•‘ํ• ์ง€ ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
๋Œ€ํ‘œ์ ์œผ๋กœ JdbcTemplate, Mybatis๊ฐ€ ์žˆ์œผ๋ฉฐ, DBMS์— ์ข…์†์ ์ธ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

MyBatis๋Š” ์ž๋ฐ”์—์„œ SQL Mapper๋ฅผ ์ง€์›ํ•ด์ฃผ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ๋กœ, SQL๋ฌธ์„ ์ง์ ‘ ์ž‘์„ฑํ•˜์—ฌ ์ฟผ๋ฆฌ ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ฒดํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
์ฟผ๋ฆฌ๋ฌธ์„ xml๋กœ ๋ถ„๋ฆฌํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๋ณต์žกํ•œ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
๊ทธ๋Ÿฌ๋‚˜ ๊ฐ์ฒด์™€ ์ฟผ๋ฆฌ๋ฌธ์„ ๋ชจ๋‘ ๊ด€๋ฆฌํ•ด์•ผ ํ•˜๋ฉฐ, DB CRUD ๋ฉ”์†Œ๋“œ๋ฅผ ์ง์ ‘ ๋‹ค ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์žˆ์Šต๋‹ˆ๋‹ค.


ORM(Object Relational Mapping) - JPA(Java Persistence API)

ORM์ด๋ž€ Object์™€ DB ํ…Œ์ด๋ธ”์„ ๋งคํ•‘ํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ์ฒดํ™”ํ•˜๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค.
๊ฐœ๋ฐœ์ž๊ฐ€ ๋ฐ˜๋ณต์ ์ธ SQL์„ ์ง์ ‘ ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ๋˜์–ด ํŽธ๋ฆฌํ•˜๋ฉฐ DBMS์— ์ข…์†์ ์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
๋Œ€ํ‘œ์ ์œผ๋กœ JPA๊ฐ€ ์žˆ์œผ๋ฉฐ, ๋ณต์žกํ•œ ์ฟผ๋ฆฌ์˜ ๊ฒฝ์šฐ JPQL์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜ SQL Mapper๋ฅผ ํ˜ผ์šฉํ•˜์—ฌ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

JPA

JPA๋ž€ ์ž๋ฐ” ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๊ด€๊ณ„ํ˜• ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์„ ์ •์˜ํ•œ ์ธํ„ฐํŽ˜์ด์Šค์ž…๋‹ˆ๋‹ค.
์ž๋ฐ” ORM์˜ ๊ธฐ์ˆ  ํ‘œ์ค€์œผ๋กœ, CRUD ๋ฉ”์†Œ๋“œ๋ฅผ ๊ธฐ๋ณธ์œผ๋กœ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
์ฟผ๋ฆฌ๋ฅผ ์ง์ ‘ ๋งŒ๋“ค์ง€ ์•Š์•„๋„ ๋˜๋ฉฐ ๊ฐ์ฒด ์ค‘์‹ฌ์œผ๋กœ ๊ฐœ๋ฐœ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
์ฟผ๋ฆฌ๊ฐ€ ์ˆ˜์ •๋˜์–ด ๋ฐ์ดํ„ฐ ์ •๋ณด๊ฐ€ ๋ฐ”๋€Œ๋ฉด ๊ฐ์ฒด๋งŒ ์ˆ˜์ •ํ•˜๋ฉด ๋˜์–ด ํŽธ๋ฆฌํ•˜์ง€๋งŒ, ๋ณต์žกํ•œ ์ฟผ๋ฆฌ ์ž‘์„ฑ์ด ์–ด๋ ต๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

Spring Data JPA๋Š” Spring์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ชจ๋“ˆ ์ค‘ ํ•˜๋‚˜๋กœ, ๊ฐœ๋ฐœ์ž๊ฐ€ JPA๋ฅผ ๋” ์‰ฝ๊ณ  ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค.
์ด๋Š” JPA๋ฅผ ํ•œ ๋‹จ๊ณ„ ์ถ”์ƒํ™”์‹œํ‚จ Repository๋ผ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ œ๊ณตํ•จ์œผ๋กœ์จ ์ด๋ฃจ์–ด์ง‘๋‹ˆ๋‹ค.
์‚ฌ์šฉ์ž๊ฐ€ Repository ์ธํ„ฐํŽ˜์ด์Šค์— ์ •ํ•ด์ง„ ๊ทœ์น™๋Œ€๋กœ ๋ฉ”์†Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด, Spring์ด ์•Œ์•„์„œ ํ•ด๋‹น ๋ฉ”์†Œ๋“œ ์ด๋ฆ„์— ์ ํ•ฉํ•œ ์ฟผ๋ฆฌ๋ฅผ ๋‚ ๋ฆฌ๋Š” ๊ตฌํ˜„์ฒด๋ฅผ ๋งŒ๋“ค์–ด์„œ Bean์œผ๋กœ ๋“ฑ๋กํ•ด์ค๋‹ˆ๋‹ค.

Hibernate๋Š” JPA๋ผ๋Š” ๋ช…์„ธ์˜ ๊ตฌํ˜„์ฒด์ž…๋‹ˆ๋‹ค.
javax.persistence.EntityManager์™€ ๊ฐ™์€ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ง์ ‘ ๊ตฌํ˜„ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ,
์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด, JPA์™€ Hibernate๋Š” ๋งˆ์น˜ ์ž๋ฐ”์˜ interface์™€ ํ•ด๋‹น interface๋ฅผ ๊ตฌํ˜„ํ•œ class์™€ ๊ฐ™์€ ๊ด€๊ณ„์ž…๋‹ˆ๋‹ค.


์œ„ ์‚ฌ์ง„์€ JPA์™€ Hibernate์˜ ์ƒ์† ๋ฐ ๊ตฌํ˜„ ๊ด€๊ณ„๋ฅผ ๋‚˜ํƒ€๋‚ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
JPA์˜ ํ•ต์‹ฌ์ธ EntityManagerFactory, EntityManager, EntityTransaction์„ Hibernate์—์„œ๋Š”
๊ฐ๊ฐ SessionFactory, Session, Transaction์œผ๋กœ ์ƒ์†๋ฐ›๊ณ  ๊ฐ๊ฐ Impl๋กœ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ’ก JPQL (Java Persistence Query Language)

JPA๋Š” SQL์„ ์ถ”์ƒํ™”ํ•œ JPQL์ด๋ผ๋Š” ๊ฐ์ฒด ์ง€ํ–ฅ ์ฟผ๋ฆฌ ์–ธ์–ด๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ํ…Œ์ด๋ธ”์„ ๋Œ€์ƒ์œผ๋กœ ์ฟผ๋ฆฌ ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ฟผ๋ฆฌํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.
JPQL์€ SQL์„ ์ถ”์ƒํ™”ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ํŠน์ • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค SQL์— ์˜์กดํ•˜์ง€ ์•Š๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ JPQL๊ณผ SQL์˜ ๊ฐ€์žฅ ๋šœ๋ ทํ•œ ์ฐจ์ด์ ์€ JPQL์€ ์—”ํ‹ฐํ‹ฐ ๊ฐ์ฒด๋ฅผ ๋Œ€์ƒ์œผ๋กœ ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•˜๋ฉฐ, SQL์€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ”์„ ๋Œ€์ƒ์œผ๋กœ ์ฟผ๋ฆฌ๋ฌธ์„ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

String username = "java";
String jpql = "select m from Member m where m.username = :username";

List<Member> result = em.createQuery(query, Member.class).getResultList();

JPQL์€ ๊ธฐ๋ณธ ๋ฌธ์ž์—ด๋กœ ์ž‘์„ฑ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ปดํŒŒ์ผ ์‹œ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๋‹จ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.
๋˜ํ•œ ๋™์ ์œผ๋กœ ์ฟผ๋ฆฌ ์–ธ์–ด๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๋ฐ ํšจ์œจ์ ์ด์ง€ ๋ชปํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ’ก QueryDSL

QueryDSL์ด๋ž€ ์ •์  ํƒ€์ž…์„ ์ด์šฉํ•ด์„œ SQL, JPQL์„ ์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ฃผ๋Š” ์˜คํ”ˆ์†Œ์Šค ๋นŒ๋” API์ž…๋‹ˆ๋‹ค.
ํ•˜์ด๋ฒ„๋„ค์ดํŠธ ์ฟผ๋ฆฌ ์–ธ์–ด(HQL: Hibernate Query Language)์˜ ์ฟผ๋ฆฌ๋ฅผ ํƒ€์ž…์— ์•ˆ์ „ํ•˜๊ฒŒ ์ƒ์„ฑ ๋ฐ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ์ž…๋‹ˆ๋‹ค.
JPQL์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ๋“ฑ์žฅํ•˜์—ฌ ์ปดํŒŒ์ผ ์‹œ ์˜ค๋ฅ˜๋ฅผ ๋ฐœ๊ฒฌํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€๋Šฅํ•˜๋ฉฐ, ๋ณต์žกํ•˜๊ณ  ๋™์ ์ธ ์ฟผ๋ฆฌ ์ž‘์„ฑ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

String username = "java";

List<Member> result = queryFactory
        .select(member)
        .from(member)
        .where(usernameEq(username))
        .fetch();