Spring数据动态组成@query查询 [英] spring data compose @query query dynamically

查看:496
本文介绍了Spring数据动态组成@query查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,我必须以某种方法编写JPQL查询,然后将此查询传递给spring数据查询方法以用作@Query批注中的查询

I have situation in which I have to compose a JPQL query in some method, then pass this query to spring data query method to be used as the query in @Query annotation

@Query(value = ":DyanamicQuery")
List<PrizeInsuranceConfiguration> filterConfigurPrizeInsurance(String DyanamicQuery);

或至少是条件部分

@Query(value = "SELECT c FROM PrizeInsuranceConfiguration c WHERE  :DyanamicConditions")
List<PrizeInsuranceConfiguration> filterConfigurPrizeInsurance(String DyanamicConditions);

推荐答案

可以,您可以这样做.为什么不这样做有两个原因:

Do, you can do that. There are two reasons why not:

  1. sql注入(spring数据可以使用预准备语句);
  2. (第一个原因的结果)spring数据创建查询树并绑定所有参数

但是,如果您需要动态查询,则可以使用规范

But if you need dynamic query you can use Specifications, Query by Example or Querydsl.

这篇关于Spring数据动态组成@query查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆