我正在寻找一个使用Spring Data JPA动态构建查询的解决方案.我有一个GameController,它有一个RESTful服务端点/游戏,有4个可选参数:流派,平台,年份,标题.API可以不传递,全部4,以及它们之间的每个组合.如果未传递任何参数,则默认为null.我需要一个存储库中的方法来构建适当的查询,理想情况下仍然允许Spring Data JPA Paging,尽管我不确定这是否可行.
我找到了这篇文章,但除非我误解,否则这似乎不是我需要的.http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/
我知道JPA有一个Query Criteria API,但实际上并不知道如何实现它.
我意识到我可以为每个可能的场景创建一个方法,但这似乎是非常糟糕的做法和许多不必要的代码.
GameRepository:
package net.jkratz.igdb.repository;
import net.jkratz.igdb.model.Game;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface GameRepository extends JpaRepository<Game, Long> {
@Query("select g from Game g, GamePlatformMap gpm, Platform p where g = gpm.game and gpm.platform = p and p.id = :platform")
Page<Game> getGamesByPlatform(@Param("platform") Long platformId, Pageable pageable);
@Query("select g from Game g where g.title like :title")
Page<Game> getGamesByTitle(@Param("title") String title, Pageable pageable);
@Query("select g from Game …Run Code Online (Sandbox Code Playgroud)