drop table answer
drop table question ์ผ๋ก
ํ ์ด๋ธ ์ญ์ ํจ
@Autowired
private QuestionService qService;
for(int i =1; i<=300; i++) {
String subject = String.format("ํ
์คํธ ๋ฐ์ดํฐ์
๋๋ค:[%03d]", i);
String content = "๋ด์ฉ๋ฌด";
qService.create(subject, content);

๋ฐ์ดํฐ 300๊ฐ ์ ๋ ฅ ํ์ธ



์คํฌ๋กค์ด ๋๋ฌด ๊ธธ์ด์ง๊ธฐ ๋๋ฌธ์ ํ์ด์ง์ ํ๋ คํจ
QuestionRepository
package com.mysite.sbb.question;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
public interface QuestionRepository extends JpaRepository<Question, Integer>{
Question findBySubject(String subject);
Question findBySubjectAndContent(String subject,String content);//์ ๋ชฉ๊ณผ ๋ด์ฉ์ด ๊ฐ์ ๋
//ํ์ด์ง-๋ชจ๋ ์ง๋ฌธ์ ํ์ด์ง ๋ณ๋ก ๊ฐ์ ธ์จ๋ค.
Page<Question> findAll(Pageable pageable);
}

QuestionService
public Page<Question> getList(int page){
Pageable pageable = PageRequest.of(page, 10); //ํ ํ์ด์ง์ 10๊ฐ
return this.qRepo.findAll(pageable); //๋ชจ๋ ์ง๋ฌธ ๋ฆฌ์คํธ
}
QuestionController
//ํ์ด์ง ์ถ๊ฐ
@GetMapping("/list")
public String list(Model model,@RequestParam(value = "page",defaultValue = "0") int page) {
Page<Question> paging = this.qService.getList(page);
model.addAttribute("paging", paging);
return "question_list";
}


question_list.html ์ ํ์ด์ง๋ค์ด์ ์ถ๊ฐ
</table>
<!-- ํ์ด์ง์ฒ๋ฆฌ ์์ -->
<div th:if="${!paging.isEmpty()}">
<ul class="pagination justify-content-center">
<li class="page-item" th:classappend="${!paging.hasPrevious} ? 'disabled'">
<a class="page-link"
th:href="@{|?page=${paging.number-1}|}">
<span>์ด์ </span>
</a>
</li>
<li th:each="page: ${#numbers.sequence(0, paging.totalPages-1)}"
th:classappend="${page == paging.number} ? 'active'"
class="page-item">
<a th:text="${page}" class="page-link" th:href="@{|?page=${page}|}"></a>
</li>
<li class="page-item" th:classappend="${!paging.hasNext} ? 'disabled'">
<a class="page-link" th:href="@{|?page=${paging.number+1}|}">
<span>๋ค์</span>
</a>
</li>
</ul>
</div>
<!-- ํ์ด์ง์ฒ๋ฆฌ ๋ -->
<a th:href="@{/question/create}" class="btn btn-primary">์ง๋ฌธ ๋ฑ๋กํ๊ธฐ</a>

์ด์ ํ์ด์ง๊ฐ ์์ ๊ฒฝ์ฐ์๋ ๋นํ์ฑํ

0๋ถํฐ ๋ชจ๋ ํ์ด์ง-1๊น์ง / 1๋ถํฐ ์์ํ๋ฉด totalPages๋ง ํด๋๋จ
ํ์ด์ง๊ฐ ํ์ฌ ํ์ด์ง์ ๋์ผํ๋ฉด active

๋ค์ ํ์ด์ง๊ฐ ์์ ๊ฒฝ์ฐ์๋ ๋นํ์ฑํ

ํ ์คํธ ํด๋ณด๋ฉด ์ถ๋ ฅ์ ๋๋๋ฐ,
์ ์ฒด ํ์ด์ง๊ฐ ๋์ด
๊ทธ๋์ 10๋ฒ๊น์ง๋ง ๋์ค๋๋ก ์์ ํ ๊ฑฐ์

<li th:each="page: ${#numbers.sequence(0, paging.totalPages-1)}"
th:if="${page >= paging.number-5 and page <= paging.number+5}"
th:classappend="${page == paging.number} ? 'active'" class="page-item">




๋ง์ง๋ง์ผ๋ก ์์ฑ์ผ์๋ฅผ ์ญ์์ผ๋ก(desc)๋ก ์กฐํํ๊ฒ ์์ ํ ๊ฑฐ์
QuestionService- getlist
public Page<Question> getList(int page){
Pageable pageable = PageRequest.of(page, 10, Sort.by("createDate").descending()); //ํ ํ์ด์ง์ 10๊ฐ
return this.qRepo.findAll(pageable); //๋ชจ๋ ์ง๋ฌธ ๋ฆฌ์คํธ
}
deata.domain Sort

์์ฑ์ผ์๋ก ๋ด๋ฆผ์ฐจ์
์ฒซํ์ด์ง๋ 300๋ถํฐ ๋์ด

'BACKEND > SpringBoot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| ๋ต๋ณ ๊ฐ์ ํ์ (1) | 2023.11.09 |
|---|---|
| ๊ฒ์๋ฌผ์ ์ผ๋ จ๋ฒํธ ์ถ๊ฐํ๊ธฐ (1) | 2023.11.09 |
| ์๋ฌ ์ฐฝ , ๋ค๋ธ๋ฐ ๋ชจ๋ํ (2) | 2023.11.09 |
| ๋ต๋ณ(Answer) ์ ํจ์ฑ ๊ฒ์ฌ ์ถ๊ฐ (0) | 2023.11.09 |
| ์ง๋ฌธ ๋ฑ๋ก ์ฒ๋ฆฌ ๋ฐ ์ ํจ์ฑ๊ฒ์ฌ (5) | 2023.11.09 |