如何在Spring Data Azure Cosmos DB中编写动态SQL [英] How to write dynamic sql in Spring Data Azure Cosmos DB
本文介绍了如何在Spring Data Azure Cosmos DB中编写动态SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我们计划使用Spring Data Azure Cosmos DB v3.我看到我们扩展了CosmosRepository接口并实现了仓库,并应用了@Query注释
We are planning to use Spring Data Azure Cosmos DB v3. I see that we extend CosmosRepository interface and implement our repo and apply @Query annotation
@Query(value = "select * from c where c.firstName = @firstName and c.lastName = @lastName")
List<User> getUsersByTitleAndValue(@Param("firstName") int firstName, @Param("lastName") String lastName);
但是我们需要动态生成@Query中使用的sql
But we have a need to generate the sql used in @Query dynamically
例如
String sql = "Select * from some table t"
if(someCondition){
sql.append(" where t.name="+ nameVar);
}
如何在Spring Data Azure Cosmos DB v3中实现这一目标?
How to achieve this in Spring Data Azure Cosmos DB v3 ?
推荐答案
您可以执行以下操作:-
you can do something like this:-
@Query(value = "select * from c where (@firstName=null or c.firstName=@firstName) and (lastName=null or c.lastName=@lastName)")
List<User> getUsersByFirstNameAndLastName(@Param("firstName") int firstName, @Param("lastName") String lastName);
这样,它将忽略 null 参数.
这篇关于如何在Spring Data Azure Cosmos DB中编写动态SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文