Spring/SpringBoot

#2 application.yml vs properties, SpringBoot쿼리 파라미터

SK_MOUSE 2022. 4. 3. 01:07
반응형

이프로젝트에서는 yml을 사용

 

application.properties를 지우고 application.yml파일 생성

 

application.yml 설정

application.yml 내용(띄어쓰기 주의★★★★★)

spring:
#spring아래
  datasource:
    url: jdbc:h2:tcp://localhost/~/jpashop
    username: sa
    password:
    driver-class-name: org.h2.Driver
#spring아래
  jpa:
    hibernate:
      ddl-auto: create
    properties:
      hibernate:
#          show_sql: true
        format_sql: true


logging.level:
  org.hibernate.SQL: debug
#  org.hibernate:type: trace

h2 1.4.200 버전 다운로드 링크
윈도우 설치 버전: https://h2database.com/h2-setup-2019-10-14.exe
윈도우, 맥, 리눅스 실행 버전: https://h2database.com/h2-2019-10-14.zip

 

데이터베이스 파일 생성 방법

jdbc:h2:~/저장할DB이름(최소 한번)
~/저장한DB이름.mv.db 파일 생성 확인
이후 부터는 jdbc:h2:tcp://localhost/~/저장할DB이름 이렇게 접속

 

1. Member.java 엔티티생성

package jpabook.jpashop;

import lombok.Getter;
import lombok.Setter;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
@Getter @Setter
public class Member {

    @Id
    @GeneratedValue
    private Long id;
    private String username;
}

 

2. MemberRepository.java : 데이터베이스 자동관리 by 엔티티매니저

package jpabook.jpashop;

import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Repository
public class MemberRepository {

    @PersistenceContext//데이터를 알아서 EntityManager가 넣어줌
    private EntityManager em;

    public Long save(Member member){
        em.persist(member);
        return member.getId();//id정도만 조회하는용도
    }

    public Member find(Long id){//조회하는용도
        return em.find(Member.class, id);
    }
}

 


테스트

 

MemberRepositoryTest.java

package jpabook.jpashop;

import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;

import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MemberRepositoryTest {
    @Autowired
    MemberRepository memberRepository;

    @Test
    @Transactional //NO EntityManager에러 안뜨려면. 이걸 넣어줘야함
    public void testMember() throws Exception{
        //given
        Member member = new Member();
        member.setUsername("memberA");

        //when
        Long saveId = memberRepository.save(member);//저장하면서 id값 return받음
        Member findMember = memberRepository.find(saveId);//저장한 id값으로 find하기

        //then
        Assertions.assertThat(findMember.getId()).isEqualTo(member.getId());
        Assertions.assertThat(findMember.getUsername()).isEqualTo(member.getUsername());
        
        Assertions.assertThat(findMember).isEqualTo(member);//영속성 같으면 같은거니깐. 컨테이너내에서 같은것.
        System.out.println("findMember == member: " + (findMember==member));
    }
}

쿼리 파라미터 로그 남기기

 

gradle에 dependencies에 아래 라이브러리를 추가해 주면 된다.

implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.5.6'

 

반응형