-
1. JPA 프로젝트 생성하기 (intellij, gradle, h2 database)JAVA/JPA, QueryDSL 2022. 4. 22. 14:49728x90
2. h2 database 설치
3. h2 database 실행
연결 클릭
5. 프로젝트 다운로드 후 실행
6. application.properties 를 삭제하고 application.yaml을 생성후 내용 작성
spring: datasource: driver-class-name: org.h2.Driver username: sa password: url: jdbc:h2:tcp://localhost/~/test; jpa: show-sql: true open-in-view: false hibernate: ddl-auto: create naming: physical-strategy: org.hibernate.boot.model.naming.CamelCaseToUnderscoresNamingStrategy properties: hibernate.format_sql: true database-platform: org.hibernate.dialect.H2Dialect h2: console: enabled: true mvc: pathmatch: matching-strategy: ant_path_matcher
7. Entity 작성
@Entity : 해당 클레스가 Entity라고 지정
@Table : 해당 Entity의 Database 상의 Table 이름 지정
@Setter , Getter : get set 함수 대신 작성 해주는 Lombom 어노테이션
@NoArgsConstructor : Defualt 생성자
@Id : 해당 Entity의 pk 값을 지정
@GeneratedValue : auto_increment 지정 , GenerationType.IDENTITY : 1~ 증가하는 숫자로 지정
@Column : 해당 변수가 컬럼임을 지정
@Builder : builder() 메소드를 통해 객체를 생성 할 수 있게 함.
8. 프로젝트 실행
- 콘솔창에 query가 출력되며 h2 database에 table이 하나 생성 됨
9. Board Table에 대한 Repository 생성
@Repository : 지정한 Entity의 query를 담당하는 데이터베이스 명령 관련 어노테이션
10. CRUD service 작성
각 Dto 생성및 service class 생성 @Autowired 주입으로 boardRepository 생성 create Read update delete @Transactional :
-영속화 상태를 만들어주는 어노테이션. 해당 어노테이션이 붙어있는 메소드안에서 쿼리 수행중 error가 발생하면 쿼리가 수행되기 전 상태로 rollback 됨.
-Transactional이 끝나면 자동으로 flush가 되어 update 쿼리시 따로 save를 할 필요가 없음.
-어노테이션에 readOnly = true를 붙혀 read 메소드에서는 읽기전용으로 만들어 혹시 모르는 값 변경을 막는다.
728x90반응형LIST'JAVA > JPA, QueryDSL' 카테고리의 다른 글
2. JPA 프로젝트 테스트하기 (0) 2022.04.22