A good example is worth a thousand words.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public List<CompanyInfo> findCompanies(CompanyFilter filter) {
/*-
@SQLComment(name="findCompanies", resultClass="example.CompanyInfo", configClass="example.FindCompaniesConfig")
SELECT comp.id, comp.name, country.name
FROM companies comp JOIN countries country ON comp.countryId = country.id
JOIN employees emp ON comp.id = emp.companyId -- Add join only if //@ ceo != null
WHERE 1 = 1
AND comp.id = :companyId -- Rows containing placeholders will only be used
AND comp.name LIKE :name -- in a query if all placeholders are not null.
AND emp.name LIKE :ceo
ORDER BY
country.name -- Only if //@ order == 'country'
comp.name ASC, country.name DESC -- Only if //@ order == 'company'
*/
// Set statement parameters
FindCompaniesConfig config = new FindCompaniesConfig();
config.setCompanyId(filter.getCompanyId());
// Configure paging if needed
config.limit(20);
config.offset(0);
// Load data
List<CompanyInfo> companies = list(config, CompanyInfoMapper.INSTANCE);
return companies;
}
SQL statements in their native form, as used in database tools, can be placed in separate files or inserted directly into Java code as comments. The SQL Comments Maven plugin parses Java code and generates input and output classes, including transformers from JDBC ResultSet.
The generation of SQL statements at runtime can be controlled by JavaScript expressions at the end of a line, which are treated as SQL comments. There are two simple conditions that must be met for a row to be added to the statement:/p>
- all placeholders must have a non-null value (unless configured otherwise) and
- the JavaScript expression must return the boolean value 'true'.
You can find out more in the Quick Start Guide, or view the full Documentation and Tutorials.