SQL templates are regular SQL statements that can contain references to runtime parameters and template code. SQL templates are used to avoid hard coding SQL into Java classes whilst still allowing SQL to be dynamically produced at runtime based on the available parameters.
Here is an example of how a SQL template is processed.
A SQL template file is an XML file containing related SQL templates, identified by a unique key. A template file can be used to populate a StatementPreparer with SQL templates.
A SQL template file is created for each entity in the application. Each file contains
the SQL templates relevant to that entity. For example, a SQL template file named
customer-sql.xml
is created for a "Customer" entity and
contains SQL templates used to manage the Customer
table.
Following is an example of the XML file format:
<sql-queries> <sql key="getAllCustomers"> <![CDATA[ SELECT * FROM Customers ]]> </sql> <sql key="getCustomer"> <![CDATA[ SELECT * FROM Customer WHERE customer_id = :customerId ]]> </sql> </sql-queries>