我正在尝试使用带有pageable类的spring数据显示mysql数据库中的数据。
我是spring数据的新手,不幸的是我在浏览器中显示了所有的行,而不是一些行。
在我的代码下查找
pom.xml文件
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.5.0.RELEASE</version>
</dependency>
产品导入
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));
}
}
产品控制器
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}
你能帮我找出我做错了什么吗?
1条答案
按热度按时间icomxhvb1#
你在这里用错了:
您基本上是加载所有查询结果,然后在
PageImpl
,这只是数据的 Package 。你应该创建一个
PagingAndSortingRepository
并调用传递Pageable
,这是存储库要执行的页面请求。如果这对您来说没有意义,请查找类似这样的关于“spring数据分页”的教程,并查看文档。