Spring Data JPA 中的动态查询 [英] Dynamic Queries in Spring Data JPA

查看:25
本文介绍了Spring Data JPA 中的动态查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找使用 Spring Data JPA 动态构建查询的解决方案.我有一个 GameController,它有一个 RESTful 服务端点/games,它有 4 个可选参数:流派、平台、年份、标题.API 可能不会传递任何一个,所有 4 个,以及它们之间的每个组合.如果没有传递任何参数,则默认为 null.我需要 Repository 中的一个方法来构建适当的查询,并且理想情况下还允许 Spring Data JPA Paging,尽管我不确定这是否可行.

I am looking for a solution to dynamically build queries using Spring Data JPA. I have a GameController which has a RESTful service endpoint /games which takes 4 optional parameters: genre, platform, year, title. The API may be passed none of those, all 4, and every combination in between. If any parameter is not passed it defaults to null. I need a method in the Repository that will build the appropriate query and ideally also still allow Spring Data JPA Paging, although I'm not sure if that is possible.

我找到了这篇文章,但这似乎不是我需要的,除非我误解了.http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/

I found this article but this doesn't seem to be what I need unless I am misunderstanding. http://spring.io/blog/2011/04/26/advanced-spring-data-jpa-specifications-and-querydsl/

我知道 JPA 有一个 Query Criteria API,但真的不知道如何实现它.

I know JPA has a Query Criteria API but really have no idea how to implement this.

我意识到我可以为每个可能的场景创建一个方法,但这似乎是非常糟糕的做法和很多不必要的代码.

I realize I could create a method for each possible scenario but that seems like really bad practice and a lot of unnecessary code.

游戏库:

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 g, GameGenreMap ggm, Genre ge where g = ggm.game and ggm.genre = ge and ge.id = :genreId")
    Page<Game> getGamesByGenre(@Param("genre") Long genreId, Pageable pageable);
}

推荐答案

我想说使用 QueryDSL 是做你想做的一种方式.

I would say that using QueryDSL is one way of doing what you want.

例如我有一个如下定义的存储库:

For example I have a repository defined as below:

public interface UserRepository extends PagingAndSortingRepository<User, Long>, QueryDslPredicateExecutor<User> {

    public Page<User> findAll(Predicate predicate, Pageable p);
}

我可以使用任意参数组合调用此方法,如下所示:

I can call this method with any combination of parameters, like below:

public class UserRepositoryTest{

    @Autowired
    private UserRepository userRepository;

    @Test
    public void testFindByGender() {
        List<User> users = userRepository.findAll(QUser.user.gender.eq(Gender.M));
        Assert.assertEquals(4, users.size());

        users = userRepository.findAll(QUser.user.gender.eq(Gender.F));
        Assert.assertEquals(2, users.size());
    }

    @Test
    public void testFindByCity() {

        List<User> users = userRepository.findAll(QUser.user.address.town.eq("Edinburgh"));
        Assert.assertEquals(2, users.size());

        users = userRepository.findAll(QUser.user.address.town.eq("Stirling"));
        Assert.assertEquals(1, users.size());
    }

    @Test
    public void testFindByGenderAndCity() {
        List<User> users = userRepository.findAll(QUser.user.address.town.eq("Glasgow").and(QUser.user.gender.eq(Gender.M)));
        Assert.assertEquals(2, users.size());

        users = userRepository.findAll(QUser.user.address.town.eq("Glasgow").and(QUser.user.gender.eq(Gender.F)));
        Assert.assertEquals(1, users.size());
    }
}

这篇关于Spring Data JPA 中的动态查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆