如何使用 @Query 注解将参数传递给 Couchbase 查询?

huangapple go评论60阅读模式
英文:

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&lt;CarDocument, String&gt; {
    @Query(
        value = &quot;#{#n1ql.selectEntity} #{#n1ql.bucket} where cars.`:#{#carId}` is not missing; &quot;)
    List&lt;CarDocument&gt; getCarDocumentsByCarId(@Param(&quot;carId&quot;)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&lt;User, String&gt; {

    @Query(&quot;#{#n1ql.selectEntity} WHERE username = $1&quot;)
    User findByUsername(String username);
}

User user = userRepository.findByUsername(&quot;johndoe&quot;);

<!-- 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 #{[&lt;n&gt;]}

@Query(&quot;SELECT META(#{#n1ql.bucket}).id AS __id, META(#{#n1ql.bucket}).cas AS __cas, meta().id as id FROM #{#n1ql.bucket} WHERE #{#n1ql.filter} #{[1]}&quot;)
@ScanConsistency(query = QueryScanConsistency.REQUEST_PLUS)
Flux&lt;String&gt; findIdByDynamicN1ql(String docType, String queryStatement);


List&lt;String&gt; all = reactiveAirportRepository.findIdByDynamicN1ql(&quot;ignored&quot;, &quot;and 1=1&quot;).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

huangapple
  • 本文由 发表于 2023年5月24日 18:37:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定