gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:18:58 26 4
gpt4 key购买 nike

我是 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/

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