gpt4 book ai didi

java - 我的浏览器中显示的可分页数据错误

转载 作者:行者123 更新时间:2023-11-29 18:01:27 25 4
gpt4 key购买 nike

我正在尝试使用 Spring Data 和 Pageable 类来显示 MySQL 数据库中的数据。

我是 Spring Data 的新手,不幸的是我在浏览器中显示了所有行,而不是一些行。

在下面找到我的代码

pom.xml

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>

ProductDaoImpl

package com.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.model.Product;

@Repository
public class ProductDaoImpl implements ProductDao {

@Autowired
private SessionFactory sessionFactory;

public SessionFactory getSessionFactory() {
return sessionFactory;
}

@SuppressWarnings("unchecked")
public Page<Product> getPageProduct(@Param("libelleProduct")String libelleProduct, Pageable pageable) {
List<Product> listProduct = sessionFactory.getCurrentSession().createQuery("From Product p where p.libelleProduct like :libelleProduct").setParameter("libelleProduct", libelleProduct).list();
Page<Product> pageProduct = new PageImpl<Product>(listProduct, pageable, listProduct.size());
return pageProduct;
}

}

产品服务

package com.service;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import com.dao.ProductDao;
import com.model.Product;

@Service
@Transactional
public class ProductServiceImpl implements ProductService {

@Autowired
private ProductDao productDao;

public ProductDao getProductDao() {
return productDao;
}

public Page<Product> getPageProduct(String libelleProduct, int page, int size) {
return productDao.getPageProduct(libelleProduct, new PageRequest(page, size));
}

}

产品 Controller

package com.controller;

import org.jboss.logging.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.model.Product;
import com.service.ProductService;

@RestController
public class ProductController {

public Logger logger = Logger.getLogger(ProductController.class);

public ProductController(){
System.out.println("ProductController()");
}

@Autowired
private ProductService service;

@RequestMapping(value="/listedesproduit", method=RequestMethod.GET, headers="Accept=application/json")
@ResponseBody
public Page<Product> getPageProduct(){
Page<Product> pageProduct = service.getPageProduct("%e%",0, 1);
pageProduct.forEach(p->System.out.println(p.getLibelleProduct()));
return pageProduct;
}

}

下面是我在浏览器中得到的结果,我看到显示四行,而不是每页显示一行。

{"content":[{"idProduct":2,"libelleProduct":"sprite","qteProduct":5},{"idProduct":3,"libelleProduct":"eku","qteProduct":5},{"idProduct":4,"libelleProduct":"33 export","qteProduct":5},{"idProduct":6,"libelleProduct":"guiness","qteProduct":5}],"size":1,"number":0,"numberOfElements":4,"sort":null,"totalPages":4,"totalElements":4,"firstPage":true,"lastPage":false}

你能帮我找出我做错了什么吗?

最佳答案

您在这里使用错误:

List<Product> listProduct = sessionFactory.getCurrentSession()
.createQuery("From Product p where p.libelleProduct like :libelleProduct")
.setParameter("libelleProduct", libelleProduct).list();
Page<Product> pageProduct = new PageImpl<Product>(listProduct, pageable, listProduct.size());

您基本上是加载所有查询结果,然后将其返回到 PageImpl 中,这只是数据的包装器。

您应该创建一个 PagingAndSortingRepository 并调用传递 Pageable 的查询方法,这是存储库要执行的页面请求。

如果这对您来说没有意义,请查找有关“spring 数据分页”的教程,例如 this one看看documentation .

关于java - 我的浏览器中显示的可分页数据错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48287063/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com