■ Oracle DB에 테이블 만들기 + 질문 등록
# 프로젝트 생성 시 Dependencies에 JPA, Oracle 추가 설정
# application.properties 설정 확인(의존설정)
* jpa위에 3개는 옵션(선택사항), 아래2개는 필수
# Oracle Datasource config spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver spring.datasource.url=jdbc:oracle:thin:@localhost:1521:xe spring.datasource.username=system spring.datasource.password=12345 # JPA config # SQL Query가 콘솔창에 출력되는 설정 spring.jpa.properties.hibernate.show_sql=true # SQL Query가 콘솔창에 가독성 좋게 변환해서 출력하는 설정 spring.jpa.properties.hibernate.format_sql=true # SQL Query가 실행될 때 콘솔창에 파라미터 값이 오는 자리에 ?를 표시해주는 설정 logging.level.org.hibernate.type.descriptor.sql.spi=trace # 데이터베이스 테이블 생성 관련한 옵션 설정 spring.jpa.hibernate.ddl-auto=update # 연결된 데이터베이스의 종류를 설정 spring.jpa.database-platform=org.hibernate.dialect.OracleDialect # spring.jpa.database-platform=org.hibernate.dialect.MySQL80Dialect |
# Question entity
package com.kmii.JPATest.entity;
import java.time.LocalDateTime;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.SequenceGenerator;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity // 엔티티 클래스로 설정
@Table(name="jpaquestiontbl") //실제로 매핑될 데이터베이스의 테이블 이름 , 생략시 클래스 이름 그대로 생성
@SequenceGenerator(
name="QUESTION_SEQ_GENERATOR", //JPA 내부 시퀀스 이름
sequenceName="QUESTION_SEQ", // 실제 DB 시퀀스 이름
initialValue = 1, // 시퀀스 시작값
allocationSize = 1 // 시퀀스 증가치
)
@Data
@NoArgsConstructor
@AllArgsConstructor // dto따로만들지 않는경우는 여기에 적어주고, dto를 따로 만들어도 된다. -> 여기선 같이 사용 :이 파일은 entity class면서 dto class / 따로쓰는게 좋다
public class Questiontbl {
@Id // 해당 테이블의 기본키 설정
@Column(name="qnum") // 실제로 DB테이블에 만들어질 필드 이름 지정, 생략시 밑에 설정한 필드이름으로 들어간다.
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "QUESTION_SEQ_GENERATOR") //GenerationType.IDENTITY 사용할경우 Oracle, MySQL 둘다사용가능
private Long qnum; // 질문 번호, 기본키-시퀀스자동증가(oracle이라서), oracle에 미리 만들어놔야한다 , null값 설정할수 있어야해서 int대신 Long, Integer
@Column(name="qtitle", length = 20, nullable=false)
private String qtitle; // 질문 제목
@Column(name="qcontent", length= 200, nullable=false)
private String qcontent; // 질문 내용
@CreationTimestamp //자동으로 입력(sysdate)
private LocalDateTime qdate; // 질문 등록일 - sysdate
@UpdateTimestamp
private LocalDateTime udate; // 레코드(질문글) 수정한 시간 자동입력
}
# Repository 패키지에 인터페이스 생성 (= Dao 클래스의 역할)
package com.kmii.JPATest.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.kmii.JPATest.entity.Questiontbl;
public interface QuestionRepository extends JpaRepository<Questiontbl,Long> { //Dao 클래스의 역할 - SQL문이 없이도 동작 가능 , <연동될 entity클래스이름, 기본키의 타입(wrapper class)>
}
# Test
- 파일 생성 위치
package com.kmii.JPATest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import com.kmii.JPATest.entity.Questiontbl;
import com.kmii.JPATest.repository.QuestionRepository;
@SpringBootTest
public class TestQuestion {
@Autowired
private QuestionRepository questionRepository;
@Test
@DisplayName("질문 등록 테스트")
public void writeQuestion() {
Questiontbl question = new Questiontbl(); // Questiontbl안에 dto 설정해놓아서 불러오기
question.setQtitle("첫번째 질문입니다");
question.setQcontent("오늘은 목요일이 맞나요?");
questionRepository.save(question); //save(entity), insert문 쿼리가실행됨
//questionRepository는 상속 받아서 select(find) , delete(delete), insert(save) 문은 가져올 수 있지만 update는 없다
}
}
# Test 실행 → Oracle DB에 테이블 생성, 데이터 확인
'Write it > Spring Boot' 카테고리의 다른 글
Spring Boot) Junit test 연습 (0) | 2025.09.09 |
---|---|
Spring Boot) H2 이용 질문 테이블 만들기 (0) | 2025.09.08 |
Spring Boot) JPA의 개념 , 특징 (0) | 2025.09.08 |
Spring Boot에서 Mybatis 이용 간단한 예제 만들어보기 (0) | 2025.09.05 |
Spring Boot) 스프링 부트란? (0) | 2025.09.04 |