[ @PostMapping์œผ๋กœ ๋žœ๋คํ•œ ์ˆซ์ž 6๊ฐœ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ]

 

Spring - [intelliJ,Thymeleaf] @PostMapping์œผ๋กœ ๋žœ๋ค ์ˆซ์ž 6๊ฐœ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ

[ ๋กœ๋˜, ๋ฒ„ํŠผ ๋ˆŒ๋Ÿฌ ๋žœ๋คํ•œ ์ˆซ์ž 6๊ฐœ ์ถœ๋ ฅํ•˜๊ธฐ(2) ] Spring - [intelliJ,Thymeleaf] ๋กœ๋˜, ๋ฒ„ํŠผ ๋ˆŒ๋Ÿฌ ๋žœ๋ค ์ˆซ์ž 6๊ฐœ ์ถœ๋ ฅํ•˜๊ธฐ(2)[ ๋กœ๋˜, ๋ฒ„ํŠผ ๋ˆŒ๋Ÿฌ ๋žœ๋คํ•œ ์ˆซ์ž 6๊ฐœ ์ถœ๋ ฅํ•˜๊ธฐ ] Spring - [intelliJ,Thymeleaf] ๋กœ๋˜, ๋ฒ„

post-this.tistory.com

 

 

 


 

 

๋Œ“๊ธ€์„ ์ €์žฅํ•˜์—ฌ
์ตœ๊ทผ ๋Œ“๊ธ€ 10๊ฐœ๋งŒ ํ™”๋ฉด์— ์ถœ๋ ฅ๋œ๋‹ค.


 

 

1. ๋™์ž‘ํ™”๋ฉด๊ณผ ์ „์ฒด์ฝ”๋“œ

 

 

 

๋™์ž‘ํ™”๋ฉด

 

 

 

 

 

์ „์ฒด์ฝ”๋“œ

 

GitHub - hyeong-ing/LuckyLotto: ๋กœ๋˜ ๋ฒˆํ˜ธ๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ์›น์‚ฌ์ดํŠธ์ž…๋‹ˆ๋‹ค.

๋กœ๋˜ ๋ฒˆํ˜ธ๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ์›น์‚ฌ์ดํŠธ์ž…๋‹ˆ๋‹ค. Contribute to hyeong-ing/LuckyLotto development by creating an account on GitHub.

github.com

 

 

 

 


 

 

 

 

2. ์–ด๋–ป๊ฒŒ ๋งŒ๋“ค์ง€ ์ƒ๊ฐํ•˜๊ธฐ ๐Ÿ‘จ๐Ÿป‍๐Ÿ’ป

 

+ ์ด๊ฒƒ์ €๊ฒƒ ์‹œ๋„ํ•ด๋ณด๋ฉด์„œ ๋งŒ๋“ ๊ฑฐ๋ผ, DI๋‚˜ ํด๋ฆฐ์ฝ”๋“œ๋ฅผ ์ง€ํ‚ค์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.

 

 

๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๋ฅผ ์‚ฌ์šฉํ•ด๋ณด๊ณ  ์‹ถ์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๊ฐ„๋‹จํ•œ ๋Œ“๊ธ€์ฐฝ์„ ๋งŒ๋“ค๊ธฐ๋กœ ๊ฒฐ์ •ํ–ˆ๋‹ค.

 

โ–ก ๋Œ“๊ธ€์€ ๊ฐ€์žฅ ์ตœ์‹ ์ธ 10๊ฐœ๋งŒ ์ถœ๋ ฅ๋œ๋‹ค.

     โ–ป  ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ๊บผ๋‚ผ๋•Œ DESC๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ 10๊ฐœ๋งŒ ๋ณด์ด๋„๋ก ํ•œ๋‹ค.

 

โ–ก 25์ž ์ด์ƒ์€ ์ดˆ๊ณผํ•˜์ง€ ์•Š๋„๋ก ํ•œ๋‹ค.

     โ–ป  ๊ธ€์ด 1์ž ์ด์ƒ 25์ž ์ดํ•˜๋กœ ์ €์žฅ๋˜๊ฒŒ ์กฐ๊ฑด์„ ์ œ์‹œํ•œ๋‹ค.

     

โ–ก repository(DB)์™€ ์—ฐ๊ฒฐํ•  entity, Controller, ๋Œ“๊ธ€์„ ์ฒ˜๋ฆฌํ•  Service๋ฅผ ๋งŒ๋“ค๋ฉด ๋  ๊ฒƒ ๊ฐ™๋‹ค.

 

 

 

 

 


 

 

 

 

 

3. ํ™”๋ฉด ๋งŒ๋“ค๊ธฐ

 

โ–ก  ๋Œ“๊ธ€์„ ์ž…๋ ฅํ•  ๊ณต๊ฐ„ ๋งŒ๋“ค๊ธฐ.

<form th:action="@{/save}" method="get">
    <input class="write" type="text" name="content" 
    		placeholder ="์งง์€ ๋Œ“๊ธ€ ๋‚จ๊ธฐ๊ธฐ (25์ž์ด๋‚ด)" maxlength="25">
    <button class="save" type="submit"> ์ €์žฅ </button>
</form>

 

input ํƒœ๊ทธ๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ์ž…๋ ฅ์„ ๋ฐ›๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. type์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์žˆ๋‹ค. text, password, radio ๋“ฑ ์žˆ๊ณ  ๊ทธ ์ค‘ ๊ฐ„๋‹จํ•œ ๋Œ“๊ธ€์„ ์ €์žฅํ•˜๋‹ˆ ์†์„ฑ์€ text๋กœ ํ•ด์คฌ๋‹ค. placeholder๋Š” ์‚ฌ์šฉ์ž๊ฐ€ ์ฐฝ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ „ ์•„๋ž˜์ฒ˜๋Ÿผ ๊ธ€์ž๊ฐ€ ๋ณด์ด๋„๋ก ํ•ด์ค€๋‹ค.

 

input์— ๋‹ด๊ธด ๋‚ด์šฉ์€ content๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ๋ณด๋‚ด์ง€๊ฒŒ ๋œ๋‹ค. form์ด ๋ณด๋‚ด๋Š” ์—ญํ• ์„ ํ•˜๋Š”๋ฐ action์— ์ ์€ save๊ฐ€ ์žˆ๋Š” ๊ณณ์œผ๋กœ(์ปจํŠธ๋กค๋Ÿฌ) ์ „์†กํ•˜๊ฒŒ ๋œ๋‹ค.

 

 

 

 

โ–ก ๋Œ“๊ธ€์ด ๋ณด์—ฌ์ง€๋Š” ๊ณต๊ฐ„ ๋งŒ๋“ค๊ธฐ.

<div class="commentBox">
    <li class="font" th:each="comment : ${comments}" th:text="${comment.content}">
        comment
    </li>
</div>

 

li ํƒœ๊ทธ๋Š” HTML ๋ฆฌ์ŠคํŠธ์— ํฌํ•จ๋˜๋Š” ์•„์ดํ…œ์„ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๊ฒ€์€์ƒ‰ ์ž‘์€ ์›์ด ํ‘œ์‹œ๋œ๋‹ค${comment.contetn)๋กœ ํ™”๋ฉด์— ํ‘œ์‹œํ•  ๋‚ด์šฉ์„ ๊ฐ€์ ธ์™”๋‹ค. ์ด ๋‚ด์šฉ์€ ์ด 10๊ฐœ ๋Œ“๊ธ€๋“ค์„ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฏ€๋กœ, each ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ํ™”๋ฉด์— ์ฐจ๋ก€๋กœ ์ถœ๋ ฅํ•ด์ค€๋‹ค. 

 

 

 

 

 


 

 

 

 

4. ๊ตฌ์กฐ ์„ค๋ช…

 

๊ถ๊ทน์ ์œผ๋กœ ๋Œ“๊ธ€์„ ์ž‘์„ฑํ•˜๋ฉด ๋ ˆํฌ์ง€ํ† ๋ฆฌ์— ์ €์žฅ์ด ๋˜๋„๋ก ๋งŒ๋“ค๊ณ ์ž ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์ €์žฅ๋œ ๋Œ“๊ธ€์„ ๋ฐ›์•„ ๋ ˆํฌ์ง€ํ† ๋ฆฌ๊นŒ์ง€ ์ €์žฅํ•ด์ค„ controller๊ฐ€ ํ•„์š”ํ•˜๋‹ค. controller๋Š” save๋ผ๋Š” ์•ค๋“œํฌ์ธํŠธ๋กœ ๋Œ“๊ธ€์„ ๋ฐ›์„ ๊ฒƒ์ด๋‹ค. ๋Œ“๊ธ€์—๋Š” ์กฐ๊ฑด์ด ์žˆ์—ˆ๋‹ค. 1์ž ์ด์ƒ 25์ž ์ดํ•˜๋ผ๋Š” ์กฐ๊ฑด์ด์—ˆ๋‹ค. ๊ทธ๋ ‡๊ธฐ์— ์ด ๋ถ€๋ถ„์€ service๋ฅผ ํ†ตํ•ด ๊ด€๋ฆฌํ•  ๊ฒƒ์ด๋‹ค. service์— ์กฐ๊ฑด๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์กฐ๊ฑด์ด ๋งž์œผ๋ฉด ๋ ˆํฌ์ง€ํ† ๋ฆฌ์— ์ €์žฅ์ด ๋˜๋„๋กํ•œ๋‹ค.

๋ ˆํฌ์ง€ํ† ๋ฆฌ์— ์ €์žฅ๋œ ๋Œ“๊ธ€ 10๊ฐœ๋ฅผ ๊บผ๋‚ด ํ™”๋ฉด์— ์ถœ๋ ฅํ•  ๊ฒƒ์ด๋‹ค. ๋Œ“๊ธ€ 10๊ฐœ์˜ ์กฐ๊ฑด์€ ๊ฐ€์žฅ ์ตœ์‹ ์— ์ž‘์„ฑ๋œ ๊ฒƒ๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ 10๊ฐœ๋ผ๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์กฐ๊ฑด ์ค‘ ๊ฐ€์žฅ ์ตœ์‹ ์— ์ž‘์„ฑ๋œ ๊ฒƒ์ด๋ผ๋Š” ์กฐ๊ฑด์— ๋งž์ถฐ ๋ ˆํฌ์ง€ํ† ๋ฆฌ์— ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•  ๊ฒƒ์ด๋‹ค.(๋‚ด๋ฆผ์ฐจ์ˆœ DESC๋กœ) ๋‚˜๋จธ์ง€ ์กฐ๊ฑด์œผ๋กœ 10๊ฐœ๋งŒ ์ถœ๋ ฅ๋˜๋Š” ๊ฒƒ์ด ๋‚จ์•„์žˆ๋‹ค. 10๊ฐœ๋งŒ ์ถœ๋ ฅ์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ page๋ฅผ ์‚ฌ์šฉํ•˜๋Š”๋ฐ, ์ด๋•Œ ์ด ๋ฉ”์„œ๋“œ๋Š” service์— ์ž‘์„ฑํ•œ๋‹ค. service์— return ๊ฐ’์„ ๋ ˆํฌ์ง€ํ† ๋ฆฌ์—์„œ ๋ฐ›์•„(๋‚ด๋ฆผ์ฐจ์ˆœ) 10๊ฐœ๋งŒ ์ถœ๋ ฅ์‹œํ‚ค๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ ํ›„ controller์˜ model์„ ํ†ตํ•ด ๋ฐ›์•„ ํ™”๋ฉด์— ์ถœ๋ ฅ๋˜๋„๋ก ํ•  ๊ฒƒ์ด๋‹ค.

 

 

 


 

 

 

 

5. Entity์™€ Repository ์ž‘์„ฑํ•˜๊ธฐ.

 

โ–ก Repository

package com.LottoWeb.LuckyLotto.DB;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;


import java.util.List;

public interface BoardRepository extends JpaRepository<Comment, Long> {

    Page<Comment> findAllByOrderByIdDesc(Pageable pageable);
}

 

Jpa๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜๋ฅผ ๊ฐ„๋‹จํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. ํ•ด๋‹น ํ”„๋กœ์ ํŠธ์—์„œ ์ด์šฉํ•œ Jpa๊ธฐ๋Šฅ์€ ๋Œ“๊ธ€์„ ์ €์žฅํ•ด์ฃผ๋Š” save์™€ ๋Œ“๊ธ€์„ ์กฐํšŒํ•˜๋Š” page ๊ทธ๋ฆฌ๊ณ  ์ฟผ๋ฆฌ ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด  CRUD ์ง€์›์ด ์žˆ๋‹ค.

 

์—ฌ๊ธฐ์„œ ์ฟผ๋ฆฌ ๋ฉ”์„œ๋“œ๋Š” findAllByOrderByIdDesc๋กœ, ๋ชจ๋“  ๊ฒƒ์„ ์กฐํšŒํ•˜๊ณ  ๊ธฐ์ค€์€ ์•„์ด๋””๋กœ ํ•˜๋ฉฐ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋‚˜์—ดํ•œ๋‹ค ๋ผ๋Š” ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  Page<Comment> ํ˜•ํƒœ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

 

 

โ–ก Entity

package com.LottoWeb.LuckyLotto.DB;

import jakarta.persistence.*;
import lombok.*;


@Entity //๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์™€ ๋งคํ•‘ํ•œ๋‹ค.
@Getter
@NoArgsConstructor (access = AccessLevel.PROTECTED) //ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†๋Š” ๋””ํดํŠธ ์ƒ์„ฑ์ž ์ƒ์„ฑ
@Table(name = "Comment_List")
public class Comment {

    @Id //์—”ํ‹ฐํ‹ฐ์˜ ์ฃผ์š” ์‹๋ณ„์ž
    @GeneratedValue(strategy = GenerationType.IDENTITY) //์—”ํ‹ฐํ‹ฐ์˜ ์‹๋ณ„์ž์˜ ๊ฐ’์„ ์ž๋™์œผ๋กœ ์ƒ์„ฑ
    private Long id;

    // ํ•ด๋‹น ์—”ํ‹ฐํ‹ฐ ํ•„๋“œ๊ฐ€ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์˜ ์ปฌ๋Ÿผ์— ๋งคํ•‘๋  ๋•Œ ์ œ์•ฝ์„ ๊ฑธ์—ˆ๋‹ค.
    // 25์ž ๊ธธ์ด!
    @Column(length = 25)
    private String content;

    // ๋Œ“๊ธ€ content๋กœ ๋ฐ›์•„์˜จ ๊ฒƒ์„ ํ•„๋“œ์— ๋„˜๊ธฐ๋Š” ์ƒ์„ฑ์ž.
    public Comment(String content) {
        this.content = content;
    }

    /*
    @Builder
    public Comment(Long id, String content) {
        this.id = id;
        this.content = content;
    }
    */

}

 

์—”ํ‹ฐํ‹ฐ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค. ์—”ํ‹ฐํ‹ฐ๋Š” ๋ฐ์ดํ„ฐ์˜ ์ง‘ํ•ฉ์œผ๋กœ ์–ด๋–ค ์ •๋ณด๋“ค์ธ ์ธ์Šคํ„ด์Šค(๋Œ“๊ธ€์ด ํ•˜๋‚˜๋ฉด ์—”ํ‹ฐํ‹ฐ ์„ฑ๋ฆฝ ๋ถˆ๊ฐ€)๊ฐ€ ๋‘ ๊ฐœ ์ด์ƒ์ด ๋˜์–ด์•ผํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์œ ์ผํ•œ ์‹๋ณ„์ž๊ฐ€ ์žˆ์–ด์•ผํ•œ๋‹ค. ๋ฐ˜๋“œ์‹œ ์†์„ฑ์„ ํฌํ•จํ•ด์•ผํ•œ๋‹ค. ์—ฌ๊ธฐ์„œ ์œ ์ผํ•œ ์‹๋ณ„์ž๋กœ Id๋ฅผ ๋ถ€์—ฌํ–ˆ๋‹ค. ์ด๋Š” ์ค‘๋ณต๋  ์ˆ˜ ์—†๋Š” ๊ฒƒ์„ ์‹๋ณ„์ž๋กœ ๊ณจ๋ผ์•ผํ•œ๋‹ค. ์ด๋ฆ„์ด๋‚˜, ๋Œ“๊ธ€์„ ์‹๋ณ„์ž๋กœ ๊ณ ๋ฅด๋ฉด ์ค‘๋ณต๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•ˆ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์†์„ฑ์œผ๋กœ content๋ฅผ ๋„ฃ์—ˆ๋‹ค. ์ฆ‰ Comment ์—”ํ‹ฐํ‹ฐ์— content์ธ ๋Œ“๊ธ€ ์†์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. 

 

ํ•ด๋‹น ์ฝ”๋“œ๋Š” ์•„์ฃผ ๊ฐ„๋‹จํ•˜๊ณ  ์‰ฌ์šด ์ฝ”๋“œ๋‹ค. ๊ทธ์ € ๋Œ“๊ธ€์„ ๊บผ๋‚ด ์กฐํšŒ๋งŒ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— @setter๋‚˜ @Builder๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋‹ค. ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— JPA๋ฅผ ์ƒ์†ํ–ˆ๊ธฐ๋•Œ๋ฌธ์— JPA ๋ฆฌํ”Œ๋ ‰์…˜ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ“๊ธ€์„ ์กฐํšŒํ•˜๋Š”๋ฐ๋Š” ๋ฌธ์ œ๊ฐ€ ์—†๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ๋งŒ์•ฝ ๋Œ“๊ธ€์„ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜, ๋” ๋‹ค์–‘ํ•œ ์†์„ฑ์ด ์ƒ์„ฑ๋œ๋‹ค๋ฉด @Builder๋กœ ๊ด€๋ฆฌํ•ด์ฃผ๋ฉด ์ข‹๋‹ค. @setter๋Š” ์ˆ˜์ •์ด ๋ฌด๋ถ„๋ณ„ํ•˜๊ฒŒ ์ผ์–ด๋‚  ์ˆ˜ ์žˆ์–ด์„œ ์ถ”์ฒœํ•˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํ•œ๋‹ค.

 

 

 

 


 

 

 

 

 

6. ๋Œ“๊ธ€ ์ €์žฅํ•˜๊ธฐ

 

 

โ–ก Controller ์ž‘์„ฑํ•˜๊ธฐ.

@GetMapping("/save")
 public String saveComment(@RequestParam("content") String content) {
     boardService.saveComment(content);
     return "redirect:/board";
 }

 

save ์—”๋“œํฌ์ธํŠธ๋ฅผ ํ†ตํ•ด saveComment ๋ฉ”์„œ๋“œ์™€ ์—ฐ๊ฒฐํ•ด์ฃผ์—ˆ๋‹ค. ์•„๊นŒ html์„ ์ž‘์„ฑํ–ˆ์„๋•Œ, ๋Œ“๊ธ€์„ content๋กœ ๋ณด๋ƒˆ๋‹ค. ์ด content๋ฅผ ๋ฐ›์•„์„œ boardService๋กœ ๋ณด๋‚ด๋Š” ์—ญํ• ์„ ํ•˜๋Š” ๊ฒƒ์ด @RequestParam์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. @RequestParam์€ ๋Œ“๊ธ€ content๋ฅผ ๋ฐ›๊ณ  String ํƒ€์ž…์˜ content์— ๋„ฃ์–ด boardService ํด๋ž˜์Šค์˜ saveComment ๋ฉ”์„œ๋“œ๋กœ ๋ณด๋‚ธ๋‹ค. ์‹ค์ œ๋กœ ๋ฐ›์•„์˜ค๋Š” ์ด๋ฆ„๊ณผ ๊ฐ’์˜ ์ด๋ฆ„์ด ์ผ์น˜ํ•˜๋Š” ๊ฒฝ์šฐ ("content")๋Š” ์ƒ๋žต๊ฐ€๋Šฅํ•˜๋‹ค.

 

@RequestParam์„ ์‚ฌ์šฉํ•˜๋ฉด HttpServletRequest์˜ request.getParameter๋กœ ๋ฐ›์•„์ค˜์•ผํ•˜๋Š” ๊ฒƒ์„ ์‰ฝ๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. ๋งŒ์•ฝ @RequestParam์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค๋ฉด...?

@GetMapping("/save")
public saveComment(HttpServletRequest request) {
    String content = request.getParameter("content");   
    boardService.saveComment(content);   
    return "redirect:/board";
 }

 

ํ˜„์žฌ ํ”„๋กœ์ ํŠธ์—์„œ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ํ•˜๋‚˜ ๋ฐ–์— ์—†์ง€๋งŒ, ๋งŒ์•ฝ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๊ฒฝ์šฐ @RequestParam์ด ์—„์ฒญ ํšจ์œจ์ ์ผ ๊ฒƒ์ด๋‹ค.

 

 

 

โ–ก Service ์ž‘์„ฑํ•˜๊ธฐ.

public void saveComment(String content) {
    if( content.length() >= 1 && content.length() <= 25 )
        boardRepository.save(new Comment(content));
}

 

๋Œ“๊ธ€์ด ๋ ˆํฌ์ง€ํ† ๋ฆฌ๋กœ ์ €์žฅ๋˜๊ธฐ ์ „, ์กฐ๊ฑด์— ๋งž๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. if๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ content์˜ ๊ธธ์ด๊ฐ€ 1 ์ด์ƒ 25 ์ดํ•˜๋ฉด Comment ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ content๋ฅผ ๋ ˆํฌ์ง€ํ† ๋ฆฌ์— ์ €์žฅํ•˜๋„๋ก ์ž‘์„ฑํ–ˆ๋‹ค. ์•ž์„œ์„œ Entity์ธ Comment ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด๋’€๋‹ค. Comment ํด๋ž˜์Šค์˜ ์ƒ์„ฑ์ž๊ฐ€ content(๋Œ“๊ธ€)๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„ ํ•„๋“œ์— ์ €์žฅํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋งคํ•‘ํ•œ๋‹ค.

 

 

 

 

 


 

 

 

7. ๋Œ“๊ธ€ ์กฐํšŒํ•˜๊ธฐ.

 


โ–ก Service
 ์ž‘์„ฑํ•˜๊ธฐ.

public Page<Comment> get10Comments() {
    Pageable pageable = PageRequest.of(0,10);
    return boardRepository.findAllByOrderByIdDesc(pageable);

}

 

Jpa์—์„œ ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜ pageable๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ“๊ธ€์„ ๋‚˜ํƒ€๋‚ผ ๊ฒƒ์ด๋‹ค. ๋ฉ”์„œ๋“œ ์ด๋ฆ„์€ get10Comments, ๋ฐ˜ํ™˜ํƒ€์ž…์€ Page<Comment>๊ฐ€ ๋œ๋‹ค. ํ•œ ํŽ˜์ด์ง€์— ๋Œ“๊ธ€ 10๊ฐœ๋งŒ ๋‚˜ํƒ€๋‚ด๋„๋ก ํ• ๊ฑฐ๋‹ˆ๊นŒ PageRequest.of(int page, int size)๋กœ page๊ฐ€ ๊ฐ€์ ธ์˜ฌ ํŽ˜์ด์ง€ ๊ทธ๋ฆฌ๊ณ  ํ•œ ํŽ˜์ด์ง€์— ํฌํ•จ๋  ๊ฐœ์ˆ˜๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ํ˜„์žฌ ์ฝ”๋“œ์—์„œ ๋ฐ˜ํ™˜ํƒ€์ž…์„ Page๋กœ ์„ค์ •ํ–ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ํ•ด๋‹น ํ”„๋กœ์ ํŠธ๋Š” ์—ฌ๋Ÿฌ ํŽ˜์ด์ง€๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— List ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•ด๋„ ์ƒ๊ด€์—†๋‹ค. (์•„์˜ˆ pageable์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋œ๋‹ค.)

 

 

 

โ–ก Controller ์ž‘์„ฑํ•˜๊ธฐ.

@GetMapping("/board")
public String boardPage(Model model) {
    model.addAttribute("comments", boardService.get10Comments());
    return "board";
}

 

์ฒซ ํ™”๋ฉด์ด ๋“ค์–ด๊ฐ€๋ฉด ์ €์žฅํ•œ ๋Œ“๊ธ€์ด ๋‚˜ํƒ€๋‚˜๋„๋ก ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ํƒ€์ž„๋ฆฌํ”„์˜ Model์„ ํ†ตํ•ด ๋Œ“๊ธ€์„ ๋ฐ›์•„์˜ฌ ๊ฒƒ์ด๋‹ค.  ๋ฐฉ๊ธˆ Service ํด๋ž˜์Šค์— get10Comments( ) ๋ฉ”์„œ๋“œ๋ฅผ ์ •์˜ํ–ˆ๋‹ค. ๋ฉ”์„œ๋“œ๋กœ ๋ฐ›์•„์˜จ ๊ฐ’(๋ฐ์ดํ„ฐ) comments๋ฅผ ๋ทฐ์ธ ํƒ€์ž„๋ฆฌํ”„๋กœ ์ „๋‹ฌํ•œ๋‹ค.

 

@GetMapping("/save")
 public String saveComment(@RequestParam("content") String content) {
     boardService.saveComment(content);
     return "redirect:/board";
 }

 

๋Œ“๊ธ€์ด ์ €์žฅ๋˜์ž๋งˆ์ž ํ™”๋ฉด์— ๋ฐ”๋กœ ๋ณด์ด๋„๋ก ํ•˜๊ณ  ์‹ถ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด redirect:๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ /board๋กœ ์—”๋“œํฌ์ธํŠธ๋ฅผ ๋ณด๋‚ด๋„๋ก ํ•œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ์œ„ ์ฝ”๋“œ @GetMapping("/board")๋กœ ์•ค๋“œํฌ์ธํŠธ๋ฅผ ๋ฐ›์•„ ์ƒˆ๋กœ์šด comments๋ฅผ ๋ฐ›์•„ ํƒ€์ž„๋ฆฌํ”„๋กœ ์ „๋‹ฌํ•˜๊ฒŒํ•œ๋‹ค.

 

 

โ–ก html ์‚ดํ”ผ๊ธฐ.

<div class="commentBox">
    <li class="font" th:each="comment : ${comments}" th:text="${comment.content}">
        comment
    </li>
</div>

 

board.html ์ฝ”๋“œ๋ฅผ ๋‹ค์‹œ ์‚ดํŽด๋ณด๊ฒ ๋‹ค. ๋Œ“๊ธ€์„ ์กฐํšŒํ•˜๊ธฐ ์œ„ํ•ด ๋ฐ์ดํ„ฐ๋ฅผ comments๋กœ ๋ณด๋ƒˆ๊ณ , comments์˜ ์—ฌ๋Ÿฌ ๋Œ“๊ธ€์„ each ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด comment๋ผ๋Š” ๋ณ€์ˆ˜๋กœ ๊ฐ ํ•ญ๋ชฉ์„ ์‚ฌ์šฉํ–ˆ๋‹ค. ๊ทธ๋Ÿฐ๋ฐ ์—ฌ๊ธฐ์„œ th:text="${comment.content}"๊ฐ€ ์žˆ๋‹ค. comment ํด๋ž˜์Šค์— ์žˆ๋Š” content ํ•„๋“œ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์ด๋‹ค.

 

์ข€ ๋” ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•ด๊ฒ ๋‹ค. comments๋Š” 10๊ฐœ์˜ ๋ญ‰ํ……์ด ๋Œ“๊ธ€์ด๋‹ค. ๊ทธ๊ฑธ comment ๋ณ€์ˆ˜๋กœ 1comment 2comment... ์ด๋ ‡๊ฒŒ ๋ถ€์—ฌ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋‚˜์—ด๋œ ๊ฒƒ๋“ค ์•ˆ์— ์‹ค์ œ ๋Œ“๊ธ€์„ ๋„ฃ์–ด์ฃผ๋Š” ๊ฒƒ์ด๋‹ค. ๋น„์œ ๋ฅผ ๋“ค์ž๋ฉด comments๊ฐ€ ์ขŒ์„ ํ•œ ์ค„์ด ๋˜๊ณ , comment๋Š” ์ขŒ์„ ํ•˜๋‚˜ ํ•˜๋‚˜์— ์•‰๋Š” ์‚ฌ๋žŒ์ด ๋˜๊ณ , comment.content๋Š” ๊ทธ ์‚ฌ๋žŒ์˜ ์ด๋ฆ„ํ‘œ ํ˜น์€ ์ธ์  ์‚ฌํ•ญ์ด ๋˜๋Š” ๊ฒƒ์ด๋ผ๊ณ  ๋ณด๋ฉด ์‰ฌ์šธ ๊ฒƒ ๊ฐ™๋‹ค. ์ผ๋‹จ ๋‚˜๋Š” ๊ทธ๋ ‡๊ฒŒ ์ดํ•ดํ–ˆ๋‹ค.

 

 

 


 

 

[ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค H2 ์„ค์น˜ ๋ฐ ์—ฐ๋™, JPA ์—ฐ๋™ํ•˜๊ธฐ ]

 

Spring - [intelliJ,Thymeleaf] MAC, H2๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฐ„๋‹จํ•œ ๋Œ“๊ธ€์ฐฝ ๋งŒ๋“ค๊ธฐ(2)

[ ๊ฐ„๋‹จํ•œ ๋Œ“๊ธ€์ฐฝ ๋งŒ๋“ค๊ธฐ (1) ] Spring - [intelliJ,Thymeleaf] ๊ฐ„๋‹จํ•œ ๋Œ“๊ธ€์ฐฝ ๋งŒ๋“ค๊ธฐ(1)[ @PostMapping์œผ๋กœ ๋žœ๋คํ•œ ์ˆซ์ž 6๊ฐœ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ ] Spring - [intelliJ,Thymeleaf] @PostMapping์œผ๋กœ ๋žœ๋ค ์ˆซ์ž 6๊ฐœ ์—…๋ฐ์ดํŠธํ•˜๊ธฐ[

post-this.tistory.com

 

 

๐Ÿชฝ

"ํ‹€๋ฆฐ ๋‚ด์šฉ์ด ์žˆ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค."

 

 

 

[ ๋ธ”๋กœ๊ทธ์— ์“ด ๋‚ด์šฉ๋“ค์˜ ์ถœ์ฒ˜๋Š” ์•„๋ž˜์— ์ ์–ด๋‘˜ํ…Œ๋‹ˆ

๋“ค์–ด๊ฐ€์„œ ์ฝ์–ด๋ณด์‹œ๋ฉด ๋”์šฑ ๋„์›€์ด ๋˜์‹ค ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค ]

 

 

 

 

- paging ์ถœ์ฒ˜

 

JPA Paging (ํŽ˜์ด์ง€ ๋‚˜๋ˆ„๊ธฐ)

ํ˜„์žฌ 2๊ฐœ์˜ ํ”„๋กœ์ ํŠธ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ๋‹ค. > ๋‘ ํ”„๋กœ์ ํŠธ ๋ชจ๋‘ ๊ธ€ ๋˜๋Š” ์˜์ƒ์„ ํŽ˜์ด์ง•ํ•ด์„œ ํ”„๋ก ํŠธ์— ๋ฟŒ๋ ค์ฃผ๋Š” API๊ฐ€ ํ•„์š”ํ–ˆ๋‹ค. > ๊ทธ๋ž˜์„œ ์ด๋ฒˆ์— PageRequest๋ฅผ ์‚ฌ์šฉํ•ด ํŽ˜์ด์ง• ํ•˜๋Š” ๊ฒƒ์„ ๊ณต๋ถ€ํ–ˆ๊ณ  ๊ณต์œ ํ•˜๊ณ 

velog.io

 

 

 

- Pageable ์ถœ์ฒ˜

 

[JPA] Spring Data Jpa + Pageable๋กœ Pagination ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•˜๊ธฐ

์ด๋ฒˆ์ฃผ๋Š” ๋…์„œ๋ฆฌ๋ทฐ๋ฅผ ๋‚จ๊ธธ ์ˆ˜ ์žˆ๋Š” ๋ฏธ๋‹ˆ ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ  ์žˆ๋‹ค. ๋ฆฌ๋ทฐ ๊ธ€์„ ํŠน์ • ์กฐ๊ฑด์œผ๋กœ ๋ฝ‘์•„์„œ ์ •๋ ฌ ๊ธฐ์ค€์„ ์„ ํƒํ•˜์—ฌ ์กฐํšŒํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜๊ณ ์ž ํ•œ๋‹ค.์ „์ฒด ๋ฆฌ๋ทฐ๊ธ€์„ ์กฐํšŒํ•˜๊ฑฐ๋‚˜, ๋‚ด๊ฐ€

velog.io

 

 

 

- ์—”ํ‹ฐํ‹ฐ ์ถœ์ฒ˜ 

 

[DB] ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ์—”ํ„ฐํ‹ฐ(Entity)๋ž€ ๋ฌด์—‡์ธ๊ฐ€?

์—”ํ„ฐํ‹ฐ(Entity)๋ž€? ์—”ํ„ฐํ‹ฐ(Entity)๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฒˆ์—ญํ•˜๋ฉด ์‹ค์ œ, ๋…๋ฆฝ์ฒด๋ผ๋Š” ๋œป์œผ๋กœ ๋ฐ์ดํ„ฐ ๋ชจ๋ธ๋ง์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฐ์ฒด๋ผ๊ณ  ์ƒ๊ฐํ•˜์‹œ๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค. ์ฆ‰ ์—”ํ„ฐํ‹ฐ(Entity)๋Š” ์—…๋ฌด์— ํ•„์š”ํ•˜๊ณ  ์œ ์šฉํ•œ ์ •๋ณด๋ฅผ ์ €

coding-factory.tistory.com

 

 

 

- NoargsConstructor ์ถœ์ฒ˜

 

@NoargsConstructor(AccessLevel.PROTECTED) ์™€ @Builder

@NoargsConstructor(AccessLevel.PROTECTED) ์™€ @Builder๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ• ๋•Œ ์ฃผ์˜ํ•  ์ ์— ๋Œ€ํ•ด์„œ ์„œ์ˆ ํ•ฉ๋‹ˆ๋‹ค. "์™œ" ์•ˆ๋˜๋Š”์ง€์™€ "์™œ" ์ด๋ ‡๊ฒŒ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ๋Š”์ง€์— ๋Œ€ํ•ด ์ง‘์ค‘ํ•˜์—ฌ ์„œ์ˆ ํ•ฉ๋‹ˆ๋‹ค. 1. ์™œ NoargsConstructor(Access

cobbybb.tistory.com

 

 

 

- NoargsConstructor ์ถœ์ฒ˜

 

๋กฌ๋ณต @All/NoArgsConstructor ์ œ๋Œ€๋กœ ์•Œ๊ณ  ์‚ฌ์šฉํ•ด๋ณด์ž.

๋ฌธ๋“ ์ „ ํšŒ์‚ฌ ๋ ˆ๊ฑฐ์‹œ ์ฝ”๋“œ๋ฅผ ์ƒ๊ฐํ•˜๋‹ค๊ฐ€ @AllArgsConstructor, @NoArgsConstructor ๋“ฑ ์—ฌ๋Ÿฌ ์–ด๋…ธํ…Œ์ด์…˜์ด ํ•œํด๋ž˜์Šค์— ๊ฐ™์ด ์ž‘์„ฑ๋˜์–ด์žˆ๋Š” ๊ฒƒ์„ ๋ณธ์ ์ด์žˆ๋‹ค.Lombok์€ ๋ถˆํ•„์š”ํ•œ ์ฝ”๋“œ์™€ ์ž‘์—…์„ ์ค„์—ฌ์ฃผ๋Š” ์ข‹์€ ๋ผ

velog.io

 

 

 

- @builder ์ถœ์ฒ˜

 

[Java] ๋นŒ๋” ํŒจํ„ด(Builder Pattern)์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ์ด์œ 

๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ƒ์„ฑ์ž ํŒจํ„ด, ์ •์  ๋ฉ”์†Œ๋“œ ํŒจํ„ด, ์ˆ˜์ •์ž ํŒจํ„ด, ๋นŒ๋” ํŒจํ„ด ๋“ฑ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐœ์ธ์ ์œผ๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ์—๋Š” ๋ฐ˜๋“œ์‹œ ๋นŒ๋” ํŒจํ„ด์„ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋Š”

mangkyu.tistory.com

 

 

 

- Entity ์ถœ์ฒ˜

 

[SpringBoot] Entity๋ž€? + @Table, @NoArgsConstructor, @AllArgsConstructor

๐Ÿ’ก Entity๋ž€ ๋ฌด์—‡์ด๋ฉฐ, ์—”ํ‹ฐํ‹ฐ ํด๋ž˜์Šค ์•ˆ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์ž. Entity(์—”ํ‹ฐํ‹ฐ)๋ž€? ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์—์„œ ์˜์†์ ์œผ๋กœ ์ €์žฅ๋˜๊ณ  ๊ด€๋ฆฌ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ์—”ํ‹ฐํ‹ฐ๋Š” ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค

dahlia15.tistory.com

 

 

 

 

- Entity์™€ Repository ์ถœ์ฒ˜

 

[Spring] Spring Entity, Repository ์„ค๋ช…

Spring Entity, Repository๋Š” DB์™€ ๊ด€๋ จ๋œ ํ•ญ๋ชฉ๋“ค ์ค‘ ๋ช‡ ๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. ๊ทธ ์™ธ์—๋Š” DAO, DTO, VO, Transaction ๋“ฑ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๊ธ€์ด ์œ ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค. Repository์— ๋Œ€ํ•ด์„œ ์ฐธ๊ณ ํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

workshop-6349.tistory.com

 

 

 

- Getter ์ถœ์ฒ˜

 

[OOP] Getter์™€ Setter๋Š” ์ง€์–‘ํ•˜๋Š”๊ฒŒ ์ข‹๋‹ค

๋ชฉ์ฐจ ๋“ค์–ด๊ฐ€๊ธฐ ์ „์— ์–ผ๋งˆ ์ „ ์‚ฌ๋‚ด์—์„œ Getter์™€ Setter๋ฅผ ํ•จ๋ถ€๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋˜๋Š” ์ด์œ ์— ๋Œ€ํ•œ ์„ธ๋ฏธ๋‚˜๊ฐ€ ์žˆ์—ˆ๋‹ค. Setter์— ๋Œ€ํ•œ ์ด์•ผ๊ธฐ๋Š” ์›Œ๋‚™ ๋งŽ์ด ์•Œ๋ ค์ ธ์žˆ์—ˆ์ง€๋งŒ Getter์— ๋Œ€ํ•œ ์ด์•ผ๊ธฐ๋Š” ์ž˜ ํ•˜์ง€ ์•Š

colabear754.tistory.com

 

 

 

- Jpa ์ถœ์ฒ˜

 

[Spring JPA] JPA ๋ž€?

์ด๋ฒˆ ๊ธ€์—์„œ๋Š” JPA(Java Persistence API)๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ์•„๋ณด๋ ค๊ณ ํ•œ๋‹ค. JPA๋Š” ์ž๋ฐ” ์ง„์˜์—์„œ ORM(Object-Relational Mapping) ๊ธฐ์ˆ  ํ‘œ์ค€์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค์˜ ๋ชจ์Œ์ด๋‹ค. ๊ทธ ๋ง์€ ์ฆ‰, ์‹ค์ œ์ ์œผ๋กœ ๊ตฌํ˜„๋œ๊ฒƒ์ด

dbjh.tistory.com

 

 

 

- Jpa ์ถœ์ฒ˜

 

[JAVA] - JPA๋ž€? ๊ฐ„๋‹จํ•˜๊ณ  ์‰ฝ๊ฒŒ ์ดํ•ดํ•˜๊ธฐ(์˜ˆ์ œ์ฝ”๋“œ ์™„๋ฒฝ์ •๋ฆฌ)

๐Ÿง‘๐Ÿป‍๐Ÿซ ๊ฐ„๋‹จ ์ •๋ฆฌ JPA๋Š” ์ž๋ฐ”์˜ ORM๊ธฐ์ˆ ์„ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ํ•˜๋„๋ก ๋„์™€์ฃผ๋Š” API์ด๋‹ค. JpaRepository๋ฅผ ์ƒ์†ํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค์— ๋ฉ”์„œ๋“œ ์ด๋ฆ„๋งŒ ์ ์–ด๋†“์œผ๋ฉด ์•Œ์•„์„œ ๋‹ค ์ฒ˜๋ฆฌ(๊ตฌํ˜„์ฒด ์ƒ์„ฑ, ์ฟผ๋ฆฌ๋ฌธ ๊ตฌํ˜„ ๋“ฑ)ํ•ด

ccomccomhan.tistory.com

 

 

 

- save ์ถœ์ฒ˜

 

๋‚ด๊ฐ€ repository.save()๋ฅผ ํ†ตํ•ด์„œ ๊ฐ์ฒด๋ฅผ ์ €์žฅ์‹œ์ผฐ๋˜ ์ด์œ 

๊ทธ ๋•Œ ๋‹น์‹œ์—๋Š” Spring์€ ๋ฌผ๋ก  Java์— ๋Œ€ํ•œ ๊ฐœ๋…๋„ ๋งค์šฐ ์•ฝํ•  ๋•Œ๋ผ ์ง€๊ธˆ๋„ ์•ฝํ•˜์ง€๋งŒ > > > ๋ ˆํผ๋Ÿฐ์Šค์˜ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ ธ๋‹ค ์‚ฌ์šฉํ•˜๊ธฐ์— ๊ธ‰๊ธ‰ํ–ˆ๋‹ค. > > ๋‹น์‹œ์˜ ๋‚˜๋Š” ํ•ด๋‹น ๊ฐœ๋…์— ๋Œ€ํ•œ ์ •ํ™•ํ•œ ์ดํ•ด๋ณด๋‹ค๋Š” ๊ธฐ

velog.io

 

 

 

- save ์ถœ์ฒ˜

 

Spring Data JPA - Save Method ๋™์ž‘ ๋ฐฉ์‹

Spring Data JPA์—์„œ ์ œ๊ณตํ•˜๋Š” JpaRepository.save(T); [T๋Š” Entity]์˜ ๋‚ด๋ถ€ ๋™์ž‘ ๋ฐฉ์‹์— ๋Œ€ํ•ด์„œ ์ •๋ฆฌํ•˜๋ ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. JPA์—์„œ์˜ save ๋ฉ”์„œ๋“œ ๊ตฌํ˜„์ฒด ์ฝ”๋“œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. // SimpleJpaRepository.java [JPA ๊ตฌํ˜„์ฒด] @Tra

wangtak.tistory.com

 

 

 

- CRUD ์ถœ์ฒ˜

 

[Spring] JPA์˜ ๊ธฐ๋ณธ CRUD๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์ž !

JPA (Java Presistence API) ์ž๋ฐ” ์ง„์˜์—์„œ ORM ๊ธฐ์ˆ  ํ‘œ์ค€์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค ๋ชจ์Œ์ด๋‹ค. ๋Œ€ํ‘œ์ ์œผ๋กœ Hibernate, Spring JPA ๊ฐ€ ์žˆ๋‹ค. ์‰ฝ๊ฒŒ ๋งํ•˜๋ฉด JPA๋Š” ๊ฐ์ฒด์™€ ํ…Œ์ด๋ธ”์„ ์ž๋™์œผ๋กœ ๋งคํ•‘์‹œ์ผœ์„œ, ์ฟผ๋ฆฌ๋ฌธ ์ž‘

wooj-coding-fordeveloper.tistory.com

 

 

 

- findAllByOrderByIdDesc ์ถœ์ฒ˜

 

JPA - findAllByOrderByIdDesc

findAll์„ ํ•˜๋ฉด์„œ Orderby๋ฅผ ํ•  ํ•„์š”๊ฐ€ ์žˆ์—ˆ๋‹ค. https://docs.spring.io/spring-data/jpa/docs/2.3.3.RELEASE/reference/html/#jpa.repositories And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname = ?2 Or findByLastnameOrFirstname

mymoney1.tistory.com

 

 

+ Recent posts