43

我将 sql 存储在属性文件中并使用 spring 注入它,这很有效:

someSQL = select result from myTable where y = 2 and x = ? order by z

但为了便于阅读,我想要这个:

    someSQL = select result 
              from myTable 
              where y = 2  
              and x = ? 
              order by z

我需要使用的正确文本格式是什么?

4

4 回答 4

72

在行尾使用 \ 就像

  someSQL = select result \
              from myTable \
              where y = 2  \
              and x = ? \
              order by z

此外,请注意任何尾随空格,因为 Java 在组合行时会查找连续的反斜杠+换行符。

换句话说:反斜杠必须是换行前的最后一个字符。

于 2012-07-23T10:32:38.273 回答
9

您添加 \(斜杠)以继续到下一行。属性文件将是这样的 -

prop1=first line of prop1 \
second line of prop1\
third line of prop1
prop2=first line of prop2 \n \
second line of prop2 \n \
third line of prop2
于 2012-07-23T10:34:20.327 回答
7

使用\ 换行,并确保每个\之前有一个空格

   someSQL = select result \
              from myTable \
              where y = 2  \
              and x = ? \
              order by z \

如果没有给出一个空格,输出将是这样的

 someSQL = select result\
              from myTable\
              where y = 2\
              and x = ?\
              order by z\
    someSQL=select resultfrom myTablewhere y = 2and x = ?order by z
              

这将导致

Java级别: java.sql.SQLSyntaxErrorException

数据库级别 Missing Keyword

于 2018-07-26T10:53:13.613 回答
-1

实际上很重要的一点是,在“\”之后一定不能是任何东西,甚至不能是一个空格

于 2017-07-18T15:39:43.243 回答