我的分页有问题,我有 9 个项目,所以会有两页,因为每页上的分页是 5,但是,当表格为空时,我意识到有不止一页,因为应该只有1 页,因为没有记录。该代码有效,但它给了我这个错误
ProductDAO.java
package com.springboot3.sb3hxh.DAO;
import com.springboot3.sb3hxh.Entity.*;
import java.util.*;
public interface ProductDAO {
List<ProductEntity> indexPagination(int page, int size);
int totalProducts();
}
ProductServe.java
package com.springboot3.sb3hxh.Service;
import com.springboot3.sb3hxh.DAO.*;
import com.springboot3.sb3hxh.Entity.*;
import jakarta.persistence.*;
import jakarta.transaction.*;
import org.springframework.stereotype.*;
import java.time.*;
import java.util.*;
@Service
public class ProductService implements ProductDAO {
@PersistenceContext
private EntityManager entityManager;
public ProductService(EntityManager theEntityManager) {
this.entityManager = theEntityManager;
}
@Override
public List<ProductEntity> indexPagination(int page, int size) {
TypedQuery<ProductEntity> query = entityManager.createQuery("SELECT p FROM ProductEntity p " +
"WHERE p.deleted_at IS NULL " +
"ORDER BY p.id ASC", ProductEntity.class);
query.setFirstResult(page * size);
query.setMaxResults(size);
return query.getResultList();
}
@Override
public int totalProducts() {
TypedQuery<Long> query = entityManager.createQuery("SELECT COUNT(p) FROM ProductEntity p", Long.class);
return query.getSingleResult().intValue();
}
}
ProductController.php
package com.springboot3.sb3hxh.Controller;
import com.springboot3.sb3hxh.Entity.*;
import com.springboot3.sb3hxh.Service.*;
import jakarta.validation.*;
import org.springframework.stereotype.*;
import org.springframework.ui.*;
import org.springframework.validation.*;
import org.springframework.web.bind.annotation.*;
import java.util.*;
@Controller
@RequestMapping("/products")
public class ProductController {
private ProductService productService;
public ProductController(ProductService theProductService){
productService = theProductService;
}
@GetMapping("/list")
public String listProducts(Model model, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5") int size){
List<ProductEntity> ProductEntity = productService.indexPagination(page -1, size);
int totalItems = productService.totalProducts();
int totalPages = (int) Math.ceil((double) totalItems / size);
model.addAttribute("currentPage", page);
model.addAttribute("totalPages", totalPages);
model.addAttribute("products", productEntity);
return "/product/list-products";
}
}
list-hunters.html
<div class="pagination justify-content-center">
<ul class="pagination">
<li class="page-item" th:each="i: ${#numbers.sequence(1, totalPages)}" th:class="${currentPage == i} ? 'active' : ''">
<a class="page-link" th:href="@{'/products/list?page=' + ${i}}">[[${i}]]</a>
</li>
</ul>
</div>
我不知道你的代码中错误的确切原因,但是你有什么理由不使用Page,Pageable与JPA吗? 使用它们,您可以在 JPA 级别轻松获取页面,而无需查询或页面索引。 我制作了一个带有分页功能的 Spring 服务器,只需引入一些 Class 或 Interace 实现,这并不难。 (如果你工作的公司开发环境是不允许使用我推荐的,那很抱歉)
页面(https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/Page.html),可分页(https://docs.spring .io/spring-data/commons/docs/current/api/org/springframework/data/domain/Pageable.html)