Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

세팅하기 본문

인프런/[인프런] Spring Data JPA

세팅하기

Seung__ 2021. 8. 27. 22:08

1. application.yml


spring:
  datasource:
    url: jdbc:h2:tcp://localhost/~/datajpa
    username: sa
    driver-class-name: org.h2.Driver

  jpa:
    hibernate:
      ddl-auto: create
    properties:
      hibernate:
        format_sql: true


logging.level:
    org.hibernate.SQL: debug

 

2. 엔티티를 만들자.


@Entity
@Getter @Setter
public class Member {

    @Id @GeneratedValue
    private Long id;

    private String userName;

    protected Member() {
    }

    public Member(String userName) {
        this.userName = userName;
    }

    public void changeUserName(String userName){
        this.userName = userName;
    }
}

 

3. Repository를 만들자.


크..

public interface MemberRepository extends JpaRepository<Member, Long> {


}

 

4. 잘 돌아가는지 test


@SpringBootTest
@Transactional
@Rollback(value = false)
class MemberRepositoryTest {

    @Autowired MemberRepository memberRepository;

    @Test
    public void testMember(){
        Member member = new Member("memberA");
        Member savedMember = memberRepository.save(member);

        Optional<Member> byId = memberRepository.findById(savedMember.getId());
        Member findMember = byId.get();

        Assertions.assertThat(findMember.getId()).isEqualTo(savedMember.getId());
        Assertions.assertThat(findMember.getUserName()).isEqualTo(savedMember.getUserName());
        Assertions.assertThat(findMember).isEqualTo(member);
    }

}

 

 

 

5. GitHub : initialcommit


 

GitHub - bsh6463/SpringDataJpa

Contribute to bsh6463/SpringDataJpa development by creating an account on GitHub.

github.com

 

Comments