有没有办法在 Spring Data @Query 注释值中使用常量?

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

本文介绍了有没有办法在 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 不会将其识别为对 UserModel 类的使用.唯一的好处是您可以将值保存在一个地方,这在大多数情况下就足够了. 这已在 IntelliJ IDEA 2017.1.我不知道其他 IDE.

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. This has been resolved in IntelliJ IDEA 2017.1. I don't know about other IDEs.

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

本文标题为:有没有办法在 Spring Data @Query 注释值中使用常量?

基础教程推荐