728x90
스프링부트 페이징 구현 수업 정리 2
이전내용 이어서 정리
JpaController.java
//Jpa/memberList
@RequestMapping(value="/jpa/memberList", method= RequestMethod.GET)
public String memberList(Pageable pageable,
@RequestParam(value="searchCate",required=false,defaultValue="")
String searchCate,
@RequestParam(value="searchKeyword",required=false,defaultValue="")
String searchKeyword, Model model) {
System.out.println(pageable);
System.out.println(searchCate);
System.out.println(searchKeyword);
Page<Member> members = null;
if(searchCate.equals("name")){
members = memberRepository.findByNameContaining(searchKeyword, pageable);
}else if(searchCate.equals("id")){
members = memberRepository.findByIdContains(searchKeyword, pageable);
}else if(searchCate.equals("phone")){
members = memberRepository.findByPhoneContaining(searchKeyword, pageable);
}else if(searchCate.equals("nameid")){
String[] result = searchKeyword.split(",");
members=memberRepository.findByNameAndId(result[0],result[1],pageable);
} else {
//검색내용없을때
members = memberRepository.findAll(pageable);
}
model.addAttribute("searchCate", searchCate);
model.addAttribute("members", members);
model.addAttribute("searchKeyword", searchKeyword);
return "jpa/memberList";
}
MemberRepository.java
package com.example.demo.repository;
import com.example.demo.entity.Member;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
public interface MemberRepository extends JpaRepository<Member, Integer> {
Page<Member> findByNameContaining(String searchKeyword, Pageable pageable);
// 회원 검색용 - name
//Page<Member> findByNameContaining(String name, Pageable pageable);
// 회원 검색용 - id
Page<Member> findById(String id, Pageable pageable);
// 회원 검색용 - phone
// Containing은 포함이므로 010- 만 검색해도 010 이 포함된 번호를 모두 검색.
Page<Member> findByPhoneContaining(String phone, Pageable pageable);
// 만약 010- 으로만 된 번호만 검색하고 싶다면???
Page<Member> findByPhone(String phone, Pageable pageable);
// LIKE - 특정 문자열이 포함되었는지? --> %111% 사용.
Page<Member> findByPhoneLike(String phone, Pageable pageable);
// 111 번호로 시작하는 번호를 찾으려면? --> 111% --> 쿼리조건 키워드조합 --> findByPhoneStartsWith
Page<Member> findByPhoneStartsWith(String phone, Pageable pageable);
// 111 번호로 끝나는 번호를 찾으려면? --> %111 --> 쿼리조건 키워드조합 --> findByPhoneEndsWith
Page<Member> findByPhoneEndsWith(String phone, Pageable pageable);
// 대소문자 구분하기
Page<Member> findByIdContains(String id, Pageable pageable);
// and 쿼리 조건 만들기
Page<Member> findByNameAndId(String name, String id, Pageable pageable);
// or 쿼리 조건 만들기
Page<Member> findByNameContainsOrIdContains(String name, String id, Pageable pageable);
// JPA 정렬 쿼리 메서드 만들기1 - 회원 등록순(num)으로 정렬 --> (1) 조건 (2) 정렬
Page<Member> findByNumGreaterThanEqualOrderByNameAsc(Integer num, Pageable pageable);
// JPA 정렬 쿼리 메서드 만들기2
Page<Member> findByAgeLessThanEqualOrderByNameAsc(int age, Pageable pageable);
// JPA 정렬 쿼리 메서드 만들기3
Page<Member> findByNameContains(String name, Pageable pageable);
}
쿼리메소드 Sample 및 JPQL snippet
memberList.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>memberList</title>
<style>
ul > li {
display: inline-block;
border: 0px solid;
padding: 10px;
}
ul {
margin-left: -50px;
}
</style>
</head>
<body>
<h2>Member List</h2>
<p><a href="/">Main</a> / 현재 <span th:text="${#lists.size(members)}" style="font-weight: bold">10</span>명의 회원정보가 등록되었습니다.</p>
<form
style="padding: 10px; border: 0px solid; width: 780px; display: flex; justify-content: flex-end"
th:action="@{/jpa/memberList}" method="GET">
<input type="hidden" name="page" value="0">
<input type="hidden" name="size" value="3">
원하는 검색 컬럼 선택
<input type="text" id="searchCate" name="searchCate" placeholder="검색어 입력" autocomplete="off" th:value="${searchCate}">
검색 내용
<input type="text" id="searchKeyword" name="searchKeyword" placeholder="검색어 입력" autocomplete="off" th:value="${searchKeyword}">
<button type="submit">Search</button>
</form>
<table cellpadding=10 border=1 width="800">
<thead>
<tr>
<th width="50">#</th>
<th>Name</th>
<th>ID</th>
<th>Phone</th>
<th>Age</th>
<th>Edit</th>
<th>Del</th>
<th>Del</th>
</tr>
</thead>
<tbody>
<tr th:each="member : ${members}">
<!--검색버튼-->
<p><a href="/" style="padding:10px border:0px"> </a> </p>
<th th:text="${member.num}">1</th>
<th><a href="#" th:href="@{/jpa/memberDetail( num=${member.num} )}" th:text="${member.name}">홍길동</a></th>
<th th:text="${member.id}">mr.hong</th>
<th th:text="${member.phone}">010-123-5678</th>
<th th:text="${member.age}">20</th>
<th><a href="#" th:href="@{/jpa/memberWrite( num=${member.num} )}">수정</a></th>
<th><a href="#" th:href="@{/jpa/memberDelete( num=${member.num} )}">삭제(바로 삭제)</a></th>
<th><a href="#none" th:onclick="btnDel( [[${member.num}]] );" style="color: #ff0000">삭제(확인 후 삭제)</a></th>
</tr>
</tbody>
</table>
<p>
총 레코드 갯수 <span style="color: red; font-weight: bold" th:text="${members.totalElements}">4</span>
현재 페이지 <span style="color: green; font-weight: bold" th:text="${members.pageable.pageNumber+1}">1</span> /
총 페이지 <span style="color: blue; font-weight: bold" th:text="${members.totalPages}">5</span>
</p>
<ul>
<li>
<th:block th:if="${ members.pageable.pageNumber+1 == 1 }">
<span style="color: #999">Previous</span>
</th:block>
<th:block th:unless="${ members.pageable.pageNumber+1 == 1 }">
<a href="#" th:href="@{/jpa/memberList( page=${members.pageable.pageNumber-1},size=3,searchKeyword=${searchKeyword},searchCate=${searchCate})}">Previous</a>
</th:block>
</li>
<li>/</li>
<li>
<th:block th:if="${ members.totalPages == members.number+1 }">
<span style="color: #999">Next</span>
</th:block>
<th:block th:unless="${ members.totalPages == members.number+1 }">
<a href="#" th:href="@{/jpa/memberList( page=${members.number}+1, size=3,searchKeyword=${searchKeyword},searchCate=${searchCate})}">Next</a>
</th:block>
</li>
</ul>
</body>
</html>
출력화면 ▼
마지막으로 페이지 정렬을 해보자
//Jpa/memberList
@RequestMapping(value="/jpa/memberList", method= RequestMethod.GET)
public String memberList(@PageableDefault (size=5, sort="name", direction = Sort.Direction.DESC)Pageable pageable,
@RequestParam(value="searchCate",required=false,defaultValue="")
String searchCate,
@RequestParam(value="searchKeyword",required=false,defaultValue="")
String searchKeyword, Model model) {
System.out.println(pageable);
System.out.println(searchCate);
System.out.println(searchKeyword);
Page<Member> members = null;
if(searchCate.equals("name")){
members = memberRepository.findByNameContaining(searchKeyword, pageable);
}else if(searchCate.equals("id")){
members = memberRepository.findByIdContains(searchKeyword, pageable);
}else if(searchCate.equals("phone")){
members = memberRepository.findByPhoneContaining(searchKeyword, pageable);
}else if(searchCate.equals("nameid")){
String[] result = searchKeyword.split(",");
members=memberRepository.findByNameAndId(result[0],result[1],pageable);
} else {
// 사용 예시:
// pageable - PageRequest.of(page, size);
// pageable - PageRequest.of(page, size, sort);
// PageRequest(1,3,Sort.by("name").ascending())
// pageable - PageRequest.of(page, size, sort, direction);
// PageRequest.of(pageable.getPageNumber(), 3, Sort.Direction.DESC, "name");
// pageable = PageRequest.of(pageable.getPageNumber(), 3,
// Sort.by("age").descending().and(Sort.by("name").ascending()));
//검색내용없을때
members = memberRepository.findAll(pageable);
}
model.addAttribute("searchCate", searchCate);
model.addAttribute("members", members);
model.addAttribute("searchKeyword", searchKeyword);
return "jpa/memberList";
}
728x90
'STUDY > SpringBoot' 카테고리의 다른 글
Spring Boot - kafka producer 예제 (0) | 2024.10.11 |
---|---|
[Springboot] 22-09-29 삭제·상세·수정 기능 수업 -3 (0) | 2022.09.29 |
[Springboot] 22-09-28 페이징 기능 수업 -1 (0) | 2022.09.28 |
[SpringBoot] 22-09-27 Thymeleaf 문법 총 정리 (0) | 2022.09.27 |
[SpringBoot] 22-09-26 Login CRUD 수업 (0) | 2022.09.26 |