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

时间:2018-01-16 17:46:56

标签: java mysql spring hibernate spring-data-jpa

我正在尝试使用具有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;
    }

}

ProductService

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));
    }

}

ProductController的

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 个答案:

答案 0 :(得分:0)

你在这里错了:

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的查询方法,这是要由存储库执行的页面请求。

如果这对您没有意义,请查找关于&#34; spring data paging&#34;的教程。与this one类似,请查看documentation