📚 공부한 책 : 코드로배우는 스프링 부트 웹프로젝트
❤️ github 주소 : https://github.com/qkralswl689/LearnFromCode/tree/main/guestbook2022
더미데이터 생성 테스트
★ 생성된 데이터의 regdate , moddate 이 자동으로 값이 채워진것을 확인할 수 있다
♡ 자동으로 처리되는 날짜/시간 설정 보기
♡ 자동으로 처리되는 날짜/시간 적용 보기
import com.example.guestbook2022.entity.Guestbook;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.stream.IntStream;
@SpringBootTest
public class GuestbookRepositoryTests {
@Autowired
private GuestbookRepository guestbookRepository;
@Test // 더미데이터생성
public void insertDDumies(){
IntStream.rangeClosed(1,300).forEach(i -> {
Guestbook guestbook = Guestbook.builder()
.title("Title..." + i)
.content("Content..." + i)
.writer("user" + (i % 10))
.build();
guestbookRepository.save(guestbook);
});
}
}
작성글 수정 시간 테스트
- Entity 클래스에 메서드 추가 후 테스트 코드 작성
import lombok.*;
import javax.persistence.*;
@Entity
@Getter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class Guestbook extends BaseEntity{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // index 자동생성
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;
}
}
import com.example.guestbook2022.entity.Guestbook;
import com.example.guestbook2022.entity.QGuestbook;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Optional;
@SpringBootTest
public class GuestbookRepositoryTests {
@Autowired
private GuestbookRepository guestbookRepository;
@Test // 수정시간 테스트
public void updateTest(){
// 300 번 선택
Optional<Guestbook> result = guestbookRepository.findById(300L);
// 존재하는 번호로 테스트
// isPresent() : ! = null 과 같다
if (result.isPresent()){
Guestbook guestbook = result.get();
guestbook.changeTitle("Changed Title...");
guestbook.changeContent("Changed Content...");
guestbookRepository.save(guestbook);
}
}
}
♦︎ Querydsl 사용법
- BooleanBuilder생성 -> 쿼리의 where문에 들어가는 조건을 넣어주는 컨테이너
- 조건에 맞는 구문은 Predicate 타입의 함수 생성
- BooleanBuilder에 작성된 Predicate를 추가하고 실행
테스트 조건 : title 에 1이 포함된 내용 출력
import com.example.guestbook2022.entity.Guestbook;
import com.example.guestbook2022.entity.QGuestbook;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import java.util.Optional;
import java.util.stream.IntStream;
@SpringBootTest
public class GuestbookRepositoryTests {
@Autowired
private GuestbookRepository guestbookRepository;
@Test // Querydsl 테스트 - 단일항목테스트(title 에 1이 포함된 내용 출력)
public void testQueryy1(){
// 페이징 처리
Pageable pageable = PageRequest.of(0,10, Sort.by("gno").descending());
// findAll -> JpaRepository but Querydsl -> QuerydslPredicateExecutor 사용
// Predicate
// Q도메인 클래스를 사용하면 엔티티 클래스에 선언된 필드를 변수로 사용할 수 있다
QGuestbook qGuestbook = QGuestbook.guestbook;
// BooleanBuilder : 쿼리의 where문에 들어가는 조건을 넣어주는 컨테이너
BooleanBuilder booleanBuilder = new BooleanBuilder();
// title like %1% 쿼리를 코드로 구현
BooleanExpression expression = qGuestbook.title.contains("1");
// 만들어진 조건 결합
booleanBuilder.and(expression);
// BooleanBuilder는 guestbookRepository에 추가된 QuerydslPredicateExecutor 인터페이스의 findAll()을 사용할 수 있다
Page<Guestbook> result = guestbookRepository.findAll(booleanBuilder,pageable);
result.stream().forEach(guestbook -> {
System.out.println(guestbook);
});
}
}
Hibernate:
select
guestbook0_.gno as gno1_0_,
guestbook0_.moddate as moddate2_0_,
guestbook0_.regdate as regdate3_0_,
guestbook0_.content as content4_0_,
guestbook0_.title as title5_0_,
guestbook0_.writer as writer6_0_
from
guestbook guestbook0_
where
guestbook0_.title like ? escape '!' // 조건처리가 진행된것을 확인할 수 있다
order by
guestbook0_.gno desc limit ?
Hibernate:
select
count(guestbook0_.gno) as col_0_0_
from
guestbook guestbook0_
where
guestbook0_.title like ? escape '!'
Guestbook(gno=291, title=Title...291, content=Content...291, writer=user1)
Guestbook(gno=281, title=Title...281, content=Content...281, writer=user1)
Guestbook(gno=271, title=Title...271, content=Content...271, writer=user1)
Guestbook(gno=261, title=Title...261, content=Content...261, writer=user1)
Guestbook(gno=251, title=Title...251, content=Content...251, writer=user1)
Guestbook(gno=241, title=Title...241, content=Content...241, writer=user1)
Guestbook(gno=231, title=Title...231, content=Content...231, writer=user1)
Guestbook(gno=221, title=Title...221, content=Content...221, writer=user1)
Guestbook(gno=219, title=Title...219, content=Content...219, writer=user9)
Guestbook(gno=218, title=Title...218, content=Content...218, writer=user8)
테스트 조건 : title OR content 에 1이 포함된 내용 출력
import com.example.guestbook2022.entity.Guestbook;
import com.example.guestbook2022.entity.QGuestbook;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
\
@SpringBootTest
public class GuestbookRepositoryTests {
@Autowired
private GuestbookRepository guestbookRepository;
@Test // Querydsl 테스트 - 다중항목테스트(title 이나 content 에 1이 포함된 내용 출력)
public void testQueryy2() {
// 페이징 처리
Pageable pageable = PageRequest.of(0, 10, Sort.by("gno").descending());
QGuestbook qGuestbook = QGuestbook.guestbook;
BooleanBuilder booleanBuilder = new BooleanBuilder();
// title like %1% or content like %1% 쿼리를 코드로 구현
BooleanExpression expression = qGuestbook.title.contains("1");
BooleanExpression exAll = expression.or(qGuestbook.content.contains("1"));
// 만들어진 조건 결합
booleanBuilder.and(exAll);
booleanBuilder.and(qGuestbook.gno.gt(0L));
Page<Guestbook> result = guestbookRepository.findAll(booleanBuilder, pageable);
result.stream().forEach(guestbook -> {
System.out.println(guestbook);
});
}
}
Hibernate:
select
guestbook0_.gno as gno1_0_,
guestbook0_.moddate as moddate2_0_,
guestbook0_.regdate as regdate3_0_,
guestbook0_.content as content4_0_,
guestbook0_.title as title5_0_,
guestbook0_.writer as writer6_0_
from
guestbook guestbook0_
where
(
guestbook0_.title like ? escape '!'
or guestbook0_.content like ? escape '!'
)
and guestbook0_.gno>?
order by
guestbook0_.gno desc limit ?
Hibernate:
select
count(guestbook0_.gno) as col_0_0_
from
guestbook guestbook0_
where // or / and 키워드가 생성된 것을 확인 할 수 있다
(
guestbook0_.title like ? escape '!'
or guestbook0_.content like ? escape '!'
)
and guestbook0_.gno>?
Guestbook(gno=291, title=Title...291, content=Content...291, writer=user1)
Guestbook(gno=281, title=Title...281, content=Content...281, writer=user1)
Guestbook(gno=271, title=Title...271, content=Content...271, writer=user1)
Guestbook(gno=261, title=Title...261, content=Content...261, writer=user1)
Guestbook(gno=251, title=Title...251, content=Content...251, writer=user1)
Guestbook(gno=241, title=Title...241, content=Content...241, writer=user1)
Guestbook(gno=231, title=Title...231, content=Content...231, writer=user1)
Guestbook(gno=221, title=Title...221, content=Content...221, writer=user1)
Guestbook(gno=219, title=Title...219, content=Content...219, writer=user9)
Guestbook(gno=218, title=Title...218, content=Content...218, writer=user8)