๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Web Application

spring ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ

by ํ–‰๋ฑ 2021. 9. 26.

< inflearn "์Šคํ”„๋ง ์ž…๋ฌธ - ์ฝ”๋“œ๋กœ ๋ฐฐ์šฐ๋Š” ์Šคํ”„๋ง ๋ถ€ํŠธ, ์›น MVC, DB ์ ‘๊ทผ ๊ธฐ์ˆ " ๊ฐ•์˜ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฒŒ์‹œํŒ ๋งŒ๋“ค์–ด๋ณด๊ธฐ >

 

1. https://start.spring.io/ ์—์„œ springboot ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

 

 

2. HomeController ๋ฐ home.html ์ƒ์„ฑ โ–ท ์‹คํ–‰ํ•˜๊ณ  localhost:8080 ์ ‘์† ํ™•์ธ

- ์ตœ์ดˆ ์ ‘์† ์‹œ ๋‚˜์˜ค๋Š” ํŽ˜์ด์ง€ ์—ฐ๊ฒฐ by @GetMapping("/") → return "home"
- home.html์—์„œ [๊ธ€์“ฐ๊ธฐ] ํ™”๋ฉด๊ณผ [๋ชฉ๋ก๋ณด๊ธฐ] ํ™”๋ฉด ๋งํฌ๋ฅผ ๊ฑธ์–ด์คŒ
- ์‹คํ–‰๋ฐฉ๋ฒ•: src/main/java ์•„๋ž˜์˜ XXXApplication์˜ main ํ•จ์ˆ˜ ์‹คํ–‰
- spring์ด ์˜ค๋ฅ˜ ์—†์ด ์ž˜ ๋œจ๋Š”์ง€ ์ฝ˜์†”๋กœ ํ™•์ธํ•˜๊ณ , home.html์ด ์ž˜ ๋œจ๋Š”์ง€ ํ™•์ธ

 

3. ๋„๋ฉ”์ธ ํด๋ž˜์Šค (Post) ์ƒ์„ฑ

- ๊ฒŒ์‹œ๊ธ€ ํ•˜๋‚˜์— ํ•ด๋‹น๋˜๋Š” ํด๋ž˜์Šค๋กœ, ์–ด๋–ค ํ•„๋“œ๊ฐ€ ๊ด€๋ฆฌ๋˜์–ด์•ผ ํ•˜๋Š”์ง€ ๊ฒฐ์ •
- idx, title, writer, password, contents ํ•„๋“œ๋ฅผ private์œผ๋กœ ์„ ์–ธํ•˜๊ณ  getter, setter ์ƒ์„ฑ (Alt+Insert๋กœ ์ž๋™ ์ƒ์„ฑ)
- TODO: dttm, hits

 

4. Repository Interface (PostRepository) ์ƒ์„ฑ

- ๊ฒŒ์‹œ๊ธ€ ํ•˜๋‚˜์— ๋Œ€ํ•˜์—ฌ (๋ณด๋‹ค DB ์นœํ™”์ ์œผ๋กœ) ์–ด๋–ค ๊ธฐ๋Šฅ์ด ๊ตฌํ˜„๋˜์–ด์•ผ ํ•˜๋Š”์ง€ ๊ฒฐ์ •
- ๊ฒŒ์‹œ๊ธ€ ์ €์žฅ(save), ๊ฒŒ์‹œ๊ธ€ ์ฐพ๊ธฐ(findByIdx), ๊ฒŒ์‹œ๊ธ€ ์ „์ฒด ์กฐํšŒ(findAll) 3๊ฐ€์ง€๋ฅผ ๊ธฐ๋ณธ ๊ธฐ๋Šฅ์œผ๋กœ ๊ฒฐ์ •
- ๋ฉ”๋ชจ๋ฆฌ ๊ธฐ๋ฐ˜, DB ๊ธฐ๋ฐ˜ ๋ชจ๋‘ ์ž‘๋™ํ•  ์ˆ˜ ์žˆ๋„๋ก Interface๋กœ ์„ค๊ณ„
- TODO: edit, delete, matchPassword

 

5. ๋ฉ”๋ชจ๋ฆฌ ๊ธฐ๋ฐ˜ Repository ๊ตฌํ˜„์ฒด (MemoryPostRepository) ์ƒ์„ฑ โ–ท @Repository

- DB ์—ฐ๋™ ์ „์— ๋ฉ”๋ชจ๋ฆฌ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ฒŒ์‹œํŒ์ด ์ž˜ ์ž‘๋™๋˜๋Š”์ง€ ํ™•์ธ
- DB ์—ฐ๋™์€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— spring์„ ๋‚ด๋ ธ๋‹ค ์˜ฌ๋ฆฌ๋ฉด ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋˜์ง€ ์•Š์Œ
- PostRepository๋ฅผ implementsํ•˜๊ณ , ๋ฉ”๋ชจ๋ฆฌ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„ by Map<Long, Post>
- ์ผ๋‹จ ๋ฉ”๋ชจ๋ฆฌ ๊ธฐ๋ฐ˜์œผ๋กœ ์ž‘๋™๋˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ @Repository๋ฅผ ๋ถ™์—ฌ์„œ ์ปดํฌ๋„ŒํŠธ ๋“ฑ๋ก

 

6. ์„œ๋น„์Šค ํด๋ž˜์Šค (PostService) ์ƒ์„ฑ โ–ท @Service ๋ถ™์ด๊ณ  PostRepository @Autowired

- Repository๋ณด๋‹ค ์„œ๋น„์Šค ๋‹จ์— ๊ฐ€๊น๊ฒŒ ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„
- ๊ฒŒ์‹œ๊ธ€ ๋“ฑ๋ก(registerPost), ๊ฒŒ์‹œ๊ธ€ ์ฐพ๊ธฐ(findOne), ๊ฒŒ์‹œ๊ธ€ ์ „์ฒด ์กฐํšŒ(findPosts)
- Repository์˜ ๋‚ด์šฉ๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ๋ฉ”์†Œ๋“œ ์ด๋ฆ„์„ ์„œ๋น„์Šค ๋‹จ์— ๊ฐ€๊น๊ฒŒ ๋ถ™์ด๋ ค๊ณ  ๋…ธ๋ ฅํ–ˆ์Œ
- ์ƒ์„ฑ์ž๋กœ PostRepository ๊ฐ์ฒด๋ฅผ DI (@Autowired) ๋ฐ›๊ณ , PostRepository์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉ (์•ž์—์„œ @Repository๋กœ ์ปดํฌ๋„ŒํŠธ ๋“ฑ๋ก์„ ํ•ด์„œ DI๊ฐ€ ๊ฐ€๋Šฅํ•œ ๊ฒƒ์ž„)

 

7. PostController โ–ท @Controller ๋ถ™์ด๊ณ  PostService @Autowired โ–ท ์‹คํ–‰ํ•˜๊ณ  localhost:8080 ํ™•์ธ

- ์ƒ์„ฑ์ž๋กœ PostService ๊ฐ์ฒด๋ฅผ DI ๋ฐ›๊ณ , PostService์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉ
- home.html์—์„œ ๊ฑธ์–ด์ค€ [๊ธ€์“ฐ๊ธฐ]์™€ [๋ชฉ๋ก๋ณด๊ธฐ] ๋งํฌ๊ฐ€ ์–ด๋””๋กœ ์—ฐ๊ฒฐ๋˜์–ด์•ผ ํ•˜๋Š”์ง€ @GetMapping์„ ํ†ตํ•ด html ์•ˆ๋‚ด
- [๊ธ€์“ฐ๊ธฐ] - createPostForm.html, [๋ชฉ๋ก๋ณด๊ธฐ] - postList.html
- createPostForm.html์—์„œ๋Š” form tag์™€ input tag๋ฅผ ์ด์šฉํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์Œ
- PostForm ํด๋ž˜์Šค๋ฅผ ์‹ ๊ทœ ์ƒ์„ฑํ•˜๊ณ , input tag์˜ name๊ณผ ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ํ•„๋“œ๋ฅผ ๋งŒ๋“ค๋ฉด spring์ด ์•Œ์•„์„œ ๊ฐ๊ฐ์˜ ํ•„๋“œ์— ๋งคํ•‘ํ•ด์คŒ (getter, setter๋„ ๋งŒ๋“ค๊ธฐ)
- ๋‹ค์‹œ PostController๋กœ ๋Œ์•„๊ฐ€์„œ PostForm์œผ๋กœ ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ๋ฅผ Post๋กœ ๋‹ด๊ณ , PostService์˜ registerPost ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉ with @PostMapping

 

8. JDBC Template์„ ํ™œ์šฉํ•œ mysql ์—ฐ๊ฒฐ์„ ์œ„ํ•˜์—ฌ build.gradle, application.properties์— ๋ผ์ธ ์ถ”๊ฐ€

dependencies { implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation 'org.springframework.boot:spring-boot-starter-jdbc' runtimeOnly 'mysql:mysql-connector-java'}

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.datasource.url=jdbc:mysql://localhost:3306/board-spring?serverTimezone=UTC&characterEncoding=UTF-8spring.datasource.username=rootspring.datasource.password=XXXX

 

9. DB ๊ธฐ๋ฐ˜ Repository ๊ตฌํ˜„์ฒด (JdbcTemplatePostRepository) ์ƒ์„ฑ โ–ท @Repository (MemoryPostRepository์˜ @Repository๋Š” ์ œ๊ฑฐ)

- JDBC Template์„ ํ†ตํ•ด ์œ„์—์„œ ๋“ฑ๋กํ•œ mysql DB์™€ ์—ฐ๋™
- ๋ฉ”๋ชจ๋ฆฌ ๊ธฐ๋ฐ˜ ๊ตฌํ˜„์ฒด ๋Œ€์‹  DB ๊ธฐ๋ฐ˜ ๊ตฌํ˜„์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ, ๋ฉ”๋ชจ๋ฆฌ ๊ธฐ๋ฐ˜ ๊ตฌํ˜„์ฒด์— ์ ์—ˆ๋˜ @Repository๋Š” ์ œ๊ฑฐํ•˜๊ณ  ์ƒˆ๋กœ ๋งŒ๋“  DB ๊ธฐ๋ฐ˜ ๊ตฌํ˜„์ฒด์— @Repository๋ฅผ ๋ถ™์ž„

 

 

 

 

 

 

 

๋Œ“๊ธ€