IntelliJ IDEA - Java代码中SQL语法的突出显示 [英] IntelliJ IDEA - Syntax Highlighting of SQL Inside Java Code

查看:4067
本文介绍了IntelliJ IDEA - Java代码中SQL语法的突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用IntelliJ 13作为项目的IDE,通过Spring的JDBC模板与DB进行通信。当我在Java中使用代码片段时,如下所示:

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

其中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>  






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

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

同一查询的

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

没有语法高亮显示SQL语句。
您是否知道如何为不是JdbcTemplate或java.sql接口的方法参数的Java stings启用SQL staments的语法高亮?

解决方案

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



设置



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


  1. 打开设置( Ctrl + Alt + S / )>编辑器>语言注入

  2. 在右侧,单击添加按钮并选择 Java参数

  3. 语言注入设置对话框中,选择 SQL (或所需语言) ID字段


    • 您还可以指定特定的SQL方言,例如 MySQL Oracle 等。


  4. 在类方法字段中,输入要识别的方法采用 SQL 参数。您可以通过省略号按钮打开课程浏览器/搜索窗口


    • 您可以从代码或库中选择类


  5. 如果您的方法需要多个参数,您可以在对话框中选择参数:

现在,当您使用该方法时,IntelliJ IDEA将自动应用语言注入:



注释



您可以将方法参数(或变量或字段)注释为特定语言类型。默认情况下,IntelliJ IDEA将使用 annotations.jar org.intellij.lang.annotations.Language 注释。 > IntelliJ IDEA附带。它位于< installDirectory> /redist/annotations.jar 或maven central:)和帮助>手册>语言和框架 - 特定指南> IntelliLang (可用在线)了解更多信息。


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:

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):


.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?

解决方案

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) IntelliLang Plugin enabled.

Setting

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

  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 Settings dialog, 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 SQL parameter. You can open a class browser/search window via the ellipsis button
    • 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:

Now IntelliJ IDEA will automatically apply language injection when you use that method:

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.Language annotation that is in the annotations.jar included with IntelliJ IDEA. It is located at <installDirectory>/redist/annotations.jar or 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.)

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:

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:

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

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:

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.

这篇关于IntelliJ IDEA - Java代码中SQL语法的突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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