[Spring] JPA / DBMS 커넥션 맺어주기

형이·2023년 9월 21일

Spring

목록 보기
5/21
post-thumbnail

📝 Spring

🖥️ 1. JPA

  • JPA는 기존의 반복 코드는 물론이고, 기본적인 SQL도 JPA가 직접 만들어서 실행해준다.
  • JPA를 사용하면 SQL과 데이터 중심의 설계에서 객체 중심의 설계로 패러다임을 전환할 수 있다.
  • JPA를 사용하면 개발 생산성을 크게 높일 수 있다.

[application.properties]


    #DBMS 커넥션 맺어주기
    spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
    spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
    spring.datasource.username=springweb
    spring.datasource.password=springweb

    #jpa
    spring.jpa.show-sql=true
    spring.jpa.hibernate.ddl-auto=none

[build.gradle]

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	// DB 커넥션을 위한 implementation
	// 참고 링크 : https://mvnrepository.com/artifact/com.oracle.jdbc/ojdbc8/12.2.0.1
	// implementation 'org.springframework.boot:spring-boot-starter-jdbc'
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation group: 'com.oracle.database.jdbc', name: 'ojdbc6', version: '11.2.0.4'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

[Member.java]


package com.codingbox.core2.dto;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.SequenceGenerator;

@Entity			// jpa에서 관리하는 class라고 선언해주는 어노테이션
public class Member {

	// 테이블의 Primary Key를 지정
	@Id
    // PK 값으로 사용되는 시퀀스 객체가 존재할 경우 strategy를 SEQUENCE로 지정
    // @SequenceGenerator 어노테이션 함께 사용
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="mySeq")
	@SequenceGenerator(name="mySeq", sequenceName="member_seq", allocationSize=1)
	private int id;
	private String name;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

0개의 댓글