Forward Engineering
First, create Java entity classes, then the framework generates database tables based on them. Hibernate supports forward engineering.
Reverse Engineering
First, create the database tables, then the framework generates the folloiwng resources from them:
- Java entity clases
- Mapper interfaces
- Mapper XML mapping files
Steps to Set Up Reverse Engineering
Maven POM Configuration
<build>
<plugins>
<!-- Reverse engineering plugin -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<!-- Core dependency of the generator -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<!-- Database connection pool -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- MySQL JDBC driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Configuration File for Reverse Engineering
The configuration file must be placed at resource/generatorConfig.xml.
<?xml version="1.0" encoding="UTF-8"?>
<generatorConfiguration>
<properties resource="jdbc.properties" />
<!--
targetRuntime: Determines the version of generated code.
- MyBatis3Simple: Generates basic CRUD (clean and simple)
- MyBatis3: Generates CRUD with support for conditional queries (Example classes)
-->
<context id="DB2Tables" targetRuntime="MyBatis3">
<!-- Database connection details -->
<jdbcConnection driverClass="${jdbc.driver}"
connectionURL="${jdbc.url}"
userId="${jdbc.username}"
password="${jdbc.password}" />
<!-- Strategy for generating Java entity classes -->
<javaModelGenerator targetPackage="com.example.mybatis.entity"
targetProject=".\src\main\java">
<!-- Enable subpackages based on package name (each dot becomes a directory) -->
<property name="enableSubPackages" value="true" />
<!-- Trim whitespace from field values -->
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- Strategy for generating SQL mapping files -->
<sqlMapGenerator targetPackage="com.example.mybatis.mapper"
targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- Strategy for generating Mapper interfaces -->
<!-- type="XMLMAPPER" means the Mapper will rely on XML mapping files -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.example.mybatis.mapper"
targetProject=".\src\main\java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- Tables to reverse engineer -->
<!-- Use tableName="*" to target all tables; then domainObjectName is omitted -->
<table tableName="t_emp" domainObjectName="Employee" />
<table tableName="t_dept" domainObjectName="Department" />
</context>
</generatorConfiguration>
Execute the Generator
Run the Maven plugin: either from the command line (mvn mybatis-generator:generate) or via the configured IDE run target.
QBC (Query By Criteria) with MyBatis3
When the targetRuntime is set to MyBatis3, the generated Mapper interface includes the method selectByExample(Example example). This method lets you construct dynamic queries using an Example object.
First, create an instance of the Example class (e.g., EmployeeExample).
EmployeeExample example = new EmployeeExample();
Use createCriteria() to start building conditions. Then chain and{FieldName}{Criteria} methods to add individual conditions. All condition inside the same Criteria are joined with AND.
example.createCriteria()
.andEnameLike("A")
.andAgeGreaterThan(20)
.andDidIsNotNull();
To add alternative conditions (OR), call or() to start a new Criteria group and then apply AND conditions inside it.
example.or().andSexEqualTo("Male");
Finally, call the selectByExample method on the Mapper to retrieve the results.
List<Employee> employees = mapper.selectByExample(example);
Selective Updates
Generated Mappers also provide two variants of update methods:
updateByPrimaryKeySelective(new Employee()): Updates only the non-null fields of the provided entity. If a field isnull, the corresponding column in the database remains unchanged.updateByPrimaryKey(new Employee()): Updates all columns. If a field isnull, the corresponding column in the database is set toNULL.