@Query MongoDb Spring存储库的命名参数

@Query MongoDb Spring存储库的命名参数

问题描述:

是否可以在mongodb存储库中为@Query方法使用命名参数,就像我们可以使用jpa存储库(

Is it possible to use named parameters for a @Query method in a mongodb repository, just like we can do with a jpa repository (http://docs.spring.io/spring-data/jpa/docs/1.4.3.RELEASE/reference/html/jpa.repositories.html section 2.3.5)?

作为示例,我想使用以下代码:

As an example, I would like to use the following code:

@Query("{'store' : :store, 'app' : :app }")
List<T> findByStoreAndApp(@Param("store") String store, @Param("app") String app);

代替:

@Query("{'store' : ?0, 'app' : ?1 }")
List<T> findByStoreAndApp(String store, String app);

恐怕spring boot确实在其源代码中提供了此功能.

I'm afraid that the spring boot does provide this function in its source code.

您可以创建一个界面并扩展 MongoRepository 它提供了一些简单的方法,您不需要实现它. 就像使用JpaRepository一样.

You can create an interface and extends MongoRepository It provides some simple method, and you don't need the implement it. Just like use the JpaRepository.

@NoRepositoryBean
public interface MongoRepository<T, ID extends Serializable> extends PagingAndSortingRepository<T, ID> {
    <S extends T> List<S> save(Iterable<S> var1);

    List<T> findAll();

    List<T> findAll(Sort var1);

    <S extends T> S insert(S var1);

    <S extends T> List<S> insert(Iterable<S> var1);
}

但是,如果您需要执行一些特殊的查询.您可以@Autowired MongoTemplate 并使用其自己的方法.对于redis,springboot甚至不提供MongoRepository这样的Repository.您只能将StringRedisTemplate之类的模板用于查询操作. 可能是后者,spring boot将为NoSql添加与JpaRepository相同的功能.但是现在,它不提供此功能.

However, if you need to do some special query. You could just @Autowired the MongoTemplate and use its own method. And for redis, springboot even doesn't provide the Repository like MongoRepository. You could just only use the template like StringRedisTemplate for query operation. May be latter, spring boot would add the same function like JpaRepository for NoSql. But now, it doesn't provide this function.