MyBatis Reverse Engineering Setup Guide

The MyBatis reverse engineering tool automatically generates Java code based on database tables. Here are the steps to set up reverse engineering using MyBatis Generator.

Create a Maven project or any other type of Java project.

Add the dependency for the MyBatis Generator plugin in the project’s pom.xml file, such as:

<dependency>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-core</artifactId>
    <version>1.4.0</version>
</dependency>

3. Configuring the execution of the MyBatis Generator plugin in the project’s pom.xml file, for example:

<build>
    <plugins>
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.4.0</version>
            <configuration>
                <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                <overwrite>true</overwrite>
                <verbose>true</verbose>
            </configuration>
            <executions>
                <execution>
                    <id>Generate MyBatis Artifacts</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>8.0.24</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

Create a configuration file named generatorConfig.xml under the src/main/resources directory in the project, to set up the generation rules for MyBatis Generator.

5. Configure database connection information, table names, and the package name for generated Java code in the generatorConfig.xml file. For example:

<generatorConfiguration>
    <context id="MyBatis3Simple" targetRuntime="MyBatis3">
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test"
                        userId="root"
                        password="root">
        </jdbcConnection>

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

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

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

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

Execute the command ‘mvn mybatis-generator:generate’ in the command line, or in your IDE right-click on the project and select “Run As” -> “Maven Build”, then enter ‘mybatis-generator:generate’ as goals to run the command and generate code.

The generated Java code will be automatically saved in the configured directory, allowing access to the database in the project using this generated code.

bannerAds