我想将名称传递给方法,并让它在查询中为我添加百分号。
如果查询为@Query("...file_name LIKE $1 ...")
,而我传入%dogNames%
,则它运行良好。但是,要传递dogNames
(无%
),查询必须是什么,它的工作原理完全一样?
我可以通过findByFileNameContains()
执行此操作。对我来说,findByFileNameContains()
的查询是自动生成的,并且可以使我传入“ dogNames.txt”之类的字符串,并且它将返回文件名称为“ /bin/dogNames.txt.9348393.tgz”和“ /bigdogNames.txt”。因此它为我写了这样的内容:@Query ("#{#n1ql.selectEntity} WHERE file_name LIKE %$1% ")
百分号包含在查询中,而我只传递了我想要包含文件名的字符串。
我如何编写自己执行相同查询的查询?我尝试显示findByFileNamePatternAndFileDirectory()
的内容,但出现此错误:
{"msg":"syntax error - at %!(NOVERB)","code":3000}
DAO类:
import com.mystuff.File;
import java.util.List;
import org.springframework.data.couchbase.core.query.Query;
import org.springframework.data.couchbase.repository.CouchbasePagingAndSortingRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface FileDao
extends CouchbasePagingAndSortingRepository<File, String> {
List<File> findByFileNameContains(String fileName);
@Query(
"#{#n1ql.selectEntity} WHERE #{#n1ql.filter} AND file_name LIKE %$1% AND file_dir = $2")
List<File> findByFileNamePatternAndFileDirectory(
String fileName, String fileDir);
}
[更多完整的错误日志:
{"msg":"syntax error - at %!(NOVERB)","code":3000}
at org.springframework.data.couchbase.core.CouchbaseTemplate.findByN1QL(CouchbaseTemplate.java:470)
at org.springframework.data.couchbase.repository.query.AbstractN1qlBasedQuery.executeCollection(AbstractN1qlBasedQuery.java:157)
at org.springframework.data.couchbase.repository.query.AbstractN1qlBasedQuery.executeDependingOnType(AbstractN1qlBasedQuery.java:132)
at org.springframework.data.couchbase.repository.query.AbstractN1qlBasedQuery.execute(AbstractN1qlBasedQuery.java:107)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:605)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.couchbase.repository.support.ViewPostProcessor$ViewInterceptor.invoke(ViewPostProcessor.java:87)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
像只能在字符串上完成。 LIKE的右侧必须用引号引起来。如果它是查询参数,则可能无法优化
使用类似这样的东西
file_name LIKE“ xyz%”
我无法找到将要在@Query("...")
中使用的查询,但是我确实使用Spring Data仓库基础结构中内置的查询生成器机制使其工作,并通过命名方法自动为我生成了查询以特定的方式:
List<File> findByFileNameContainsAndFileDirectory(
String fileName, String fileDir);
}
在这里,我找到了一些有关如何命名方法以使其执行所需查询的好信息:Spring-Data-Couchbase Docs
并且这里讨论了SpEL(Spring表达式语言,例如:#{#n1ql.filter}
),如果您好奇它是如何工作的:SpEL