Java and SQL hand in hand

SQL Comments keeps SQL statements clear for your database tools and still without loosing type safety and configurability in Java.

Code Generation

Generic Create - Read - Update - Delete operations are ready out of the box. Maven plugin 'domain' goal is used to generate domain classes and CRUD statements.

Multi-DB support

Using SQL Comments your application can support multiple databases. Moreover it is easy to configure  different SQL statements for specific database engines and profit from their non-standard functions.

Native SQL Statements

SQL statements are used in native form. There's no need to rewrite SQL statement to domain specific language in Java. Anytime you can do copy&paste into your favorite database editor and tune statement.

Type safety

Exact result and configuration POJO classes containing properties with appropriate Java types are generated from SQL statements. Statement is checked not only for Java compliance but also against database schema.

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:

  1. all placeholders must have non-null value (if not configured otherwise) and
  2. JavaScript expression must return boolean value 'true'.

Learn more in Quick Start Guide or see full Documentation and Tutorials.