Introduction to MyBatis Generator

MyBatis Generator (MBG) is a code generator for MyBatis MyBatis. It will generate code for all versions of MyBatis. It will introspect a database table (or many tables) and will generate artifacts that can be used to access the table(s). This lessens the initial nuisance of setting up objects and configuration files to interact with database tables. MBG seeks to make a major impact on the large percentage of database operations that are simple CRUD (Create, Retrieve, Update, Delete). You will still need to hand code SQL and objects for join queries, or stored procedures.

MBG generates code in different styles, and for different languages, depending on how it is configured. For example, MBG can generate Java or Kotlin code. And MBG can generate MyBatis3 compatible XML - althought that is now considered a legacy use of MBG. The newer styles of generated code do not require XML.

Depending on how it is configured, MyBatis Generator may generate:

  • Java or Kotlin classes that match the table structure. This may include:
    • a class to match the primary key of the table (if there is a primary key)
    • a class to match the non-primary key fields of the table (except BLOB fields)
    • a class to include the BLOB fields of a table (if the table has BLOB fields)
    • a class to enable dynamic selects, updates, and deletes

    There is an inheritance relationship between these classes as appropriate. Note that the generator may be configured to generate different types of POJO hierarchies - for example, you may choose to generate a single domain object for each table if you so desire.

  • In some cases, MBG will generate MyBatis3 Compatible SQL Map XML Files. MBG generates SQL for simple CRUD functions on each table in a configuration. The generated SQL statements include:
    • insert
    • update by primary key
    • update by example (using a dynamic where clause)
    • delete by primary key
    • delete by example (using a dynamic where clause)
    • select by primary key
    • select by example (using a dynamic where clause)
    • count by example

    There are different variations of these statements depending on the structure of the table (for example, if the table doesn't have a primary key, then MBG will not generate an update by primary key function).

  • Java or Kotlin client classes, interfaces, and Kotlin extension methods that make appropriate use of the above objects. The generation of client classes is optional. MBG will generate a mapper interface that works with the MyBatis 3.x mapper infrastructure

MyBatis generator is designed to run well in an iterative development environment, and can be included as an Ant task, or a Maven plugin, in a continuous build environment. Important things to note when running MBG iteratively include:

  1. MBG will automatically merge XML files if there is an existing file with the same name as the newly generated XML file. MBG will not overwrite any custom changes you make to the XML files it generates. You can run it over and over again without fear of losing custom changes to you XML. MBG will replace any XML elements that were generated in a previous run.
  2. MBG will not merge Java files, it can either overwrite existing files or save newly generated files with a different unique name. If you make changes to the generated Java files and run MBG iteratively you will have to merge the changes by hand. When run as an Eclipse plugin, then MBG can automatically merge Java files.
  3. MBG will not merge Kotlin files, it can either overwrite existing files or save newly generated files with a different unique name. If you make changes to the generated Kotlin files and run MBG iteratively you will have to merge the changes by hand.

Running with Eclipse

When running as an Eclipse feature the generator can also merge Java files and save user modifications to the generated Java files. The generator uses the Eclipse Java parser and AST walker to accomplish this. The Eclipse feature also has a few user interface enhancements that make the generator somewhat easier to run. Lastly, the Eclipse feature contributes a full user manual for the generator to the Eclipse help system.

The Eclipse feature can be found on the Eclipse marketplace here: https://marketplace.eclipse.org/content/mybatis-generator.

Dependencies

MBG has no dependencies beyond the JRE. Java 8 or above is required. Also, a JDBC driver that implements the DatabaseMetaData interface, especially the getColumns and getPrimaryKeys methods is required.

Generated Code Dependencies

MBG will generate code in different styles. This is controlled by the "targetRuntime" attribute of a <context> element. All runtimes generate code that is dependent on MyBatis. Some of the runtimes generate code that is also dependent on MyBatis Dynamic SQL. The following table shows the required dependencies in the generated code.

Runtime MyBatis Generator Version MyBatis Version MyBatis Dynamic SQL Version
MyBatis3, MyBatis3Simple Any 3.0+ N/A
MyBatis3DynamicSQL 1.3.6 - 1.3.7 3.4.2+ 1.1.0 - 1.2.1
MyBatis3DynamicSQL, MyBatis3Kotlin 1.4.0 3.4.2+ 1.1.3+
MyBatis3DynamicSQL 1.4.1+ 3.4.2+ 1.3.1+
MyBatis3Kotlin 1.4.1+ 3.4.2+ 1.4.0+

Support

Support for MyBatis Generator is provided through the MyBatis user mailing list. You may subscribe to or view the mailing list through Google code here:

http://groups.google.com/group/mybatis-user

If you think you have found a bug, please ask a question about it on the user list first, before creating a new ticket. If you find a bug, or have a new feature request, you may open a new issue at GitHub here:

https://github.com/mybatis/generator/issues