[JPA]SpringBoot + JPA

์ด๋Šฅ๋ฉธยท2023๋…„ 12์›” 26์ผ
0

SpringBoot

๋ชฉ๋ก ๋ณด๊ธฐ
7/7

๐Ÿ“Œ์•Œ์•„๋‘๊ธฐ

์ž‘์„ฑ์ค‘

๐Ÿ™†โ€โ™€๏ธ ์‚ฌ์šฉํˆด

  1. STS

๐ŸŽฏ ์—ฐ๋™๋ฐฉ๋ฒ•

1. build.gradleํŒŒ์ผ์— ์˜์กด์„ฑ์ฃผ์ž…

implementation 'org.springframework.boot:spring-boot-starter-data-jpa:3.1.6' //jpa
compileOnly 'org.projectlombok:lombok:1.18.26' //lombok

2. application.propertiesํŒŒ์ผ์— DB์™€ JPA์„ค์ •

#JPA๊ฐ€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰ ์‹œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์Šคํ‚ค๋งˆ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑ ๋˜๋Š” ์ˆ˜์ •ํ• ์ง€๋ฅผ ๊ฒฐ์ •ํ•˜๋Š” ์„ค์ •
spring.jpa.hibernate.ddl-auto=update

#oracleDB
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle12cDialect

#oracleDB
spring.datasource.driverClassName=oracle.jdbc.OracleDriver
spring.datasource.url=jdbc:oracle:thin:@localhost:1521/xe
spring.datasource.username=์•„์ด๋””
spring.datasource.password=๋น„๋ฐ€๋ฒˆํ˜ธ

3. ํ…Œ์ด๋ธ”๊ณผ ์ปฌ๋Ÿผ๊ฐ’๊ณผ ๋Œ€์‘ํ•˜๋Š” Entity์ƒ์„ฑ

package com.example.demo.entity;

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

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor //๊ธฐ๋ณธ์ƒ์„ฑ์ž ์ƒ์„ฑ ์–ด๋…ธํ…Œ์ด์…˜
@Entity(name="ํ…Œ์ด๋ธ”๋ช…") //ํ…Œ์ด๋ธ”๊ณผ ๋งค์นญ๋  ์—”ํ‹ฐํ‹ฐ์ž„์„ ๋‚˜ํƒ€๋‚ด๋Š” ์–ด๋…ธํ…Œ์ด์…˜
public class testEntity {

	@Id //pk๋ฅผ ๋‚˜ํƒ€๋ƒ„
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private long notiSeq;
	
	//๊ธฐ๋ณธ์ ์œผ๋กœ @Column ์žฅ์ฐฉ๋˜์–ด์žˆ์Œ
	private String notiKindCd;
	private String notiKindNm;
	private String notiTitle;
	private String notiDate;
	private String notiContent;
	
	@Builder //Setter๋Œ€์‹  ์ดˆ๊ธฐํ™”
	public testEntity(String notiKindCd, String notiKindNm, String noti_kind_nm, String notiTitle, String notiDate, String notiContent) {
		this.notiKindCd = notiKindCd;
		this.notiKindNm = notiKindNm;
		this.notiTitle = notiTitle;
		this.notiDate = notiDate;
		this.notiContent = notiContent;
	}

}

4. Entity๋ฅผ ์‚ฌ์šฉํ•˜๋Š” Repository์ƒ์„ฑ

package com.example.demo.entity;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface lostNotiRepository  extends JpaRepository<testEntity, Long>{//์—ฐ๊ฒฐํ•œ ์—”ํ‹ฐํ‹ฐ<์ด๋ฆ„, pk ๋ณ€์ˆ˜ํ˜•>
//ํ…Œ์ŠคํŠธ๋งŒํ•˜๊ธฐ์œ„ํ•ด jpa๋ฉ”์†Œ๋“œ ๋ฏธ๊ธฐ์ž…
}

5. ์ปจํŠธ๋กค๋Ÿฌ

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.entity.testEntity;
import com.example.demo.entity.lostNotiRepository;

import java.util.List;

//import org.junit.After;
//import org.junit.Test;

@RestController
public class MainController {
	lostNotiRepository lostNotiRepository;
	
	@Autowired //๋นˆ์ฃผ์ž…
	public MainController(lostNotiRepository lostNotiRepository) {
		this.lostNotiRepository = lostNotiRepository;
	}
	
	@RequestMapping("/hello")
	public String hello() {
		List<testEntity> temp = lostNotiRepository.findAll(); //ํ…Œ์ด๋ธ”์— ์žˆ๋Š” ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋“ค์„ ๊ฐ€์ ธ์˜ค๋Š” findAllํ•จ์ˆ˜
		return "hello";
	}
	

}
profile
์•ˆ๋…•ํ•˜์„ธ์š”

0๊ฐœ์˜ ๋Œ“๊ธ€