英文:
How to pass parameter to Couchbase query with @Query annotation?
问题
我试图将参数传递给自定义查询,但它不起作用。
我的类如下:
@Repository
public interface CarRepository extends CouchbaseRepository<CarDocument, String> {
@Query(
value = "#{#n1ql.selectEntity} #{#n1ql.bucket} where cars.`:#{#carId}` is not missing; ")
List<CarDocument> getCarDocumentsByCarId(@Param("carId") Integer carId);
}
返回的值为null。所以它不起作用。
我做错了什么?
英文:
I am trying to pass a parameter to custom query but it is not working.
My class is like this:
@Repository public interface CarRepository extends CouchbaseRepository<CarDocument, String> {
@Query(
value = "#{#n1ql.selectEntity} #{#n1ql.bucket} where cars.`:#{#carId}` is not missing; ")
List<CarDocument> getCarDocumentsByCarId(@Param("carId")Integer carId);
}
The returned value is null. So it is not working.
what am I doing wrong?
答案1
得分: 1
这是一个示例,演示了如何将参数传递给Couchbase查询:
@Repository
public interface UserRepository extends CouchbaseRepository<User, String> {
@Query("#{#n1ql.selectEntity} WHERE username = $1")
User findByUsername(String username);
}
User user = userRepository.findByUsername("johndoe");
占位符$1用于表示传递给方法的第一个参数。
当您调用findByUsername方法并将用户名作为参数传递时,它将替换在查询中占位符的位置。
英文:
Here's an example of how you can pass parameters to a Couchbase query:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
@Repository
public interface UserRepository extends CouchbaseRepository<User, String> {
@Query("#{#n1ql.selectEntity} WHERE username = $1")
User findByUsername(String username);
}
User user = userRepository.findByUsername("johndoe");
<!-- end snippet -->
The placeholder $1 is used to represent the first parameter passed to the method.
When you call the findByUsername method and pass a username as an argument, it will be substituted in the query at the placeholder's position.
答案2
得分: 0
使用SpEL表达式 #{[<n>]}
@Query("SELECT META(#{#n1ql.bucket}).id AS __id, META(#{#n1ql.bucket}).cas AS __cas, meta().id as id FROM #{#n1ql.bucket} WHERE #{#n1ql.filter} #{[1]}")
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
Flux<String> findIdByDynamicN1ql(String docType, String queryStatement);
List<String> all = reactiveAirportRepository.findIdByDynamicN1ql("ignored", "and 1=1").toStream().collect(Collectors.toList());
产生以下结果。注意它将 #{[1]}
替换为第二个参数。
2023-06-23 16:21:29,302 DEBUG core.ReactiveFindByQueryOperationSupport: 184 - findByQuery scope: null collection: null options: null statement: SELECT META(9d4e29b1-6d3c-4c07-872d-241d232a3a0a
).id AS __id, META(9d4e29b1-6d3c-4c07-872d-241d232a3a0a
).cas AS __cas, meta().id as id FROM 9d4e29b1-6d3c-4c07-872d-241d232a3a0a
WHERE t
= "airport" and 1=1
英文:
use the spel expression #{[<n>]}
@Query("SELECT META(#{#n1ql.bucket}).id AS __id, META(#{#n1ql.bucket}).cas AS __cas, meta().id as id FROM #{#n1ql.bucket} WHERE #{#n1ql.filter} #{[1]}")
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
Flux<String> findIdByDynamicN1ql(String docType, String queryStatement);
List<String> all = reactiveAirportRepository.findIdByDynamicN1ql("ignored", "and 1=1").toStream().collect(Collectors.toList());
Gives the following. Notice how it replaces the #{[1]}
with the second argument.
2023-06-23 16:21:29,302 DEBUG core.ReactiveFindByQueryOperationSupport: 184 - findByQuery scope: null collection: null options: null statement: SELECT META(9d4e29b1-6d3c-4c07-872d-241d232a3a0a
).id AS __id, META(9d4e29b1-6d3c-4c07-872d-241d232a3a0a
).cas AS __cas, meta().id as id FROM 9d4e29b1-6d3c-4c07-872d-241d232a3a0a
WHERE t
= "airport" and 1=1
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论