
application.properties ๋ก DB ์ ์ ์ ๋ณด ๋ฑ๋ก@Entity ํด๋์ค๋ก ์๋ ํ
์ด๋ธ ์์ฑ| ๊ฐ๋ | ์ค๋ช |
|---|---|
| JPA | Java Persistence API. ์๋ฐ ์ง์์ ORM ํ์ค ๋ฌธ๋ฒ |
| Hibernate | JPA ๊ตฌํ์ฒด. ์ฌ์ค์ ์ ๊ณ ํ์ค ORM |
| Entity | @Entity ์ด๋
ธํ
์ด์
์ด ๋ถ์ ํด๋์ค๋ก, ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ํ
์ด๋ธ๊ณผ ์ผ๋์ผ๋ก ๋งคํ๋๋ ์๋ฐ ๊ฐ์ฒด |
| @Id | ๊ธฐ๋ณธ ํค(Primary Key) ์ง์ |
| @GeneratedValue | ์๋ ์ฆ๊ฐ ๊ฐ ๋ถ์ฌ (auto increment) |
| @Column | ์ ์ฝ ์กฐ๊ฑด ๋ถ์ฌ (๊ธธ์ด, null ์ฌ๋ถ, unique ๋ฑ) |
| runtimeOnly | ๋ฐํ์ ์์๋ง ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ (ex: DB ๋๋ผ์ด๋ฒ) |
build.gradle)dependencies {
runtimeOnly 'com.mysql:mysql-connector-j'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
}
(PostgreSQL ์ฌ์ฉ ์)
runtimeOnly 'org.postgresql:postgresql'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
application.properties)spring.datasource.url=jdbc:mysql://์๋ํฌ์ธํธ์ฃผ์:3306/๋ฐ์ดํฐ๋ฒ ์ด์ค๋ช
spring.datasource.username=DB์์ด๋
spring.datasource.password=DB๋น๋ฒ
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.hibernate.ddl-auto=update
ddl-auto=update โ ํ
์ด๋ธ ์๋ ์์ฑ/์์ ๋ฐ์ none ๊ถ์ฅimport jakarta.persistence.*;
@Entity
public class Item {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
public Integer id;
@Column(length = 200, nullable = false, unique = true)
public String title;
public Integer price;
}
@Id + @GeneratedValue = ์๋ ์ฆ๊ฐ PKinsert into item values (1, '์
์ธ ', 7000);
insert into item values (2, '๋ฐ์ง', 5000);
insert into item values (3, '์์ผ', 6000);
@Entity : ํด๋์ค = ํ
์ด๋ธbuild.gradle์ JPA + DB ๋๋ผ์ด๋ฒ ์์กด์ฑ ์ถ๊ฐ application.properties์ ์ ์ ์ ๋ณด ์
๋ ฅ @Entity ํด๋์ค๋ก ํ
์ด๋ธ ์๋ ์์ฑ + ์ ์ฝ์กฐ๊ฑด @Column์ผ๋ก ๋ถ์ฌ