1 ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์ง์ ‘ ๋‹ค๋ฃฐ ๋•Œ ๋ฌธ์ œ์ 

1๏ธโƒฃ ๋ฒˆ๊ฑฐ๋กญ๋‹ค

class User {
	private int id;
    private String name;
    private int age;
    private long salary;
}

์œ„์˜ ๋ฐ์ดํ„ฐ(ํ•„๋“œ)๋ฅผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ €์žฅํ•ด์„œ ๊ด€๋ฆฌํ•ด์•ผํ•œ๋‹ค๋ฉดโ“

  • ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ํ…Œ์ด๋ธ” ์ƒ์„ฑ

  • ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์ง์ ‘ ์ฟผ๋ฆฌ ์ƒ์„ฑ

  • ์ฟผ๋ฆฌ JDBC API ํ†ตํ•ด ์ง์ ‘ ์‹คํ–‰

  • ์ฟผ๋ฆฌ ๊ฒฐ๊ณผ๋กœ ํ•ด๋‹น ๊ฐ์ฒด ์ง์ ‘ ์ƒ์„ฑ

โžก๏ธ ๊ฐœ๋ฐœ์ž ์ž…์žฅ์—์„œ ๋„ˆ๋ฌด ๋ฒˆ๊ฑฐ๋กœ์›€

2๏ธโƒฃ SQL์— ์˜์กด์  โ†’ ๋ณ€๊ฒฝ์— ์ทจ์•ฝ

class User {
	private int id;
    private String name;
    private int age;
    private long salary;
    
    // ์ถ”๊ฐ€!
    private int weight;
}

User์˜ ๋ชธ๋ฌด๊ฒŒ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒˆ๋กœ ๋ฐ›์•„์™€์•ผ ํ•œ๋‹ค๋ฉดโ“

  • ์ฟผ๋ฆฌ๋ฌธ ์ง์ ‘ ์ˆ˜์ • ํ•„์š”

  • ์œ ์ €๊ฐ์ฒด์— ๊ฐ’ ๋„ฃ์–ด์ฃผ๋Š” ๋ถ€๋ถ„ ์ถ”๊ฐ€

ORM(SQL ์ž๋™์ƒ์„ฑ) ์ด ์—†๋Š” ํ™˜๊ฒฝ โ“
โ†’ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง๋ณด๋‹ค, SQL ์ž‘์„ฑ์— ๋” ๋งŽ์€ ๋…ธ๋ ฅ์„ ํ•ด์•ผํ•จ

3๏ธโƒฃ ๊ฐ์ฒด์ง€ํ–ฅ ๋ชจ๋ธ๊ณผ RDBMS ํŒจ๋Ÿฌ๋‹ค์ž„ ๋ถˆ์ผ์น˜๋ฐœ์ƒ

๊ฐ์ฒด๋ฆด๋ ˆ์ด์…˜
๋ฐ€๋„๋ฌธ์ œ๋‹ค์–‘ํ•œ ํฌ๊ธฐ์˜ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Œํ…Œ์ด๋ธ”(row, column) โ†’ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐํƒ€์ž…
์„œ๋ธŒํƒ€์ž…๋ฌธ์ œ์ƒ์†, ๋‹คํ˜•์„ฑ ๊ตฌํ˜„ ์‰ฌ์›€์ƒ์† ๋ฐ ๋‹คํ˜•์ ๊ด€๊ณ„ ํ‘œํ˜„๋ถˆ๊ฐ€
์‹๋ณ„์„ฑ๋ฌธ์ œ๋ ˆํผ๋Ÿฐ์Šค, ์ธ์Šคํ„ด์Šค ๋™์ผ์„ฑ์˜ค์ง PK
๊ด€๊ณ„๋ฌธ์ œ์„œ๋กœ ๊ฐ„ ๊ฐ์ฒด์ฐธ์กฐ โ†’ M : N ๊ฐ€๋Šฅ, ๋ฐฉํ–ฅ์žˆ์ŒM : N ๋ถˆ๊ฐ€๋Šฅ, FK๋กœ ๋ฐ”๋กœ ์กฐํšŒ๊ฐ€๋Šฅ (๋ฐฉํ–ฅ์—†์Œ)
๋ฐ์ดํ„ฐ๋„ค๋น„๊ฒŒ์ด์…˜๋ฌธ์ œ๋งˆ์Œ๋Œ€๋กœ ๋ ˆํผ๋Ÿฐ์Šค ํƒ€๊ณ  ๊ฐ€๋Šฅ๋น„ํšจ์œจ์  (๋งค๋ฒˆ JOIN โ†’ ์„ฑ๋Šฅ๋ฌธ์ œ
  • Java-Spring ์„œ๋ฒ„์™€ RDBMS ๊ฐ„, ํŒจ๋Ÿฌ๋‹ค์ž„ ๋ถˆ์ผ์น˜๊ฐ€ ๋ฐœ์ƒ

2 ORM, JPA์˜ ๋“ฑ์žฅ

1๏ธโƒฃ ORM

๐Ÿ“Œ ORM : ๊ฐ์ฒด ๊ด€๊ณ„ ๋งตํ•‘ ๊ธฐ์ˆ 

  • ํŒจ๋Ÿฌ๋‹ค์ž„ ๋ถˆ์ผ์น˜ ๋ฌธ์ œํ•ด๊ฒฐ โœ…

  • ๋ฐ˜๋ณต์ ์ด๊ณ  ๋ฒˆ๊ฑฐ๋กœ์šด ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‹จ์—์„œ์˜ ์ฟผ๋ฆฌ์ž‘์—… ์ค„์—ฌ์คŒ โœ…

  • SQL ์ž๋™์ƒ์„ฑ

2๏ธโƒฃ JPA

๐Ÿ“Œ JPA(Java Persistence API) : Java ORM ๊ธฐ์ˆ ์— ๋Œ€ํ•œ ํ‘œ์ค€๋ช…์„ธ

  • ์ฟผ๋ฆฌ ์ž๋™์ƒ์„ฑ

  • ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ณ„์ธต โ†’ SQL ์˜์กด๋„ ๋‚ฎ์ถค โžก๏ธ ๋ฒˆ๊ฑฐ๋กœ์šด ์ž‘์—… ๋‹จ์ถ• โญ๏ธ

  • ํŒจ๋Ÿฌ๋‹ค์ž„ ๋ถˆ์ผ์น˜ ๋ฌธ์ œํ•ด๊ฒฐ

  • ์„ฑ๋Šฅ ์ตœ์ ํ™” ์™„๋ฃŒ

  • H2, MySQL, ORACLE โ†’ ์–ธ์–ด๊ฐ€ ๋ชจ๋‘ ๋‹ฌ๋ผ๋„ ์ฝ”๋“œ๋ณ€๊ฒฝ โŒ

  • ๊ด€๊ณ„ํ˜• DB์ด์ž, ํ‘œ์ค€์„ ์ค€์ˆ˜ํ•œ SQL ์ง€์› โ†’ JPA๊ฐ€ ์•Œ์•„์„œ ์ฒ˜๋ฆฌํ•จ


3 Entity ํด๋ž˜์Šค์™€ Annotation

๐Ÿ“Œ @Entity : JPA์—์„œ ํ…Œ์ด๋ธ”์— ๋Œ€์‘ํ•˜๋Š” ํ•˜๋‚˜์˜ Java ํด๋ž˜์Šค

@Entity
public class Account {
	private String username;
    private String password;
}

// username, password โ†’ DB์˜ Column
// username, password : Account Entity ํด๋ž˜์Šค์˜ ํ•„๋“œ
usernamepassword
mallang1234
chunsik5678
  • ํ•˜๋‚˜์˜ Entity ํƒ€์ž…์ƒ์„ฑ โ†’ ํ•˜๋‚˜์˜ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑ

  • @Entity ์–ด๋…ธํ…Œ์ด์…˜ : JPA๋ฅผ ์‚ฌ์šฉํ•ด ํ…Œ์ด๋ธ”๊ณผ ๋งคํ•‘ํ•  ํด๋ž˜์Šค๋ฅผ ๋ถ™์—ฌ์ฃผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜
    (ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ JPA๊ฐ€ ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ๊ด€๋ฆฌํ•จ)

    • Entity : ํ…Œ์ด๋ธ”์ „์ฒด
    • Column : ํ•„๋“œ, ์„ธ๋กœ ์—ด๋ถ€๋ถ„
    • row : Entity ๊ฐ์ฒด, ๊ฐ€๋กœ ํ–‰๋ถ€๋ถ„

4 JPA ์˜ˆ์‹œ

1๏ธโƒฃ Entity Java class

// DB ํ…Œ์ด๋ธ” ์—ญํ• 
// @Entity -> @getter, @setter ๋”ฐ๋กœ ์ง€์ • ์•ˆํ•จ
@Entity
public class User {
	// primary key(๊ธฐ๋ณธํ‚ค) ๋ณ€์ˆ˜ ์„ ์–ธ
    @Id
    // ID๊ฐ€ ์ž๋™์œผ๋กœ ์ƒ์„ฑ ๋ฐ ์ฆ๊ฐ€
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    
    @Column(nullable = false, unique = true)
    private String username;
    
    @Column(nullable = false)
    private String password;
    
    @Column(nullable = false, unique = ture)
    private String email;
    
    @Column(nullable = false)
    @Enumerated(value = EnumType.STRING)
    private UserRoleEnum role;
    
    @Column(unique = true)
    private Long kakaoId;
}

2๏ธโƒฃ DB ํ…Œ์ด๋ธ” ๋ฐ Column

3๏ธโƒฃ ํ…Œ์ด๋ธ”ํ˜•์‹

IDEMAILKAKAO_IDPASSWORDROLEUSERNAME
1mallang@mallang.commallang1234ADMINmallang

5 DB์˜ ์—ฐ๊ด€๊ด€๊ณ„

JavaDatabase
์—ฐ๊ด€๊ด€๊ณ„๊ฐ์ฒด, ๋ ˆํผ๋Ÿฐ์Šคํ…Œ์ด๋ธ”(Entity)์‚ฌ์ด์˜ ๊ด€๊ณ„ ; FK

์ •๋ณด์‚ฌ์ด์˜ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•˜๊ณ  ์ฒ˜๋ฆฌํ•˜๋Š” ๋‘ ๋ฐฉ์‹์˜ ์ฐจ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ์œ„ํ•ด,
JPA โžก๏ธ Java ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒ์—์„œ, ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ํ‘œ์‹œํ•ด์ฃผ๋Š” ์žฅ์น˜๋ฅผ ๊ฐ€์ง€๊ณ ์žˆ์Œ

1๏ธโƒฃ DB ์—ฐ๊ด€๊ด€๊ณ„ ์ „์ œ์กฐ๊ฑด

  • JPA๊ฐ€ ์ œ๊ณตํ•˜๋Š” ์—ฐ๊ด€๊ด€๊ณ„ โ†’ DB์˜ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•จ

  • ๋•Œ๋ฌธ์— DB์˜ ์—ฐ๊ด€๊ด€๊ณ„๋ฅผ ๋จผ์ € ํŒŒ์•…ํ•œ ํ›„, ์„ค๊ณ„

  • DB ์—ฐ๊ด€๊ด€๊ณ„ โ†’ ๋น„์ฆˆ๋‹ˆ์Šค ์š”๊ตฌ์‚ฌํ•ญ์— ๋งž์ถฐ ์ด๋ค„์ง

2๏ธโƒฃ JPA ์—ฐ๊ด€๊ด€๊ณ„

๐Ÿ“Œ JPA ์—ฐ๊ด€๊ด€๊ณ„ : Entity ํด๋ž˜์Šค ํ•„๋“œ ์œ„ โ†’ ์—ฐ๊ด€๊ด€๊ณ„ Annotation ์„ค์ • โ†’ ํ˜•์„ฑ์™„๋ฃŒ

๊ด€๊ณ„Annotation
1 : N (์ผ๋Œ€๋‹ค)@OneToMany
N : 1 (๋‹ค๋Œ€์ผ)@ManyToOne
1 : 1 (์ผ๋Œ€์ผ)@OneToOne
N : M (๋‹ค๋Œ€๋‹ค)@ManytoMany

6 JPA ์ œ๊ณต๊ธฐ๋Šฅ

1๏ธโƒฃ ์ƒํ’ˆEntity ์„ค๊ณ„

@Entity
public class Product extends Timestamped {
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    
    private Long userId;
    
    private String title;
    
    private String image;
    
    private String link;
    
    private int lprice;
    
    private int myprice;
}

2๏ธโƒฃ Repository ์„ค๊ณ„

public interface ProductRepository extends JpaRepository<Product, Long> {
	// JPA๋ ˆํฌ์ง€ํ† ๋ฆฌ -> ์ƒ์†๋ฐ›์Œ
    // <์—ฐ๊ฒฐํ•  Entity ํ…Œ์ด๋ธ”, Id ํƒ€์ž…>
}

3๏ธโƒฃ JPA ์ œ๊ณต๊ธฐ๋Šฅ

// ๐Ÿ“ ์ƒํ’ˆ์ƒ์„ฑ
	Product product = new Product();
    productRepository.save(product); // save() -> ๊ฐ์ฒด DB์— ์ €์žฅ

// ๐Ÿ“ ์ƒํ’ˆ์ „์ฒด์กฐํšŒ
	List<Product> productList = productRepository.findAll();

// ๐Ÿ“ ์ƒํ’ˆ์ „์ฒด๊ฐœ์ˆ˜์กฐํšŒ
	long productCount = productRepository.count();

// ๐Ÿ“ ์ƒํ’ˆ์‚ญ์ œ
	productRepository.delete(product);

4๏ธโƒฃ Idํ•„๋“œ ์™ธ ์ถ”๊ฐ€๊ธฐ๋Šฅ

JpaRepository<Product, Long> {
	// ๐Ÿ“ ํšŒ์›Id -> ์ƒํ’ˆ์กฐํšŒ
    List<Product> findAllByUserId(Long userId);
    
    // ๐Ÿ“ title -> ์ƒํ’ˆ์กฐํšŒ
    List<Product> findByTitle(String title);
    
    // ๐Ÿ“ word๊ฐ€ ํฌํ•จ๋œ ์ƒํ’ˆ -> ์กฐํšŒ
    List<Product> findAllByTitleContaining(String word);
    
    // ๐Ÿ“ ์ตœ์ €๊ฐ€ fromPrice ~ toPrice -> ์ƒํ’ˆ์กฐํšŒ
    List<Product> findAllByLpriceBetween(int fromPrice, int toPrice);
}
profile
๐ŸฑSunyeon-Jeong, mallang developer๐Ÿฐ

0๊ฐœ์˜ ๋Œ“๊ธ€