LOCATION | Checklist visited(기능별) | |||
특이 작업 | checklist visited : 기능별 | 메인 | 딸려서 변경되는 부분 | |
ISC | 템플릿 검색 입력 폼 | ISC | ||
I가 어디로 흘러가는지 | I이 어떤 변수로 흘러가는지 | |||
I 후 작동 | I 후 작동 | |||
부가 기능 추가(CSS 등) | 위상 처리 : 특정 URL 입력시에만 발생 | |||
Controller에서 post가 아닌, hiddne form을 사용해서 get방식으로 요청한다
list 요청은, 검색어와 번호를 따로 사용하는게 아닌, 기존 paging을 중복으로 사용한다
자바스크립트를 사용한다
-
SQL Query문 해석
select
distinct q.id,
q.author_id,
q.content,
q.create_date,
q.modify_date,
q.subject
from question q
left outer join site_user u1 on q.author_id=u1.id
left outer join answer a on q.id=a.question_id
left outer join site_user u2 on a.author_id=u2.id
where
q.subject like '%스프링%'
or q.content like '%스프링%'
or u1.username like '%스프링%'
or a.content like '%스프링%'
or u2.username like '%스프링%'
SELECT는 최종 OUTPUT이다
from question q로 기준점을 잡는다
outer join은 합집합으로 추가 조건이다
where는 조건 INPUT이다
-
ISC
: template 에 입력 버튼
Input 시 작동하는 method
: QuestionService에서 Query문 return하는 specification 메서드 추가
specification 메서드에 딸려오는 것
: QuestionRepository에서 Specification과 Pageable 객체를 입력으로 Question 엔티티를 조회하는 findAll 메서드를 선언했다.
specification 메서드에 딸려오는 것
: QuestionService의 getList메서드에 kw인풋, spec 리턴 추가
specification 메서드를 활용하기 위해
: QuestionController의 list에 String kw > RequestParam 인자 추가
_
(... 생략 ...)
import com.mysite.sbb.answer.Answer;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;
(... 생략 ...)
public class QuestionService {
private final QuestionRepository questionRepository;
private Specification<Question> search(String kw) {
return new Specification<>() {
private static final long serialVersionUID = 1L;
@Override
public Predicate toPredicate(Root<Question> q, CriteriaQuery<?> query, CriteriaBuilder cb) {
query.distinct(true); // 중복을 제거
Join<Question, SiteUser> u1 = q.join("author", JoinType.LEFT);
Join<Question, Answer> a = q.join("answerList", JoinType.LEFT);
Join<Answer, SiteUser> u2 = a.join("author", JoinType.LEFT);
return cb.or(cb.like(q.get("subject"), "%" + kw + "%"), // 제목
cb.like(q.get("content"), "%" + kw + "%"), // 내용
cb.like(u1.get("username"), "%" + kw + "%"), // 질문 작성자
cb.like(a.get("content"), "%" + kw + "%"), // 답변 내용
cb.like(u2.get("username"), "%" + kw + "%")); // 답변 작성자
}
};
}
(... 생략 ...)
}
추가한 search 메서드는 kw를 입력으로 받아, Specification<> / QUERY의 join문과 where문을 생성하여 리턴한다
'포트폴리오 > SPRINGBOOT 옛날' 카테고리의 다른 글
32 : anchor (0) | 2022.10.26 |
---|---|
31 : 추천 (0) | 2022.10.26 |
30 : 수정일시 표시 (0) | 2022.10.25 |
29 : ANSWER DELETE (0) | 2022.10.25 |
28 : ANSWER UPDATE (0) | 2022.10.25 |