Store SQL Statements External File

Store SQL Statements External File

If you are using spring framework and looking for technique to store SQL Statements in external file to keep code clean from big SQL statements and make it flexible to change by other people as well who is not familiar to coding.

I would suggest you to create XML file and include bean definition with constructor HahsMap as an argument so that you could include multiple entries in it and dynamically get its value using key. Below is detail sample example:

  • Create sample maven StoreSQLOutSide project structure:

Store SQL Statements External File

  •  Sample queries.xml with one bean name: SQLFactory:

  • SQLFactory.java where constructor is loaded with one HashMap parameter

  • To show you how to retrieve SQL form query.xml file. I have added method in controller to get SQL queries:

  • dispatcher-servlet.xml where we are importing queries.xml:

Store SQL Statements External File

  • Download project and run this on any server. Please use below sample URL to see what queries are there in the file:

Store SQL Statements External File Store SQL Statements External File

  • That’s it. For more details please refer spring documentation here

download  Download Project:  StoreSQLOutSide

Leave a Reply

Your email address will not be published. Required fields are marked *