List<Board> findByHitLessThan(long hit);
// 커스텀 메소드 => entity를 참고해서 조합을 해야함
// https://docs.spring.io/spring-data/mongodb/docs/3.0.9.RELEASE/reference/html/#mongodb.repositories.queries
// 정확히 일치
// 작성자가 'a'인 사람의 게시물 목록 조회
List<Board> findByWriter(String writer);
// 제목이 'a'인 게시물 목록 조회
List<Board> findByTitle(String title);
// 조회수가 100인것 목록으로 조회
List<Board> findByHit(long hit);
///////////////////////
// 크다 작다
// 조회수가 200미만 인것만 목록으로 조회
List<Board> findByHitLessThan(long hit);
// 조회수가 300이상 인것만 목록으로 조회
List<Board> findByHitGreaterThanEqual(long hit);
///////////////////////
// 여러개 포함
// 글번호가 컬렉션에 담긴 1, 5, 7만 조회
List<Board> findByNoIn(Collection<Long> nos);
// 글번호가 컬렉션에 담긴 1, 5, 7 아닌것만 조회
List<Board> findByNoNotIn(Collection<Long> nos);
List<Board>
타입으로 받을것이기 때문에 빈 List를 만들어서 조건에 맞는 결과를 Model을 통해서 jsp에 던진다@RequestParam(name = "text", defaultValue = "", required = false) String text
@RequestParam(name = "text", defaultValue = "", required = false) long text
@RequestParam(name = "text", defaultValue = "", required = false) String text
required = false
==> 주소에 의무적으로 넣지 않아도 된다jsp에서 가져온 데이터
: @RequestParam(name = "type", defaultValue = "", required = false) String type
: @RequestParam(name = "text", defaultValue = "", required = false) String text
jsp에서 받아오는 text의 타입이 String이기 때문에 Long.parseLong(text)
를 이용해서 string을 long 타입으로 바꿔준다.
: 해당 명령어만을 사용할 경우, if문으로 'text값이 없을때' 라는 조건을 충족시키기 어렵다.
: long은 null이나 "" 빈칸으로 리턴값을 만들수 없고 0L로만 만들수 있기 때문이다.
: 때문에 try_catch를 이용해서 오류를 처리한다.
2-1. 리턴값이 0L인 hit 변수를 long 타입으로 만든다.
2-2. try 할 부분은 hit에 text(검색어)를 long 타입으로 바꾸어 넣는것
2-3. catch해야 할 부분은 text(검색어)가 없거나 할 경우 hit을 0L로 리턴 시키는 것이다.
long hit = 0L;
try {
hit = Long.parseLong(text);
} catch (Exception e) {
hit = 0L;
}
list = boardRepository.findByHit(hit);
// Query Method 연습용 List
// 일치항목
@GetMapping(value = "/selectfind")
public String selectfindGET(
Model model,
// required = false ==> 주소에 의무적으로 넣지 않아도 된다
@RequestParam(name = "type", defaultValue = "", required = false) String type,
@RequestParam(name = "text", defaultValue = "", required = false) String text) {
List<Board> list = null;
if (type.equals("title")) {
list = boardRepository.findByTitle(text);
} else if (type.equals("writer")) {
list = boardRepository.findByWriter(text);
} else if (type.equals("hit")) {
long hit = 0L;
try {
hit = Long.parseLong(text);
} catch (Exception e) {
hit = 0L;
}
list = boardRepository.findByHit(hit);
}
if (text.length() == 0) {
// type 선택하지 않았을때는 전체조회
list = boardRepository.findAll();
}
model.addAttribute("list", list);
return "board/selectfind";
}
// Query Method 연습용 List
// 이상 미만
@GetMapping(value = "/selectfind1")
public String selectfind1GET(
Model model,
// required = false ==> 주소에 의무적으로 넣지 않아도 된다
@RequestParam(name = "type1", defaultValue = "", required = false) String type1,
@RequestParam(name = "text", defaultValue = "", required = false) String text) {
List<Board> list = null;
if (type1.equals("1")) {
long hit = 0L;
try {
hit = Long.parseLong(text);
} catch (Exception e) {
hit = 0L;
}
list = boardRepository.findByHitGreaterThanEqual(hit);
} else if (type1.equals("2")) {
long hit = 0L;
try {
hit = Long.parseLong(text);
} catch (Exception e) {
hit = 0L;
}
list = boardRepository.findByHitLessThan(hit);
}
if (text.length() == 0) {
// type 선택하지 않았을때는 전체조회
list = boardRepository.findAll();
}
model.addAttribute("list", list);
return "board/selectfind1";
}
jsp에서 세개의 no를 전달받음
:http://127.0.0.1:8080/board/selectfind2?type2=&no=&no=&no=
세 no들을 Collection<Long>
타입으로 받고 변수명은 nos로 정한다.
: @RequestParam(name = "no", defaultValue = "", required = false) Collection<Long> nos
if문 조건중에 nos가 비었을때의 처리는 .contains(null)
을 이용한다.
: if (nos.contains(null)) { }
// Query Method 연습용 List
// 포함 제외
@GetMapping(value = "/selectfind2")
public String selectfind2GET(
Model model,
// required = false ==> 주소에 의무적으로 넣지 않아도 된다
@RequestParam(name = "type2", defaultValue = "", required = false) String type2,
@RequestParam(name = "no", defaultValue = "", required = false) Collection<Long> nos) {
List<Board> list = null;
// System.out.println("nos ===> " + nos);
if (type2.equals("1")) {
list = boardRepository.findByNoIn(nos);
} else if (type2.equals("2")) {
list = boardRepository.findByNoNotIn(nos);
}
if (nos.contains(null)) {
list = boardRepository.findAll();
}
model.addAttribute("list", list);
// System.out.println("model ===> " + model);
return "board/selectfind2";
}