02. JPA(Hibernate) - YAPP-16th/Team_Web_3_Backend GitHub Wiki
- Object-relational mapping (๊ฐ์ฒด ๊ด๊ณ ๋งคํ)
- ๊ฐ์ฒด๋ ๊ฐ์ฒด๋๋ก ์ค๊ณํ๊ณ , ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ ๊ด๊ณํ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋๋ก ์ค๊ณํ๋ค.
- ORM ํ๋ ์์ํฌ๊ฐ ์ค๊ฐ์์ ๋งคํํด์ค๋ค.
์๋ฐ ORM ๊ธฐ์ ํ์ค์ผ๋ก, ์ธํฐํ์ด์ค์ ๋ชจ์์ด๋ค. JPA ์ธํฐํ์ด์ค๋ฅผ ๊ตฌํํ ๋ํ์ ์ธ ์คํ์์ค๊ฐ Hibernate
- ๊ฐ๋ฐ์๊ฐ ์ผ์ผํ CRUD์ฉ ์ฟผ๋ฆฌ๋ฅผ ์์ฑํด์ค์ผํ๋ Mybatis์ ๊ฐ์ Mapper๋ฐฉ์์ ์ปฌ๋ผ์ด ์ถ๊ฐ๋๊ฑฐ๋ํ๋ฉด ์์ ํด์ฃผ์ด์ผํ๋ ๋ถ๋ถ์ด ์๋นํ ๋ง์๋ค. ์ด๋ก ์ธํด์ ์๋ฐ๋ฅผ ์ฌ์ฉํ์ง๋ง ๊ฐ์ฒด์ค์ฌ ๊ฐ๋ฐ์ด ์๋๋ผ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ๋ฆ์ผ๋ก ๊ฐ๋ฐ์ ํ๊ฒ๋๋ ๋ฌธ์ ๊ฐ ์๋ค. JPA๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ฉด ์ฟผ๋ฆฌ๋ฅผ ์ง์ ์์ฑํ๋ ๊ฒ์ด ์๋๊ณ ๋ง๋ค์ด์ง ๊ฐ์ฒด๋ก ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ๋ค๋ฃจ๊ธฐ ๋๋ฌธ์ ๊ฐ์ฒด ์ค์ฌ์ผ๋ก ๊ฐ๋ฐ์ ์งํ
- SQL์ ์ง์ ์ ์ผ๋ก ์์ฑํ์ง ์๊ณ ์ํฐํฐ ํ๋๊ฐ ๋๋ ๊ฐ์ฒด๋ฅผ ๋ค๋ค์ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ๋์์ํค๊ธฐ ๋๋ฌธ์ ์ ์ง๋ณด์๊ฐ ๋์ฑ ๊ฐ๊ฒฐํ๋ค. ์๋ํ๋ฉด ์ฟผ๋ฆฌ๊ฐ ์์ ๋๋ฉด ๊ทธ์ ๋ฐ๋ผ์ ๊ทธ๋ฅผ ๋ด์ DTO ํ๋๋ ๋ชจ๋ ๋ณ๊ฒฝ์ด ๋์ผ ํ์ง๋ง JPA๋ฅผ ์ฌ์ฉํ๊ฒ ๋๋ฉด ๋จ์ํ ์ํฐํฐ ํด๋์ค ์ ๋ณด๋ง ๋ณ๊ฒฝํ๋ฉด ์ฝ๊ฒ ๊ด๋ฆฌ๊ฐ ๊ฐ๋ฅ
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.10.RELEASE")
}
}
apply plugin: 'java'
apply plugin: 'spring-boot'
sourceCompatibility = 1.5
version = '1.0'
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.11'
compile("org.springframework.boot:spring-boot-starter-web:1.2.0.RELEASE")
compile("org.springframework.boot:spring-boot-starter-data-jpa")
compile("mysql:mysql-connector-java:5.1.34")
}
dependencies๋ ์คํ๋ง ๋ถํธ๋ฅผ ์ฐ๋ ค๋ฉด spring-boot-starter-web์ด ์์ด์ผ ํ๊ณ , ๋ฐ์ดํฐ์ฐ๋ํ๋ ๊ฒ์ ํ๊ธฐ ์ํด์๋ spring-boot-starter-data-jpa๊ฐ ์์ด์ผ ํ๊ณ , mysql์ ์ฐ๋ ค๋ฉด mysql-connector-java๋ฅผ ์ถ๊ฐ ํ์(JPA์ ํ์ด๋ฒ๋ค์ดํธ ๋ด์ฅ)
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
}
}
@SpringBootApplication์ด๋ ธํ ์ด์ ์ ๋ถ์ด๋ฉด ์ต์ด ๊ธฐ๋ณธ์ ํ ์ผ๋ก ํฐ์บฃ์ ๋์์ 8080ํฌํธ๋ก ์๋ฒ ์์ฑ
spring.datasource.url=jdbc:mysql://127.0.0.1/sosi?autoReconnect=true&useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.database=mysql
spring.jpa.show-sql=true
์์ฑ๊ฐ ddl-auto๋ถ๋ถ์ด create-drop์ ์๋ฒ ์ฌ์์ ๋๋ง๋ค ํ ์ด๋ธ์ ๋ ๋ ค๋ฒ๋ฆฌ๋ ์ต์ ์ ๋๋ค.(ํ ์คํธ ์ต์ )
@Entity
public class TEST {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@Column(nullable = false)
private String name;
@OneToMany
@JoinColumn(name="TEST_id", referencedColumnName="id")
private List<testTable> testTableList;
public TEST() {
}
public TEST(String name) {
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<testTable> getTestTableList() {
return testTableList;
}
public void setTestTableList()(List<Schedule> testTableList()) {
this.testTableList = testTableList;
}
}
@Entity
public class TEST_01 {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
@JsonBackReference
private Test test;
@Column
private String program;
public TEST_01() {
}
public TEST_01(TEST test, String content) {
this.test = test;
this.content = content;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Test getTest() {
return test;
}
public void setTest(Test test) {
this.test = test;
}
public String getProgram() {
return program;
}
public void setProgram(String program) {
this.program = program;
}
}
๋ ๊ฐ์ ๋ชจ๋ธ์ด 1:N๊ด๊ณ ์ด๋ฉฐ TEST๊ฐ ์ค์ผ์ค์ ์ฌ๋ฌ๊ฐ ๊ฐ์ง ์ ์๋ ๊ตฌ์กฐ์ ๋๋ค. ์ฌ๊ธฐ์ Test_01์๋ ManyToOne์ต์ @JsonBackReference ๊ธฐ์กดDao์์๋ ํ์ด๋ฒ๋ค์ดํธ์ธ์ ์ ํตํด CRUD ์ ๊ณตํ์์ผ๋ฉฐ, Spring Data JPA์์๋ JpaRepository๋ฅผ ์ ๊ณตํ๋ฉด์ CRUD ์ ๊ณต
TestRepository.java
public interface TestRepository extends JpaRepository<Test, Long>{
}
Test_01Repository.java
public interface Test_01Repository extends JpaRepository<Test_01, Long> {
}
TestController.java
@RestController
@RequestMapping("/test")
public class TestController {
@Autowired
private TestRepository testRepository;
@RequestMapping("{testId}")
public Test getTest(@PathVariable Long testId) {
Test test = Test_01Repository.findOne(tesId);
return test;
}
}
Test_01Controller.java
@RestController
@RequestMapping("/schedule")
public class Test_01Controller {
@Autowired
private Test01Repository scheduleRepository;
@Autowired
private Test_01Repository test_01Repository;
@RequestMapping("{test_01Id}")
public Schedule getSchedule(@PathVariable Long Test_01Id) {
Test_01 test_01 = Test_01Repository.findOne(Test_01Id);
Test test = schedule.getTest();
return schedule;
}
@RequestMapping("add/{TestId}")
public Schedule addSchedule(@PathVariable Long TestId, @RequestParam(value="program") String program) {
Test test = TestRepository.findOne(testId);
Test test = testRepository.save(new Schedule(test, program));
return test;
}
}
@SpringBootApplication
public class Application {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
TestRepository testRepository = context.getBean(TestRepository.class);
testRepository.save(new Test("๊ณผ๋
"));
testRepository.save(new Test("์ ์"));
}
}
JPA ๊ธฐ๋ณธ ๋ก์ง ๋ฐ Setting