spring-data-jpa pageRequest不能强制转换为具有根的可分页

vsaztqbk  于 2022-11-10  发布在  Spring
关注(0)|答案(2)|浏览(201)

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

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

邮政服务导入:

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

家庭控制器:

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

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

gab6jxml

gab6jxml1#

现在很晚了,但也许它可以帮助别人。
您已经从java.awt.print.Pageable导入了类Pageable,但是您需要Spring包中的Pageableorg.springframework.data.domain.Pageable

kiz8lqtg

kiz8lqtg2#

org.springframework.data.domain. pagable导入*可分页*类,而不是java.awt.print. pagable

相关问题