2017-11-01 14 views
0

私はSpring Bootの初心者です。私はSpring MVCを使用してブログを作成していますが、メモリ内のデータベースに5つの最新の投稿を得る方法はわかりません(MySQL、MariaDBなどのデータベースは現在使用したくありません)。これまでに作成したクラスは次のとおりです。 PostServicesImpでは、findLatest5というメソッドを作成し、それをコントローラで使用しました。これは完全に機能します。今私はCrudRepositoryメソッドを使いたいです。おかげで私の記事を読むため ポストSpring BootでCrudRepositoryを使って5つの最新投稿を得る方法

@Entity 
public class Post { 
    @Id 
    @GeneratedValue(strategy=GenerationType.AUTO) 
    private Long id; 

    @Column(nullable=false, length=300) 
    private String title; 

    @Column(nullable=false) 
    private String body; 

    @ManyToOne(optional=false, fetch=FetchType.LAZY) 
    private Users author; 

    @Column(nullable=false) 
    private Date date= new Date(); 

    public Post(){ 

    } 

    public Post(Long id,String title, String body, Users author){ 
     this.id=id; 
     this.title=title; 
     this.body=body; 
     this.author=author; 
    } 

    public Long getId() { 
     return id; 
    } 

    public void setId(Long id) { 
     this.id = id; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getBody() { 
     return body; 
    } 

    public void setBody(String body) { 
     this.body = body; 
    } 

    public Users getAuthor() { 
     return author; 
    } 

    public void setAuthor(Users author) { 
     this.author = author; 
    } 

    public Date getDate() { 
     return date; 
    } 

    public void setDate(Date date) { 
     this.date = date; 
    } 

    @Override 
    public String toString() { 
     return "Post [id=" + id + ", title=" + title + ", body=" + body + ", author=" + author + ", date=" + date + "]"; 
    } 
} 

PostRepository

public interface PostRepository extends CrudRepository<Post, Long>{ 
    List<Post> findById(Long id); 
    List<Post> findByAuthor(String author); 
} 

PostServices:から

public interface PostServices { 
    List<Post> findLatest5(); 
} 

PostServicesImp

@Service 
public class PostServicesImp implements PostServices{ 

    @Autowired 
    private PostRepository pRepo; 

    @Override 
    public List<Post> findLatest5() { 
//  return pRepo.findLatest5Posts(new PageRequest(0, 5)); 
     return posts.stream().sorted((a,b)-> b.getDate().compareTo(a.getDate())) 
       .limit(5) 
       .collect(Collectors.toList()); 
    } 
} 

答えて

1

210:

制限クエリは、クエリメソッドの

結果は互換的に使用できるキーワード又はトップ を介して制限することができるもたらします。 オプションの数値は が返されるように最大結果サイズを指定するために先頭/先頭に を追加できます。数字を省略すると、結果のサイズは1とみなされます。

例:

User findTopByOrderByAgeDesc(); 
List<User> findTop10ByLastname(String lastname, Pageable pageable); 

だからあなたの場合には、あなたのPostRepositoryにこれを追加します。

List<Post> findTop5ByOrderByDateDesc(); 
+0

@H aVanをお読みください:[誰かが私の質問に答えるとどうすればいいですか?](https://stackoverflow.com/help/someone-answers) –

+0

ありがとうございました。申し訳ありませんが、ラップトップの前で時間を過ごした結果、間違ったボタンを押しました:D – Alice

+0

私は理解しませんでした。どちらのボタン? –

1

おそらくPagingAndSortingRepositoryの代わりCrudRepositoryを使用する必要があります。これにより、処理しているデータセットをより詳細に制御することができます。

PostRepository.java

public interface PostRepository extends PagingAndSortingRepository<Post, Long> { 
    List<Post> findById(Long id); 
    List<Post> findByAuthor(String author); 
} 

PostServicesImp

@Service 
public class PostServicesImp implements PostServices{ 

    @Autowired 
    private PostRepository pRepo; 

    @Override 
    public List<Post> findLatest5() { 
     return pRepo.findAll(new PageRequest(0, 5, new Sort(new Order(Direction.DESC, "whatever property you want to find the latest records based on")))); 
    } 
} 

参照:

https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html

How to query data via Spring data JPA by sort and pageable both out of box?

関連する問題