Automating MyBatis Code Generation with Maven and Plugin Configuration

Setting Up the Maven Project

Begin by establishing a standard Maven project structure to house the generator configuration and dependencies.

Configuring Maven Dependencies and Build Plugin

Update the pom.xml to include the necessary libraries for MyBatis and the generator tool. The configuration below specifies the Maven plugin responsible for executing the generation process.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>mybatis-auto-gen</artifactId>
    <version>1.0.0</version>

    <dependencies>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.4.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.23</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.0</version>
                <executions>
                    <execution>
                        <id>Generate MyBatis Artifacts</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <configurationFile>src/main/resources/config/mybatis-generator.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Generator Configuration File

Create the mybatis-generator.xml file within src/main/resources/config/. This XML defines the database connection, Java model generation settings, and the specific tables to be processed.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC
        "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <properties resource="config/database.properties"/>

    <classPathEntry location="${db.driverPath}"/>

    <context id="ProductionDB" targetRuntime="MyBatis3DynamicSql">
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="true"/>
        </commentGenerator>

        <jdbcConnection
                driverClass="${db.driver}"
                connectionURL="${db.url}"
                userId="${db.username}"
                password="${db.password}">
        </jdbcConnection>

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <javaModelGenerator targetPackage="com.example.model" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <sqlMapGenerator targetPackage="mappings" targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <table schema="${db.schema}" tableName="users" domainObjectName="User">
            <generatedKey column="id" sqlStatement="JDBC" identity="true"/>
        </table>
    </context>
</generatorConfiguration>

Database Properties

Define the connection parameters in src/main/resources/config/database.properties. This keeps sensitive information and environment-specific settings out of the main XML configuration.

db.driverPath=/path/to/local/repo/mysql/mysql-connector-java/8.0.23/mysql-connector-java-8.0.23.jar
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/app_db?useSSL=false&serverTimezone=UTC
db.username=developer
db.password=securePass
db.schema=app_db

Executing the Code Generation

To run the generator, use the Maven command line. Navigate to the project root and execute the following command:

mvn mybatis-generator:generate

Alternatively, within an IDE like IntelliJ IDEA, navigate to Run > Edit Configurations. Add a new Maven configuration. In the Command line field, input mybatis-generator:generate. Name this configuration (e.g., "Gen-MyBatis") and execute it.

IDE Plugin Integration

For enhanced development support, installing the "MyBatis Plugin" (available in the IntelliJ IDEA marketplace) is recommended. This utility provides navigation between XML mapper files and their corresponding Java interfaces, as well as auto-completion for SQL IDs and parameters.

Tags: MyBatis java Maven Code Generation database

Posted on Thu, 07 May 2026 03:30:36 +0000 by bodzan