如何在存储库方法中将完整查询作为字符串作为参数传递并与@Query 注释一起使用? [英] How to pass complete query as String in repository method as argument and use with the @Query Annotation?
问题描述
我在 serviceImplementation 中准备了一个查询,之后我将此查询作为方法参数传递给presentDaySummarySmscWise(String finalQuery) 存储库方法.在 Repository Interface 中,我使用 @Query Annotaion 传递了这个查询,如代码所示.
I prepared a query in serviceImplementation ,After that I passed this query as method argument of presentDaySummarySmscWise(String finalQuery) Repository method. In Repository Interface ,I passed this query with @Query Annotaion like shown in code.
强文本//服务实现类
@服务
公共类 ErrorCodeServiceImpl 实现 ErrorCodeService {
public class ErrorCodeServiceImpl implements ErrorCodeService {
@Autowired
ErrorCodeRepository errorCodeRepo;
@Override
public List<Object[]> errorCodeDefaultSummary() {
String finalQuery="select smsc,vf_reason_code from reason_code_report where
log_date='2021-05-27'";
List<Object[]> result = errorCodeRepo.presentDaySummarySmscWise(finalQuery);
return result;
}强文本
//存储库
}
strong text
//Repository
@Repository
@Repository
公共接口 ErrorCodeRepository 扩展 JpaRepository
public interface ErrorCodeRepository extends JpaRepository<ErrorCodeReportEntity, ErrorCodeReportKeys>{
@Query(value=":query",nativeQuery=true)
@Query(value=":query",nativeQuery=true)
列表<对象[]>presentDaySummarySmscWise(字符串查询);
List<Object[]> presentDaySummarySmscWise(String query);
}
推荐答案
您不能使用查询参数替换查询的任意片段.只有查询中非常特定的部分才允许使用参数.
You cannot replace arbitrary fragments of the query using a query parameter. Only very specific sections of the query allow parameters.
如果您需要动态查询,规范或 QueryDSL 就是您想要的.
If you need dynamic queries, Specifications or QueryDSL is what you want.
这篇关于如何在存储库方法中将完整查询作为字符串作为参数传递并与@Query 注释一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!