|
| 1 | +# Specialized Support for MyBatis3 |
| 2 | +Most of the examples shown on this site are for usage with MyBatis3. But the library does have some additional support for MyBatis3 beyond what is shown in the other examples. |
| 3 | + |
| 4 | +This support is added to the DELETE, SELECT, and UPDATE statement generators and enables the creating of reusable "by example" methods as delivered in MyBatis Generator. These methods can provide some boilerplate code for the setup of the statement (a column list and table name for example), and allow the user to specify a where clause. |
| 5 | + |
| 6 | +For example, it is possible to write a mapper like this: |
| 7 | + |
| 8 | +```java |
| 9 | +import static examples.simple.SimpleTableDynamicSqlSupport.*; |
| 10 | + |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +import org.apache.ibatis.annotations.Mapper; |
| 14 | +import org.apache.ibatis.annotations.Result; |
| 15 | +import org.apache.ibatis.annotations.Results; |
| 16 | +import org.apache.ibatis.annotations.SelectProvider; |
| 17 | +import org.apache.ibatis.type.JdbcType; |
| 18 | +import org.mybatis.dynamic.sql.select.MyBatis3SelectModelAdapter; |
| 19 | +import org.mybatis.dynamic.sql.select.QueryExpressionDSL; |
| 20 | +import org.mybatis.dynamic.sql.select.SelectDSL; |
| 21 | +import org.mybatis.dynamic.sql.select.render.SelectStatement; |
| 22 | +import org.mybatis.dynamic.sql.util.SqlProviderAdapter; |
| 23 | + |
| 24 | +@Mapper |
| 25 | +public interface SimpleTableAnnotatedMapper { |
| 26 | + |
| 27 | + @SelectProvider(type=SqlProviderAdapter.class, method="select") |
| 28 | + @Results(id="SimpleTableResult", value= { |
| 29 | + @Result(column="A_ID", property="id", jdbcType=JdbcType.INTEGER, id=true), |
| 30 | + @Result(column="first_name", property="firstName", jdbcType=JdbcType.VARCHAR), |
| 31 | + @Result(column="last_name", property="lastName", jdbcType=JdbcType.VARCHAR), |
| 32 | + @Result(column="birth_date", property="birthDate", jdbcType=JdbcType.DATE), |
| 33 | + @Result(column="employed", property="employed", jdbcType=JdbcType.VARCHAR, typeHandler=YesNoTypeHandler.class), |
| 34 | + @Result(column="occupation", property="occupation", jdbcType=JdbcType.VARCHAR) |
| 35 | + }) |
| 36 | + List<SimpleTableRecord> selectMany(SelectStatement selectStatement); |
| 37 | + |
| 38 | + default QueryExpressionDSL<MyBatis3SelectModelAdapter<List<SimpleTableRecord>>>.QueryExpressionAfterFrom selectByExample() { |
| 39 | + return SelectDSL.select(this::selectMany, id.as("A_ID"), firstName, lastName, birthDate, employed, occupation) |
| 40 | + .from(simpleTable); |
| 41 | + } |
| 42 | + |
| 43 | +} |
| 44 | +``` |
| 45 | + |
| 46 | +Notice the `selectByExample` method - it specifies the column list and table name and returns the intermediate builder that can be used to finish the WHERE clause. It also reuses the `selectMany` mapper method. Mapper methods built using this added support all finish with an `execute` method that builds the statement and executes the mapper method. |
| 47 | + |
| 48 | +The code is used like this: |
| 49 | + |
| 50 | +```java |
| 51 | + List<SimpleTableRecord> rows = mapper.selectByExample() |
| 52 | + .where(id, isEqualTo(1)) |
| 53 | + .or(occupation, isNull()) |
| 54 | + .build() |
| 55 | + .execute(); |
| 56 | +``` |
| 57 | + |
| 58 | +It is expected that a future version of MyBatis Generator will generate code that looks like this. |
0 commit comments