Part 1. Spring Framework
Ch 03. 예제를 만들며 이해하는 스프링 웹 어플리케이션 핵심 기술
05. H2 DB 설명, 간단한 DB 저장 기능 구현
H2 DB
- relational DB (mySQL, Oracle는 직접 설치해야함, H2 DB는 설치가 필요 없음)
- memory DB로 많이 사용 (간단하고 편리하게 사용가능)
- build.grale()에 iruntimeOnly "'com.h2database:h2'"를 통해
api를 실행 시 db가 생성되었다가 api종료 시 데이터가 삭제됨
spring:
h2:
console:
enabled: true
<application.yml>
<localhost:8080/h2-console>
package com.fastcampus.programmin.dmaker.controller;
import com.fastcampus.programmin.dmaker.service.dMakerService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
@Slf4j
@RestController //사용자의 요청을 받아 json 형태로 응답을 반환 //ctrl + 클릭
@RequiredArgsConstructor
// DMakerController(Bean) DMakerService(Bean) DeveloperRepository(Bean)
//===========================Spring Application =============================
//
//
public class DMakerController {
private final dMakerService dMakerService;
@GetMapping("/developers") //사용자 요청이 /developers를 통해 들어온다면 (ex] GET /developers HTTP/1.1)
//
public List<String> getAllDevelopers(){
// GET /Developers HTTP/1.1
log.info("GET /developers HTTP/1.1");
return Arrays.asList("snow", "elsa", "Olaf");
}
@GetMapping("/create-developer")
public List<String> createDevelopers() {
log.info("GET /create-developer HTTP/1.1");
dMakerService.createDeveloper();
return Arrays.asList("Olaf");
}
}
<DMakerController.java>
package com.fastcampus.programmin.dmaker.Repository;
import com.fastcampus.programmin.dmaker.entity.Developer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface DeveloperRepository
extends JpaRepository<Developer, Long> {
}
<DeveloperRepository.java>
package com.fastcampus.programmin.dmaker.service;
import com.fastcampus.programmin.dmaker.Repository.DeveloperRepository;
import com.fastcampus.programmin.dmaker.entity.Developer;
import com.fastcampus.programmin.dmaker.type.DeveloperLevel;
import com.fastcampus.programmin.dmaker.type.DeveloperSkillType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
@Service
@RequiredArgsConstructor
public class dMakerService {
private final DeveloperRepository developerRepository;
@Transactional
public void createDeveloper(){
Developer developer = Developer.builder()
.developerLevel(DeveloperLevel.JUNIOR)
.developerSkillType(DeveloperSkillType.FRONT_END)
.experienceYears(2)
.name("Olaf")
.age(5)
.build();
developerRepository.save(developer);
}
}
<dMakerService.java>
----------------------------------------------------------------------------------------------------------------------------------
'[패스트캠퍼스] Spring' 카테고리의 다른 글
패스트캠퍼스 챌린지 21일차 (0) | 2022.02.13 |
---|---|
패스트캠퍼스 챌린지 20일차 (0) | 2022.02.12 |
패스트캠퍼스 챌린지 18일차 (0) | 2022.02.10 |
패스트캠퍼스 챌린지 17일차 (0) | 2022.02.09 |
패스트캠퍼스 챌린지 16일차 (0) | 2022.02.08 |
댓글