MyBatis Generator Generated Java Client Objects

MyBatis Generator (MBG) generates Java client objects that are used to make interaction with the generated XML elements much easier. For each table in the configuration, MBG generates a MyBatis mapper interface. Generating Java client objects is optional, and is controlled by the <clientGenerator> configuration element.

Every field and method generated includes the jakarta.annotation.Generated annotation. In addition to providing good documentation, this enables the Java merger to understand what can be deleted on later generation runs. Any field or method in the class that does not include this annotation will be untouched. With this in mind, you can add other fields and methods to the classes without fear of losing them in later runs - simply DO NOT include the @Generated annotation on anything that you add to the class.

Note that the @Generated annotation includes the full name of the generator as a parameter and the Java merger will only interact with annotations from MBG.

Note: in the following descriptions, the term "BLOB" is used to refer to any column with a data type of BLOB, CLOB, LONGVARCHAR, or LONGVARBINARY.

Client Methods

Depending on the specifics of the table, and the configuration options, the Java client generator will generate these methods:

  • countByExample
  • deleteByPrimaryKey
  • deleteByExample
  • insert
  • insertSelective
  • selectByPrimaryKey
  • selectByExample
  • selectByExampleWithBLOBs
  • updateByPrimaryKey (with an override to specify whether to update BLOB columns)
  • updateByPrimaryKeySelective (will only update non-null fields in the parameter class)
  • updateByExample (with an override to specify whether to update BLOB columns)
  • updateByExampleSelective (will only update non-null fields in the parameter class)

MBG attempts to make it easier to deal with tables that contain BLOBs by generating different objects and methods so that you can use the BLOB fields, or ignore them, depending on the situation.

See the Example Class Usage page for an example of using the selectByExample method.

Client Usage in MyBatis

MyBatis mappers are interfaces that will map to methods in the generated XML mapper files (if you have chosen to generated XML). If you have chosen an annotated mapper, then the mapper interface contains all the generated code within itself. For example, suppose that MBG generated an interface called MyTableMapper. You can use the interface as follows:

  SqlSession sqlSession = sqlSessionFactory.openSession();

  try {
    MyTableMapper mapper = sqlSession.getMapper(MyTableMapper.class);
    List<MyTable> allRecords = mapper.selectByExample(null);
  } finally {
    sqlSession.close();
  }

See the standard MyBatis documentation for details on how to create the instance of sqlSessionFactory.