MyBatis Generator Quick Start Guide

MyBatis Generator (MBG) generates code in different styles depending on how it is configured. This is controlled by specifying the targetRuntime attribute on a <context> configuration element. The table below summarizes the different options.

To get up and running quickly with MyBatis Generator (MBG), follow these steps:

  1. Create and fill out a configuration file appropriately (see below for samples)
  2. Save the file in some convenient location (like \temp\generatorConfig.xml)
  3. Run MBG from the command line with a command like this:
    
          java -jar mybatis-generator-core-x.x.x.jar -configfile \temp\generatorConfig.xml -overwrite
        

    This will tell MBG to run using your configuration file. It will also tell MBG to overwrite any existing Java or Kotlin files with the same name. If you want to save any existing files, then omit the -overwrite parameter. If there is a conflict, MBG will save the newly generated file with a unique name (e.g. MyClass.java.1).

  4. After running MBG, you will need to create or modify the standard MyBatis configuration make use of your newly generated code. See the Tasks After Running MyBatis Generator page for more information.

Target Runtime Information and Samples

Target Runtime Comments Sample Configuration
MyBatis3DynamicSql This is the default value
  • Generates Java code
  • Does not generate XML - MyBatis3 annotations are used exclusively
  • The generated model objects are "flat" - there is no separate primary key object
  • The generated code is dependent on the MyBatis Dynamic SQL Library
  • The amount of generated code is relatively small
  • The generated code allows tremendous flexibility in query construction
Sample Configuration
MyBatis3Kotlin
  • Generates Kotlin code
  • Does not generate XML - MyBatis3 annotations are used exclusively
  • The generated model objects are "flat" - there is no separate primary key object
  • The generated code is dependent on the MyBatis Dynamic SQL Library
  • The amount of generated code is relatively small
  • The generated code allows tremendous flexibility in query construction
Sample Configuration
MyBatis3 This is the original runtime. Before version 1.3.6 of MBG, most usages of MBG used this style of code.
  • Generates Java code
  • Generates MyBatis3 compatible XML and SQL or MyBatis3 compatible annotated interfaces with no XML
  • The generated model objects may have a hierarchy with separate primary key objects and/or separate object with BLOB fields
  • The generated code has no external dependencies
  • The amount of generated code is very large
  • The generated code has limited capabilities for query construction and is difficult to extend
Sample Configuration
MyBatis3Simple This is a simplified version of the MyBatis3 runtime.
  • Generates Java code
  • Generates MyBatis3 compatible XML and SQL or MyBatis3 compatible annotated interfaces with no XML
  • The generated model objects are "flat" - there is no separate primary key object
  • The generated code has no external dependencies
  • The amount of generated code is relatively small
  • No "by example" or "selective" methods are generated
  • The generated code does not include methods for dynamic query construction and is difficult to extend
Sample Configuration

Sample Configuration for MyBatis3DynamicSql

<!DOCTYPE generatorConfiguration PUBLIC
 "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
  <context id="dsql" targetRuntime="MyBatis3DynamicSql">
    <jdbcConnection driverClass="org.hsqldb.jdbcDriver"
        connectionURL="jdbc:hsqldb:mem:aname" />

    <javaModelGenerator targetPackage="example.model" targetProject="src/main/java"/>

    <javaClientGenerator targetPackage="example.mapper" targetProject="src/main/java"/>

    <table tableName="FooTable" />
  </context>
</generatorConfiguration>

Sample Configuration for MyBatis3Kotlin

<!DOCTYPE generatorConfiguration PUBLIC
 "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
  <context id="kotlin" targetRuntime="MyBatis3Kotlin">
    <jdbcConnection driverClass="org.hsqldb.jdbcDriver"
        connectionURL="jdbc:hsqldb:mem:aname" />

    <javaModelGenerator targetPackage="example.model" targetProject="src/main/kotlin"/>

    <javaClientGenerator targetPackage="example.mapper" targetProject="src/main/kotlin"/>

    <table tableName="FooTable" />
  </context>
</generatorConfiguration>

Sample Configuration for MyBatis3

<!DOCTYPE generatorConfiguration PUBLIC
 "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
  <context id="simple" targetRuntime="MyBatis3Simple">
    <jdbcConnection driverClass="org.hsqldb.jdbcDriver"
        connectionURL="jdbc:hsqldb:mem:aname" />

    <javaModelGenerator targetPackage="example.model" targetProject="src/main/java"/>

    <sqlMapGenerator targetPackage="example.mapper" targetProject="src/main/resources"/>

    <javaClientGenerator type="XMLMAPPER" targetPackage="example.mapper" targetProject="src/main/java"/>

    <table tableName="FooTable" />
  </context>
</generatorConfiguration>

Sample Configuration for MyBatis3Simple

<!DOCTYPE generatorConfiguration PUBLIC
 "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
  <context id="simple" targetRuntime="MyBatis3Simple">
    <jdbcConnection driverClass="org.hsqldb.jdbcDriver"
        connectionURL="jdbc:hsqldb:mem:aname" />

    <javaModelGenerator targetPackage="example.model" targetProject="src/main/java"/>

    <javaClientGenerator type="ANNOTATEDMAPPER" targetPackage="example.mapper" targetProject="src/main/java"/>

    <table tableName="FooTable" />
  </context>
</generatorConfiguration>