-
Begin with an SQL statement. Test and tune it using your favourite SQL editor, or ask the database developers to do it for you.
123 -
Add the SQL to a comment in your Java code.
12345678Use the comment delimiter '/*-' to avoid formatting, or set your IDE's formatter to disable formatting of comments.
Warning:Make sure the comment is not formatted. Line breaks matter.
Add the @SQLComment annotation to the SQL statement. Set a statement name to be used as a reference. The attributes resultClass and configClass are used by the Maven plugin to generate the corresponding classes. Along with the result class, a mapper class is automatically generated to map database results to result class objects. The Java types used by the generated classes take into account the data types in your database. This ensures that SQLComments preserves type safety. See SQL Comments format for more.
-
To generate the appropriate classes, define the SQLComments Maven plugin in your build.
123456789101112131415161718192021222324252627282930313233343536373839Use Maven to generate classes.
1Goal export extracts all SQL statements into separate *.sql files. These files can also be created manually if you decide that storing SQL inside Java code is not suitable for you.
Goal generate generates result, mapper and configuration classes from *.sql files. If @SQLComment annotation does not define resultClass or configClass, class will not be generated.
There's also a third Maven goal, domain, which generates domain classes and basic SQL statements for CRUD operations. See more in Maven integration
-
Finish your Java code.
12345678910111213141516171819See more in Spring integration and How to create Spring repository class
That's all. If you encounter an issue with your SQL statement, don't waste time reproducing the scenario in the application. Instead, copy the SQL directly into your preferred SQL editor and test it as you normally would.
Continue on Documentation and tutorials