Good example is better than all 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 -- Row will be used in query only if companyId is not null
AND comp.name LIKE :name
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 the native form as used in database tools could be put in separate files or directly into Java code as a comment. SQL Comments Maven plugins parse java code and generates input and output classes including transformers from JDBC ResultSet.
Generating SQL statements in runtime could be controlled by JavaScript expressions at the end of line as SQL comment. There are two simple conditions which must be fulfilled for row to be added into statement:
- all placeholders must have non-null value (if not configured otherwise) and
- JavaScript expression must return boolean value 'true'.
Learn more in Quick Start Guide or see full Documentation and Tutorials.