์์ฑ์ค
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";
}
}