如何在Java中添加多行字符串?

huangapple go评论89阅读模式
英文:

How to add multiline Strings in Java?

问题

如何使长查询更易读?

例如,我有以下这个查询:

String query = "SELECT CASE WHEN EXISTS (SELECT * FROM users WHERE username = 'username' AND user_password = crypt('password', user_password)) THEN 'match' ELSE 'differ' END";

这个查询完全无法阅读,有没有什么方法可以使其更加美观?

英文:

How to make long queries more readable?

For example I have this one:

String query = "SELECT CASE WHEN EXISTS (SELECT * FROM users WHERE username = 'username' AND user_password = crypt('password', user_password)) THEN 'match' ELSE 'differ' END";

And it's completely unreadable, are there any ways to beautify it?

答案1

得分: 3

自 Java 15 版本开始,你可以使用文本块

String query = """
               SELECT CASE 
                  WHEN 
                      EXISTS (
                         SELECT * 
                         FROM users 
                         WHERE 
                             username = 'username' 
                             AND user_password = crypt('password', user_password)
                      ) 
                  THEN 'match' 
                  ELSE 'differ' 
                  END
                """;
英文:

Since Java 15, you can use text blocks:

String query = """
               SELECT CASE 
                  WHEN 
                      EXISTS (
                         SELECT * 
                         FROM users 
                         WHERE 
                             username = 'username' 
                             AND user_password = crypt('password', user_password)
                      ) 
                  THEN 'match' 
                  ELSE 'differ' 
                  END
                """;

答案2

得分: 2

  1. 在不希望将SQL和JAVA混合在一起的情况下,您可以将SQL查询放入一个 .sql 文件中。在需要时获取此文本。

     public class QueryUtil {
         static public String getQuery(String fileName) throws IOException {
             Path path = Paths.get("src/test/resources/" + fileName + ".sql");
             return Files.readAllLines(path).get(0);
         }
     }
    
  2. 如果您可以混合使用SQL和JAVA,从JDK 15开始,您可以使用文本块来实现此目的。

  3. 此外,您可以使用JOOQ从数据库中生成Java代码,它提供了许多优势。

英文:
  1. In cases when you don't wont to blend SQL and JAVA you can put SQL queries in an .sql file. And get this text when needed.

    public class QueryUtil {
        static public String getQuery(String fileName) throws IOException {
            Path path = Paths.get("src/test/resources//" + fileName + ".sql");
            return Files.readAllLines(path).get(0);
        }
    }
    
  2. If you can mix SQL and JAVA then starting from JDK15 you can use text blocks for this.

  3. Also you can generates Java code from your database by using JOOQ, it gives many benefits.

答案3

得分: 1

假设您无法升级到比8更新的Java版本(即使可以升级),目前最好的解决方案是使用对象关系映射(ORM)。对于Java而言,主要可以选择HibernatejOOQ。jOOQ(可能也包括Hibernate,但我没有使用过,很抱歉)允许您使用流畅的编程接口,这与现有的Java代码风格和模式非常一致。

使用ORM的另一个具体优势是,您可以非常轻松地更改所使用的数据库引擎,而无需更改您编写的Java代码,只需要在设置函数中更改SQL方言。请参阅https://www.jooq.org/javadoc/latest/org.jooq/org/jooq/SQLDialect.html。

英文:

Assuming that you can't move to a newer-than-8 version of Java (or even if you can), by far the best solution is to use an ORM. For Java it pretty much comes down to Hibernate, or jOOQ. jOOQ (and possibly Hibernate, I haven't used it so can't say, sorry) allows you to use a fluent programming interface, which is very much in keeping with existing Java code style and patterns.

Another specific advantage of using an ORM is that you can very easily change which DB engine you use without having to change the Java code that you've written beyond changing the SQL dialect in your setup functions. See https://www.jooq.org/javadoc/latest/org.jooq/org/jooq/SQLDialect.html.

答案4

得分: 1

你可以使用JOOQ,并获得诸如类型安全、自动完成、简便映射和出色支持等多个其他好处。

迄今为止,我已在几个项目中使用它,并且还尝试过像Kotlin Exposed之类的竞争对手,但始终回到了JOOQ。

英文:

You can use JOOQ and get multiple other benefits like type safety, auto-complete, easy mapping and great support.

Have used it for several projects so far and also competition like Kotlin Exposed but always came back to JOOQ.

答案5

得分: 0

移至Java 13+。有文本块可供使用。

或者使用一些ORM库

英文:

Move to Java 13+. There are Text Blocks for this.

Or use some ORM library.

huangapple
  • 本文由 发表于 2020年10月18日 04:09:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/64406846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定