์ด๋ฒ ์ฅ์ ์์ ์์๋ Querydsl ์์ฃผ์ ์์ ๋ก ๋ค๋ฅธ ๊ฐ๋ฐ ์ ์ ํ ์คํธ๋ฅผ ์งํํฉ๋๋ค.
GuestbookRepositoryTests์๋ 300๊ฐ์ ํ ์คํธ๋ฐ์ดํฐ๋ฅผ ๋จผ์ ๋ฃ๋๋ก ํฉ๋๋ค.
package org.zerock.guestbook.repository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.zerock.guestbook.entity.Guestbook;
import java.util.stream.IntStream;
@SpringBootTest
public class GuestbookRepositoryTests {
@Autowired
private GuestbookRepository guestbookRepository;
@Test
public void insertDummies(){
IntStream.rangeClosed(1,300).forEach(i ->{
Guestbook guestbook = Guestbook.builder()
.title("Title...."+i)
.content("Content..."+i)
.writer("user"+(i%10))
.build();
System.out.println(guestbookRepository.save(guestbook));
});
}
}
์์ ์ฝ๋๋ฅผ ์คํํด ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ 300๊ฐ์ ๋ฐ์ดํฐ๊ฐ ์์ฑ๋์๋์ง ํ์ธํฉ๋๋ค.
์์ฑ๋ ๋ฐ์ดํฐ์ regdate(์์ฑ์๊ฐ), moddate(์์ ์๊ฐ)์ด ์๋์ผ๋ก
null์ด ์๋ ๊ฐ์ผ๋ก ์ฑ์์ง๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
์์ ์๊ฐ ํ ์คํธ
์ํฐํฐํด๋์ค์์ ํ์์ ๋ฐ๋ผ์ ์์ ๊ธฐ๋ฅ์ ๋ง๋ค๊ธฐ๋ ํฉ๋๋ค.
์์ ์์๋ ์ ๋ชฉ๊ณผ ๋ด์ฉ์ ์์ ํ ์ ์๋๋ก changeTitle(), changeContent()์ ๊ฐ์
๋ฉ์๋๋ฅผ ์ถ๊ฐํ๋๋ก Guestbook ํด๋์ค๋ฅผ ์์ ํฉ๋๋ค.
public class Guestbook extends BaseEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long gno;
@Column(length = 100, nullable = false)
private String title;
@Column(length = 1500, nullable = false)
private String content;
@Column(length = 50, nullable = false)
private String writer;
public void changeTitle(String title){
this.title = title;
}
public void changeContent(String content){
this.content= content;
}
}
BaseEntity์ modDate๋ ์ต์ข ์์ ์๊ฐ์ด ๋ฐ์๋๊ธฐ ๋๋ฌธ์
ํน์ ํ ์ํฐํฐ๋ฅผ ์์ ํ ํ์ save()ํ์ ๊ฒฝ์ฐ์ ๋์ํ๊ฒ ๋ฉ๋๋ค.
์ ์์ ์ผ๋ก ๋์ํ๋ ์ง๋ฅผ ํ์ธํ๊ธฐ ์ํด์ ํ ์คํธ ์ฝ๋๋ฅผ ์์ฑํฉ๋๋ค.
@Test
public void updateTest(){
Optional<Guestbook> result = guestbookRepository.findById(300L);
//์กด์ฌํ๋ ๋ฒํธ๋ก ํ
์คํธ
if(result.isPresent()){
Guestbook guestbook = result.get();
guestbook.changeTitle("Changed Title....");
guestbook.changeContent("Changed Content....");
guestbookRepository.save(guestbook);
}
}
์์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด๋ฉด 300๋ฒ์ moddate์ regdate๊ฐ ๋ค๋ฅธ ๊ฐ์ ๊ฐ์ง๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
4.3.3 Querydsl ํ ์คํธ
๋ณธ๊ฒฉ์ ์ผ๋ก Querydsl์ ์ค์ต์ ๋ค์๊ณผ ๊ฐ์ ์ํฉ์ ์ฒ๋ฆฌํฉ๋๋ค.
- '์ ๋ชฉ/๋ด์ฉ/์์ฑ์'์ ๊ฐ์ด ๋จ ํ๋์ ํญ๋ชฉ์ผ๋ก ๊ฒ์ํ๋ ๊ฒฝ์ฐ
- '์ ๋ชฉ+๋ด์ฉ'/'๋ด์ฉ+์์ฑ์'/'์ ๋ชฉ+์์ฑ์'์ ๊ฐ์ด 2๊ฐ์ ํญ๋ชฉ์ผ๋ก ๊ฒ์ํ๋ ๊ฒฝ์ฐ
- ์ ๋ชฉ + ๋ด์ฉ + ์์ฑ์์ ๊ฐ์ด 3๊ฐ์ ํญ๋ชฉ์ผ๋ก ๊ฒ์ํ๋ ๊ฒฝ์ฐ
๋ง์ผ ๋ ๋ง์ ๋ฉค๋ฒ ๋ณ์๋ค์ด ์ ์ธ๋์ด ์์๋ค๋ฉด ์ด๋ฌํ ์กฐํฉ์ ์๋ ์์ฒญ ๋ง์์ง๊ฒ ๋ฉ๋๋ค.
์ด๋ฐ ์ํฉ์ ๋๋นํด์ ์ํฉ์ ๋ง๊ฒ ์ฟผ๋ฆฌ๋ฅผ ์ฒ๋ฆฌํ ์ ์๋ Querysdl์ด ํ์ํฉ๋๋ค.
Querydsl์ ์ฌ์ฉ๋ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- BooleanBuilder๋ฅผ ์์ฑํฉ๋๋ค.
- ์กฐ๊ฑด์ ๋ง๋ ๊ตฌ๋ฌธ์ Querydsl์์ ์ฌ์ฉํ๋ Predicate ํ์ ์ํจ์๋ฅผ ์์ฑํฉ๋๋ค.
- BooleanBuilder์ ์์ฑ๋ Predicate๋ฅผ ์ถ๊ฐํ๊ณ ์คํํฉ๋๋ค.
๋จ์ผํญ๋ชฉ ๊ฒ์ ํ ์คํธ
'Spring > ๐ฅ[spring]์ฝ๋๋ก ๋ฐฐ์ฐ๋ ์คํ๋ง ๋ถํธ ์น ํ๋ก์ ํธ_๊ตฌ๋ฉ๊ฐ๊ฒ ์ฝ๋ฉ๋จ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
4.4.1 ๋ฑ๋ก๊ณผ DTO๋ฅผ ์ํฐํฐ๋ก ๋ณํํ๊ธฐ (0) | 2023.03.03 |
---|---|
Part2. ๋จ์ผ ํญ๋ชฉ ๊ฒ์ ํ ์คํธ (0) | 2023.02.24 |
4.3.1 ๋์ ์ฟผ๋ฆฌ ์ฒ๋ฆฌ๋ฅผ ์ดํ Querydsl ์ค์ (0) | 2023.02.13 |
4.2 ์๋์ผ๋ก ์ฒ๋ฆฌ๋๋ ๋ ์ง/์๊ฐ ์ค์ (0) | 2023.02.07 |
ํ๋ก์ ํธ ๊ตฌ์กฐ ๋ง๋ค๊ธฐ (0) | 2023.02.06 |