IntelliJ IDEA - Java 代码中 SQL 的语法高亮显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30753512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 17:36:18  来源:igfitidea点击:

IntelliJ IDEA - Syntax Highlighting of SQL Inside Java Code

javasqlintellij-ideasyntax-highlighting

提问by Vasilen Donchev

I am using IntelliJ 13 as an IDE for a project where communication with DB is done via Spring's JDBC Template. When I have code fragments in Java like the following one:

我使用 IntelliJ 13 作为一个项目的 IDE,在该项目中与 DB 的通信是通过 Spring 的 JDBC 模板完成的。当我在 Java 中有如下代码片段时:

getJdbcTemplate().queryForObject("SELECT CONVERT(VARCHAR(255), NEWID())", String.class);

where getJdbcTemplate() returns initialized JdbcTemplate object, the IDE has proper syntax highlighting for the SQL statement (you can see it on the snippet bellow):

其中 getJdbcTemplate() 返回初始化的 JdbcTemplate 对象,IDE 对 SQL 语句具有正确的语法突出显示(您可以在下面的代码段中看到它):



.code {font-family: Monospace}
.db-stmt {background: #EDFCED}
.db-keyword {color: #000080; font-weight: bold;}
.db-column {color: #7D0C8D; font-weight: bold;}
.db-number {color: #0000FF;}
.java-class {color: #39008E; font-weight: bold;}
<span class="code">getJdbcTemplate().queryForObject("<span class="db-stmt"><span class="db-keyword">SELECT CONVERT</span>(<span class="db-keyword">VARCHAR</span>(<span class="db-number">255</span>), NEWID())</span>", String.<span class="java-class">class</span>);</span>



However, when I use another method, created by me:

但是,当我使用我创建的另一种方法时:

protected String queryForString(String sql, Object... args) {
    try {
        return getJdbcTemplate().queryForObject(sql, String.class, args);
    }
    catch (RuntimeException e) {
        return null;
    }
}

for the same query:

对于相同的查询:

queryForString("SELECT CONVERT(VARCHAR(255), NEWID())");

there is no syntax highlighting of the SQL statement. Do you know how to enable syntax highlighting of SQL staments for Java stings that are not arguments of methonds of JdbcTemplate or java.sql interfaces?

SQL 语句没有语法高亮显示。您知道如何为不是 JdbcTemplate 或 java.sql 接口的方法参数的 Java 字符串启用 SQL 语句的语法突出显示吗?

回答by Javaru

You can do this via 1) a setting 2) an annotation, and/or 3) a comment. For any of these, you must have the (bundled) IntelliLangPlugin enabled.

您可以通过 1) 设置 2) 注释和/或 3) 注释来执行此操作。对于其中任何一个,您必须IntelliLang启用(捆绑)插件。

Setting

环境

You can add a setting to say that a particular method's parameter is of a certain 'language' (SQL for example.)

您可以添加一个设置来说明特定方法的参数是某种“语言”(例如 SQL)。

  1. Open Setting (Ctrl+Alt+S/ ?,) > Editor > Language Injections
  2. On the right, click the add button and select Java Parameter
  3. In the Language Injection Settingsdialog, select SQL(or the desired language) in the ID field
    • You can also specify a specific SQL dialect such as MySQL, Oracle, etc.
  4. In the "Class Methods" field, enter the method you want to identify as taking an SQLparameter. You can open a class browser/search window via the ellipsis button enter image description here
    • You can select classes from your code, or from libraries
  5. If your method takes multiple parameters, you can select the parameter in the dialog: enter image description here
  1. 打开设置 ( Ctrl+ Alt+ S/ ?,) > 编辑器 > 语言注入
  2. 在右侧,单击添加按钮并选择 Java Parameter
  3. Language Injection Settings对话框中,SQL在 ID 字段中选择(或所需的语言)
    • 您还可以指定一个特定的SQL方言,如MySQLOracle等。
  4. 在“类方法”字段中,输入要标识为带SQL参数的方法。您可以通过省略号按钮打开类浏览器/搜索窗口在此处输入图片说明
    • 您可以从代码或库中选择类
  5. 如果您的方法采用多个参数,您可以在对话框中选择参数: 在此处输入图片说明

Now IntelliJ IDEA will automatically apply language injection when you use that method: enter image description here

现在,当您使用该方法时,IntelliJ IDEA 将自动应用语言注入: 在此处输入图片说明

Annotations

注释

You can annotate a method parameter (or variable or field) as being of a particular language type. By default, IntelliJ IDEA will use the org.intellij.lang.annotations.Languageannotation that is in the annotations.jarincluded with IntelliJ IDEA. It is located at <installDirectory>/redist/annotations.jaror in maven central: g:"com.intellij" AND a:"annotations"(Note: The latest jar in maven central is from version 12. It has everything you need. A non related annotation was added in v14. I'll upload the jars from v13 and v14 to maven central today (as I've been the one to do such in the past and have been meaning to update them). But it takes a few days to process through the system.)

您可以将方法参数(或变量或字段)注释为特定语言类型。默认情况下,IntelliJ IDEA 将使用 IntelliJ IDEAorg.intellij.lang.annotations.Languageannotations.jar包含的注释。它位于<installDirectory>/redist/annotations.jar或位于 maven central 中:g:"com.intellij" AND a:"annotations"注意:maven central 中的最新 jar 来自版本 12。它有你需要的一切。在 v14 中添加了一个非相关的注释. 我今天将把 v13 和 v14 的 jars 上传到 maven central(因为我过去一直这样做并且一直打算更新它们)。但它需要几天时间才能通过系统处理。)

Add the annotations JAR to your class path. Then annotate your method's Parameter as being SQL. Note that code completion will work when you type the language type:

将注释 JAR 添加到您的类路径。然后将您的方法的参数注释为 SQL。请注意,当您键入语言类型时,代码完成将起作用:

public void checkDatabase(String foo, @Language("SQL")String sql, String bar)

I like the annotation since even for non-IntelliJ IDEA users, it lets them know that a String should be valid SQL, XML, CSS, or whatever. You can also annotate a variable, or field: enter image description here

我喜欢这个注释,因为即使对于非 IntelliJ IDEA 用户,它也让他们知道字符串应该是有效的 SQL、XML、CSS 或其他任何东西。您还可以注释变量或字段: 在此处输入图片说明

If desired, you can change the annotation to use in Setting (Ctrl+Alt+S/ ?,) > Editor > Language Injections > Advanced

如果需要,您可以更改注释以在设置 ( Ctrl+ Alt+ S/ ?,) > 编辑器 > 语言注入 > 高级中使用

Comment

评论

As an alternative to annotations, you can use a comment. (I can't remember when this was added. So it may not be in v13). To the best of my knowledge though, language injection via comments only works for variables and fields. It does not work for parameters. For example: enter image description here

作为注释的替代方法,您可以使用注释。(我不记得这是什么时候添加的。所以它可能不在 v13 中)。不过,据我所知,通过注释进行的语言注入仅适用于变量和字段。它不适用于参数。例如: 在此处输入图片说明

P.S.See the help topics Help > Manuals > General Guidelines > Using Language Injections(available online here) and Help > Manuals > Language and Frameworks - Specific Guidelines > IntelliLang(available online here) for more information.

PS请参阅帮助主题Help > Manuals > General Guidelines > Using Language Injections(可在此处在线获取)和Help > Manuals > Language and Frameworks - Specific Guidelines > IntelliLang(可在此处在线获取)以获取更多信息。