우선 SQL이 보이도록 src > resources > aplication.properties에 입력
JPA가 sql을 자바명령어로 바꿔서 실행하는 것을 볼 수있다.
예제를 바로 확인하기위해 임시로 Application파일에서 직접 실행시키는 코드를 사용한다.
package com.sparta.week01_hwk;
import com.sparta.week01_hwk.domain.Person;
import com.sparta.week01_hwk.domain.PersonRepository;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.util.List;
@SpringBootApplication
public class Week01HwkApplication {
public static void main(String[] args) {
SpringApplication.run(Week01HwkApplication.class, args);
}
@Bean
public CommandLineRunner demo(PersonRepository repository) {
return (args) -> {
Person person1 = new Person("자바의 봄 Spring", "홍유찬");
repository.save(person1);//repository 인터페이스 호출하여 jpa사용//save = insert
List<Person> personList = repository.findAll();//findAll = select *
for(int i=0; i<personList.size(); i++){
Person p = personList.get(i);
System.out.println(p.getTitle());
System.out.println(p.getName());
}
};
}
}
실행하면 properties 설정에 의해 JPA가 실행하는 SQL문을 볼 수 있다.
properties에 h2 설정이 되어있다면
hongyuchan.tistory.com/2?category=944544
http://localhost:8080/h2-console/ 로 접속하여
생성된 테이블을 확인할 수있다. 끄읕
'H2' 카테고리의 다른 글
JPA CRUD (0) | 2020.11.15 |
---|---|
JPA로 생성일자, 수정일자 필드 만들기 (0) | 2020.11.15 |
JPA 시작하기 (0) | 2020.11.15 |
H2 웹콘솔 띄우기 (0) | 2020.11.14 |