JPA 페이지네이션 기능 사용법

HUGO·2022년 10월 6일
0

JPA

목록 보기
2/6

resources/global.properties templates에 환경 설정

item.list.size=10
default.image=classpath:/static/image/Default.jpg

jpa20221004/Jpa20221004Application.java 서버 구동하는 파일에 환경 변수 파일 설정

package com.example.jpa_20221004;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

// 환경 변수 파일 사용 설정
@PropertySource("global.properties")


@ComponentScan(basePackages = {"com.example.controller", 
						"com.example.service"})
@EntityScan(basePackages = {"com.example.entity"})
@EnableJpaRepositories(basePackages = "com.example.repository")


@SpringBootApplication
public class Jpa20221004Application {

	public static void main(String[] args) {
		SpringApplication.run(Jpa20221004Application.class, args);
	}
}

controller/ItemController.java에 size 적용

@Value("${item.list.size}") int SIZE; // size =10

@Autowired ResourceLoader resourceLoader;
    @Autowired ItemRepository iRepository;

    // 아래의 주소 같이 기입 하지 않으면 400 오류 남
    // 127.0.0.1:8080/ROOT/item/selectlist.do?name=검색어&page=1
    @GetMapping(value = {"/selectlist.do"})
   .
   .
   .
   .

entity/ItemRepository.java에 저장

  package com.example.repository;

import java.util.List;

import org.springframework.data.domain.Pageable;    // pageble 은 domain으로
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.example.entity.Item;

@Repository
public interface ItemRepository 
    extends JpaRepository<Item, Long> {
    
    // SELECT i FROM Item i WHERE i.name LIKE '%' || ?1 || '%'
    List<Item> findByNameContaining(String name);
    
    // SELECT i FROM Item i WHERE i.name LIKE '%' || ?1 || '%' 
    // ORDER BY no DESC
    List<Item> findByNameContainingOrderByNoDesc(String name, Pageable pageable);

    // 검색어에 해당하는 전체 개수
    // SELECT COUNT(*) FROM Item i 
    long countByNameContaining(String name);
}

controller/ItemController.java에 페이지 코드 작성

// 아래의 주소 같이 기입 하지 않으면 400 오류 남
    // 127.0.0.1:8080/ROOT/item/selectlist.do?name=검색어&page=1
    @GetMapping(value = {"/selectlist.do"})
    
.
.
.
long count = iRepository.countByNameContaining(name);

        ModelAndView mav = new ModelAndView();
        mav.setViewName("item_selectlist");
        mav.addObject("list", list);
        mav.addObject("pages", (count-1) / SIZE + 1);

template/item_selectlist.html에서 페이지네이션 적용 확인하기

<!DOCTYPE html>
<html lang="ko" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>물품목록</title>
</head>

<body>
    <table border="1">
        <tr th:each="obj, idx : ${list}">
            <td th:text="${obj.no}"></td>
            <td th:text="${obj.name}"></td>
            <td th:text="${obj.content}"></td>
            <td th:text="${obj.price}"></td>
            <td th:text="${obj.quantity}"></td>
            <td th:text="${obj.regdate}"></td>
        </tr>
    </table>

    <th:block th:each="i : ${#numbers.sequence(1, pages)}">
        <a th:href="@{/item/selectlist.do(name= ''  , page=${i})}" th:text="${i}">
        </a>
    </th:block>
</body>
</html>
profile
갓 신생아 개발자 이야기

0개의 댓글