1-1. 프로젝트 환경설정

지니🧸·2023년 1월 31일
0

Spring Boot & JPA

목록 보기
1/35

본 문서는 인프런의 실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발 (김영한) 강의를 공부하며 작성한 개인 노트입니다.

🐝 프로젝트 생성

  • 플러그인: 라이브러리 디펜던시까지 관리
  • 리포지토리: mavenCentral()에서 라이브러리를 다운로드 받겠다
  • 디펜던시 (의존관계): start.spring.io에서 선택한 라이브러리 전부
    • 여기에 등록된 라이브러리가 의존하는 라이브러리까지 모두 포함됨

롬복

  • 클래스에 getter/setter 메소드 자동생성
    • 클래스 위 @Getter @Setter
      다운로드: IntelliJ > Settings > Plugin 검색 > Lombok 검색
      셋팅: IntelliJ > Settings > Annotation processors 검색 > Compiler > Annotation Processors > Enable annotation processors 체크

🐱 라이브러리 살펴보기

스프링 부트 라이브러리

spring-boot-starter-web

  • spring-boot-starter-tomcat: 웹서버
  • spring-webmvc: 스프링 웹 MVC
    spring-boot-starter-thymeleaf: 타임리프 템플릿 엔진(View)
    spring-boot-starter-data-jpa
  • spring-boot-starter-aop
  • spring-boot-starter-jdbc
    • HikariCP 커넥션 풀 (부트 2.0 기본)
  • hibernate + JPA: 하이버네이트 + JPA
  • spring-data-jpa: 스프링 데이터 JPA
    spring-boot-starter(공통): 스프링 부트 + 스프링 코어 + 로깅
  • spring-boot
    • spring-core
  • spring-boot-starter-logging
    • logback, slf4j

테스트 라이브러리

spring-boot-starter-test

  • junit: 테스트 프레임워크
  • mockito: 테스트 프레임워크
  • assertj: 테스트 코드를 좀 더 편리하게
  • spring-test: 스프링 통합 테스트 지원

👒 View 환경 설정

thymeleaf

컨트롤러

@Controller
public class HelloController {
	@GetMapping("hello") // localhost:8080/hello 주소로 접속
    public String hello(Model model) {
    	model.addAttribute("data", "hello!");
        return "hello";
    }
}
  • model에 데이터를 실어서 뷰에 넘길 수 있음
    • "data" : attributeName
    • "hello!!!" : attributeValue
  • return 값: 화면 이름 (ex) hello.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
 <title>Hello</title>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'안녕하세요. ' + ${data}" >안녕하세요. 손님</p>
</body>
</html>
  • html의 namespace를 thymeleaf로 설정
  • th:text를 이용해 controller에서 추가한 attribute 사용 가능

devtools - html 코드 수정사항 반영 편리하게

build.gradle에 dependencies 추가

dependencies {
	...
    implementation 'org.springframework.boot:spring-boot-devtools'
	...
}

💭 H2 데이터베이스 설치

1.4.200 버전 설치
terminal: cd h2 > cd bin > cat h2.sh > ./h2.sh
팝업 창:

  • 파일 생성: JDBC URL에 jdbc:h2:~/jpashop
  • 파일 생성 확인: ~/jpashop.mv.db
  • 이후 접속: JDBC URL에 jdbc:h2:tcp://localhost/~/jpashop

👜 JPA와 DB 설정, 동작확인

src/main/resources/application.properties 삭제. application.yml 생성

  • application.yml
spring:
	...
    jpa:
    	hibernate:
        	ddl-auto: create
    ...
logging:
	level:
    	org.hibernate.SQL: debug
  • ddl-auto:create > 테이블 자동 생성
  • org.hibernate.SQL: debug > JPA/hibernate이 생성하는 SQL이 로거를 통해 보임

MemberRepository 파일과 Test

@Repository
public class MemberRepository {

    @PersistenceContext
    private EntityManager em;

    public Long save(Member member) {
        em.persist(member);
        return member.getId();
    }

    public Member find(Long id) {
        return em.find(Member.class, id);
    }
}

cmd+shift+T > test 자동생성

@RunWith(SpringRunner.class)
@SpringBootTest
class MemberRepositoryTest {
	
    @Test
    @Transactional
    void save() {
    }

    @Test
    void find() {
    }
}
  • RunWith(SpringRunner.class): JUnit에게 스프링과 관련된 테스트를 한다는 것을 알림
  • SpringBootTest: 스프링부트로 테스트를 돌릴 때 필수
profile
우당탕탕

0개의 댓글