Write SQL statements in the. sql file in the Spring project

Source: Internet
Author: User

When we use JDBC, if we write all the SQL statements in the Java file, because Java does not support here document, the multiline string either uses the plus sign or the Java 8 String.join()method to connect, while not syntax highlighting for SQL statements, so the SQL string is poorly read. Don't say why not just write the original SQL statements without Hibernate, or use the jdbctemplate when operating a complex system.

So we want to be able to write the SQL statements in a separate*.sqlFile, so many editors can highlight the syntax, or you can get smart hints when typing. There is a way to*.sqlAs a property file, the SQL statement that defines multiple rows in it is

Select.user=select ID, FirstName, LastName, address \
from users \
where id=?

It can be used after loadinggetProperty("select.user")To refer to the corresponding statement. The Wrap property file is the same as Bash\, but so, then*.sqlNot a pure SQL file, not correct syntax highlighting, once the SQL comments are written--It's even more of a troublemaker.

So our second scenario is: first*.sqlIt should be a real SQL file, not a spoofed property file, so how do we represent the individual keys in order to be able to reference each SQL statement in the program? The inspiration here still comes from the Linux shell, which specifies the execution environment in the Linux shell in a special way.#!Such as

#!/bin/bash
#!/usr/bin/env python

Read the full text >>


Write SQL statements in the. sql file in the Spring project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.