java - pageRequest 无法通过 root 转换为可分页

标签 java spring-mvc spring-data-jpa

我是 Java Spring 初学者。我正在创建一个博客项目,需要加载 5 篇最新帖子。我遇到了这个问题,但我不知道如何解决 //忽略我//忽略我//忽略我//忽略我//忽略我//忽略我//忽略我 帖子存储库:

import java.awt.print.Pageable;
import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

public interface PostRepository extends CrudRepository<Post, Long>{
    @Query("SELECT p FROM Post p LEFT JOIN FETCH p.author ORDER BY p.date DESC")
    List<Post> findLatest5Posts(Pageable pageable);
}

PostServicesImp:

import java.awt.print.Pageable;
import java.util.List;

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

import havan.blog.demo.models.Post;
import havan.blog.demo.models.PostRepository;

@Service
@Primary
public class PostServicesImpl implements PostServices{
    @Autowired
    private PostRepository postRepo;

    @Override
    public List<Post> findAll() {
        // TODO Auto-generated method stub
        return (List<Post>) this.postRepo.findAll();
    }

    @Override
    public List<Post> findLatest5() {
        // TODO Auto-generated method stub
        return (List<Post>) this.postRepo.findLatest5Posts((Pageable)new PageRequest(0, 5));
    }

家庭 Controller :

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import havan.blog.demo.models.LoginForm;
import havan.blog.demo.models.Post;
import havan.blog.demo.services.NotiService;
import havan.blog.demo.services.PostServices;
import havan.blog.demo.services.UserService;

@Controller
public class HomeController {

    @Autowired
    private PostServices pServices;

    @RequestMapping(path="/*")
    public String index(Model model){
        List<Post> latest5Posts=pServices.findLatest5();
        model.addAttribute("latest5posts",latest5Posts);

        List<Post> allPosts= pServices.findAll();
        model.addAttribute("allPosts",allPosts);

        return "index";
    }
}

//忽略我//忽略我//忽略我//忽略我//忽略我//忽略我//忽略我

最佳答案

已经晚了,但也许对其他人有帮助。

您已经从 java.awt.print.Pageable 导入了类 Pageable,但您需要来自 Spring 包的 Pageable:org.springframework.data.domain.Pageable

关于java - pageRequest 无法通过 root 转换为可分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46985333/

相关文章:

java - 根据设备时区显示日期

java - 膨胀布局 NullPointerException

spring-mvc - 在 Spring MVC 3 中处理后重定向时, session 属性是否显示在 URL 中?

java - Spring Data Jpa查询结果集到对象创建非常慢

java - 在 JpaRepository 中使用外键

java - 已签名的 java 应用程序 (.jar) 在 Firefox 中被视为不受信任

java - 使用 Glide 修复图像内存泄漏

java - Spring Boot 应用程序在尝试运行时抛出启动失败错误

tomcat - 将 Spring MVC 3 应用程序部署到 tomcat

java - Spring 利用 Mongo 通过 JPA 实现