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:
- Create and fill out a configuration file appropriately (see below for samples)
- Save the file in some convenient location (like \temp\generatorConfig.xml)
- 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). - 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
|
Sample Configuration |
MyBatis3Kotlin |
|
Sample Configuration |
MyBatis3 | This is the original runtime. Before version 1.3.6 of MBG, most usages of MBG used this style of code.
|
Sample Configuration |
MyBatis3Simple | This is a simplified version of the MyBatis3 runtime.
|
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="MyBatis3"> <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>