이렇게 dependencies안에 적혀있음
dependencies{
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
package jpabook.jpashop;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
@Controller
public class HelloController {
public String hello(Model model){
model.addAttribute("data","hello!!");
return "hello"; // return 은 화면 이름 즉 hello.html을 렌더링함
}
}
<!DOCTYPE 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>
spring:
datasource:
#
url: jdbc:h2:tcp://localhost/~/jpashop //db접근경로
username: sa
password:
driver-class-name: org.h2.Driver
jpa:
hibernate:
ddl-auto: create //자동으로 테이블을 만들어주는 옵션
properties:
hibernate:
#show_sql: true //SYSTEM.OUT으로 찍음
format_sql: true
logging:
level:
org.hibernate.SQL: debug //로그를 통해 출력 이녀석 쓸거면 위에 show_sql지우기
# org.hibernate.type: trace
# Member클래스
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;
}
package jpabook.jpashop;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import static org.junit.jupiter.api.Assertions.*;
@RunWith(SpringRunner.class) //스프링 러너와 함께 돌리겠다.
@SpringBootTest
class MemberRepositoryTest {
@Autowired MemberRepository memberRepository;
@Test
@Transactional //무조건 트랜젝션 안에 있어함
public void testMember() throws Exception{
//given
Member member= new Member();
member.setUsername("memberA");
//when
Long savedId = memberRepository.save(member);
Member findMember = memberRepository.find(savedId);
//then
Assertions.assertThat(findMember.getId()).isEqualTo(member.getId());
Assertions.assertThat(findMember.getUsername()).isEqualTo(member.getUsername());
Assertions.assertThat(findMember).isEqualTo(member); //이건 true가 나옴 판단기준은 같은 id면 같다고 판단
}
}