실전! 스프링 부트와 JPA 활용1 - 웹 애플리케이션 개발 강의 - 인프런
실무에 가까운 예제로, 스프링 부트와 JPA를 활용해서 웹 애플리케이션을 설계하고 개발합니다. 이 과정을 통해 스프링 부트와 JPA를 실무에서 어떻게 활용해야 하는지 이해할 수 있습니다., 스프
www.inflearn.com
해당 강의는 Inflearn에 등록된 김영한님의 Spring Boot 강의입니다.
이번 시간에는 새로운 강의 시작이므로 프로젝트 환경설정을 해보자.
(+a1, JPA 활용편2는 JPA 활용편1에 이어서 같은 프로젝트를 진행합니다. 따라서 확실하게 해주세요!)
(+a2, JPA 기본편은 Maven으로 설정 했는데, 이번편은 Gradle 설정 방법입니다.
1. 기본 설정
https://changuk0308.tistory.com/2
[SpringBoot-스프링 입문] 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술: 1. 프로젝트 환경설정
강의 출처:https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%EC%9E%85%EB%AC%B8-%EC%8A%A4%ED%94%84%EB%A7%81%EB%B6%80%ED%8A%B8 해당 강의는 Inflearn에 등록된 김영한님의 Springboot 강의입니다. 0. Intelli J(인텔리 제이) 단
changuk0308.tistory.com
2. 추가 설정
1. 라이브러리 추가(롬복 등등)
2. DB 새로 생성
1. 윈도우 아래에 이렇게 된 곳 클릭하면 h2 db 아이콘 확인
2. h2 db 아이콘 오른쪽을 클릭.
여기서 Create a new database 클릭
3. 자신이 만들 db 경로와 username, password 등 입력해주기
경로가 ./ 으로 시작하면 h2 bin 폴더에 생성되고 ~/ 으로 하면 개인 컴퓨터의 user 폴더에 생성.
4. h2 db 생성완료 및 밑줄친 부분 복사해놓기
5. 복사한거 url에 넣고 연결 시험 및 연결
6. Generi H2 (Server)로 선택. Embedded 말고 Server. 그리고 yml 파일에 있는 url로 설정
(이때 5단계에서 jpashop3으로 설정했으니 jpashop3으로 고치면 됩니다.)
3. Gradle 설정
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.2'
id 'io.spring.dependency-management' version '1.1.4'
}
group = 'jpabook'
version = '0.0.1-SNAPSHOT'
java {
sourceCompatibility = '17'
}
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
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-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-devtools' //이것을 통해 코드 수정시 컴파일만하면 변경 업데이트 해줌
//쿼리 파라미터를 로그로 남기는 외부 라이브러리
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.0'
//queryDsl 추가
// implementation "com.querydsl:querydsl-jpa:5.0.0"
// implementation "com.querydsl:querydsl-apt:5.0.0"
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
//JUnit4 추가
testImplementation("org.junit.vintage:junit-vintage-engine") {
exclude group: "org.hamcrest", module: "hamcrest-core"
}
}
tasks.named('test') {
useJUnitPlatform()
}
추후 활용편2에서 queryDSL도 해볼 예정이니 Gradle일단 복붙 하시길 바랍니다.
이것은 캐시를 자동으로 없애고, 변경시 리로딩도 자동으로 해주는 의존성 중에 하나이다.
Html변경후 서버를 재실행 안해도, build-> recompiler ~~ 누르면 바로 변경 내용이 바뀐다.
p6spy 라이브러리 설치
//쿼리 파라미터를 로그로 남기는 외부 라이브러리
implementation 'com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.0'
쿼리 파라미터 로그 남기기
(그 전에는 어떤 값이 들어가는지 확인하려면 db에 들어가서 select 문으로 확인했어야 했는데,
이젠 콘솔창에서 바로 파라미터 값을 확인 할 수 있게 되었다. )
4. yml 파일 설정
properties를 지우고 yml로 설정하자
`application.yml`
spring: #띄어쓰기 없음
datasource: #띄어쓰기 2칸
url: jdbc:h2:tcp://localhost/~/jpashop3 #4칸
username: sa
password: 123
driver-class-name: org.h2.Driver
jpa: #띄어쓰기 2칸
hibernate: #띄어쓰기 4칸
ddl-auto: create #띄어쓰기 6칸 !!!none or create
properties: #띄어쓰기 4칸
hibernate: #띄어쓰기 6칸
# show_sql: true #띄어쓰기 8칸
format_sql: true #띄어쓰기 8칸
logging.level: #띄어쓰기 없음
org.hibernate.SQL: debug #띄어쓰기 2칸
org.hibernate.orm.jdbc.bind: trace #파라미터에 머가 들어갈지 로그에 찍어줌
야물 파일을 보면 h2 데이터 베이스의 설정을 할 수 있다.
여기서 띄워쓰기를 진짜 잘해야한다. 옆에 주석으로 얼마나 왼쪽으로부터 띄워써야하는지 표시했다.
5. JDK 적용
1. Windows: File Project Structure(Ctrl+Alt+Shift+S)-> SDK 를 설치한 JDK 17로 설정
2. Windows: Setting-> "gradle" 검색 -> 우측 이미지와 같이 설정(빨간색으로 설치한 부분만 변경)
6.. 간단하게 테스트 해보기
`Hello` 객체 생성 및 롬복으로 `Getter`,`Setter` 설정하기.(이제 코드 밑에 getter,setter 안만들어도 된다.)
@Getter @Setter
public class Hello {
private String data;
}
`Controller` 및 `model` 설정
@Controller
public class HelloController {
//HTTP GET 요청에 대한 핸들러로 작동하며, "/hello" 경로로 들어오는 요청을 처리
@GetMapping("hello")
public String hello(Model model) {
//모델 객체에 (키,값)을 추가하여 뷰에서 "키"이름으로 "값"을 참조함
model.addAttribute("data", "changuk!!");
return "hello"; //뷰이름 반환, spring이 해당 이름으로 뷰를 찾아 렌더링 함.
}
}
``
@SpringBootApplication
public class JpashopApplication {
public static void main(String[] args) {
Hello hello= new Hello();
hello.setData("lombok적용 확인");
String data = hello.getData();
System.out.println("data = "+ data);
SpringApplication.run(JpashopApplication.class, args);
}
}
'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>
7. 라이브러리 살펴보기
'Spring Boot > Spring Boot JPA-활용편1 강의 정리' 카테고리의 다른 글
[SpringBoot-JPA 활용편1] 자바 ORM 표준 JPA 프로그래밍 - 활용편1: 6. 주문 도메인 개발 (1) | 2024.02.27 |
---|---|
[SpringBoot-JPA 활용편1] 자바 ORM 표준 JPA 프로그래밍 - 활용편1: 5. 상품 도메인 개발 (1) | 2024.02.26 |
[SpringBoot-JPA 활용편1] 자바 ORM 표준 JPA 프로그래밍 - 활용편1: 4. 회원 도메인 개발 (1) | 2024.02.26 |
[SpringBoot-JPA 활용편1] 자바 ORM 표준 JPA 프로그래밍 - 활용편1: 3. 애플리케이션 구현 준비 (0) | 2024.02.26 |
[SpringBoot-JPA 활용편1] 자바 ORM 표준 JPA 프로그래밍 - 활용편1: 2. 도메인 분석 설계 (1) | 2024.02.26 |