【问题标题】:java properties file using multiple lines for one propertyjava 属性文件对一个属性使用多行
【发布时间】:2012-07-21 14:03:52
【问题描述】:

我将 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

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

【问题讨论】:

    标签: java spring properties


    【解决方案1】:

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

       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

    【讨论】:

    • 很好地添加到其他项目并解决了我的问题。谢谢!
    【解决方案2】:

    实际上,告诉 '\' 后面不能是任何东西,甚至不能是一个空格,这一点非常重要!

    【讨论】:

    • 这实际上应该是评论,而不是答案
    【解决方案3】:

    在行尾使用\,如

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

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

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

    【讨论】:

      【解决方案4】:

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

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-11
        • 1970-01-01
        • 1970-01-01
        • 2012-03-24
        • 2017-08-28
        相关资源
        最近更新 更多