MyBatis Dynamic SQL Usage Notes

This page is a short introduction to using the classes generated for the MyBatis3DynamicSQL runtime. These classes are dependent on the MyBatis Dynamic SQL library. Please refer to that site for full information about how the library works.

For each introspected table, the generator will generate three objects:

  1. A "record" class that represents a row in the table. This is the same as other runtimes
  2. A "support" class that includes a table definition and column definitions for the database table
  3. A mapper interface with all the typical methods generated by the other runtimes.

Important notes about the generated objects:

  • No XML is generated
  • No separate "Example" class is generated
  • There are no separate "with BLOBs" and "without BLOBs" methods. If your table includes BLOB fields, then they are included in all operations.
  • Model classes are always generated in the "flat" model meaning there is no separate primary key class or BLOBs class
  • Java 8 or higher is required
  • MyBatis 3.4.2 or higher is required

Format of the "Support" classes

A "support" class is created for each table. The support class includes a definition of the table and all the columns in the table. These items are used as input to the code generated in the mappers - most often for the where clauses you will write.

For example, suppose there is a table "TABLE_CODE" in schema "MYSCHEMA". Further suppose that table has "ID" and "DESCRIPTION" columns. The generated support class will look like this:

package example;

import java.sql.JDBCType;
import org.mybatis.dynamic.sql.SqlColumn;
import org.mybatis.dynamic.sql.SqlTable;

public final class TableCodeDynamicSqlSupport {
    public static final TableCode tableCode = new TableCode();
    public static final SqlColumn<Integer> id = tableCode.id;
    public static final SqlColumn<String> description = tableCode.description;

    public static final class TableCode extends SqlTable {
        public final SqlColumn<Integer> id = column("ID", JDBCType.INTEGER);
        public final SqlColumn<String> description = column("DESCRIPTION", JDBCType.VARCHAR);

        public TableCode() {
            super("MYSCHEMA.TABLE_CODE");
        }
    }
}

In your code, it is typical to import the static elements of this support class so they can be used directly in the code you write. With this static import you can refer to the fields in either a direct or qualified manner - "id" or "tableCode.id".

Usage of the Mapper Classes

The following methods work the same as the other runtimes and we won't cover them here:

  • Delete by Primary Key
  • Insert - will insert nulls
  • insert selective - ignores null properties
  • Select by Primary Key
  • Update by Primary Key - will set null values
  • Update by Primary Key Selective - ignores null values

The "by example" methods work very differently. The generator will create the following "by example" methods:

  • Count by example
  • Delete by example
  • Select by example
  • Update by example

Each of these methods includes support for a very flexible WHERE clause. Each method returns a builder object that requires you to optionally add a WHERE clause, then complete the builder and execute the method. If you don't supply a WHERE clause the statement will still work - but it will apply to all rows of a table. All builders are completed by calling the build method and finally the statement is executed by calling the the execute method.

For example, you can retrieve the total number of rows in a table by calling the count by example method without a WHERE clause. That code looks like this:

  long totalRows = mapper.countByExample()
          .build()
          .execute();

You can retrieve all rows in a table by calling select by example without a WHERE clause like this:

  List<TableCode> allRecords = mapper.selectByExample()
          .build()
          .execute();

It is far more interesting to add WHERE clauses to these methods. To add support for WHERE clauses, you should import the static elements from the support class as shown above, and you should also import the SqlBuilder support. Then you can code arbitrarily complex where clauses, and "ORDER BY" phrases as shown below:

import static example.TableCodeDynamicSqlSupport.*;  // import the generated "support" items
import static org.mybatis.dynamic.sql.SqlBuilder.*;  // import MyBatis Dynamic SQL where support

public class SomeService {

    public void simpleWhere {
        ...
        // Simple WHERE clause
        List<TableCode> records = mapper.selectByExample()
                .where(id, isEqualTo(3))
                .build()
                .execute();        
        ...
    }

    public void complexWhere1 {
        ...
        // Simple WHERE clause with OR
        List<TableCode> records = mapper.selectByExample()
                .where(id, isEqualTo(3))
                .or(description, isLike("f%"))
                .build()
                .execute();        
        ...
    }

    public void complexWhere2 {
        ...
        // complex WHERE and ORDER BY
        List<TableCode> records = mapper.selectByExample()
                .where(id, isLessThan(10), and(description, isEqualTo("foo")))
                .or(description, isLike("b%"))
                .orderBy(id.descending())
                .build()
                .execute();        
        ...
    }
}

The "by example" methods in this runtime are far more flexible then the "by example" methods in the other runtimes. Please the documentation for MyBatis Dynamic SQL to learn more about the options available.