有没有办法在Spring Data @Query注释值中使用常量? [英] Is there a way to use constants inside Spring Data @Query annotation value?

查看:78
本文介绍了有没有办法在Spring Data @Query注释值中使用常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想硬编码常量值,我宁愿通过引用变量指定它们。

I don't want to hardcode constant values, I would rather specify them through a reference variable.

例如,而不是写下一个查询:

For example, rather then writing the next query:

@Query(value = "SELECT u FROM UserModel u WHERE u.status = 1")

..我想提取硬编码值'1'并写下如下内容:

..I would like to extract the hardcoded value '1' and write something like:

@Query(value = "SELECT u FROM UserModel u WHERE u.status = UserModel.STATUS_ACTIVE")  //doesn't compile

有没有办法在spring-data查询中指定第二个例子中的常量?

Is there a way to specify constants like in the second example inside spring-data queries?

推荐答案

您必须使用如下所示的完全限定类名:

You have to use fully qualified class name like this:

@Query("SELECT u FROM UserModel u WHERE u.status = com.example.package.UserModel.STATUS_ACTIVE")

但它的坏处是IDE不会将其识别为UserMod类的用法埃尔。唯一的好处是你可以将价值保持在一个地方,这在大多数情况下是足够的。

The bad thing about it though is that an IDE would not recognise this as an usage of the class UserModel. The only advantage is that you can keep the value in one place, which is sufficient most of the time.

这篇关于有没有办法在Spring Data @Query注释值中使用常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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