What content can MyBatis reverse engineering generate?

What content can MyBatis reverse engineering generate?

MyBatis reverse engineering can automatically generate Java entity classes, Mapper interfaces and XML files based on existing database tables. Here is an example:

  1. First, we need to create a Maven project and add the Mybatis-Generator plugin to the pom.xml file:
<build>
    <plugins>
        <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.4.0</version>
            <dependencies>
                <dependency>
                    <groupId>org.mariadb.jdbc</groupId>
                    <artifactId>mariadb-java-client</artifactId>
                    <version>2.2.5</version>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  1. Add the Mybatis-Generator configuration file (generatorConfig.xml) to configure information such as connection properties, generator properties, Java models, and mapping files.

Here, the MySQL database is taken as an example, and the example is as follows:

<?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>

    <context id="testTables" targetRuntime="MyBatis3">

        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8"
                        userId="root"
                        password="password">
        </jdbcConnection>

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

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

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

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

        <table tableName="t_test_user"
               domainObjectName="User"/>

    </context>
</generatorConfiguration>

In the above configuration file, we specify the database connection information, the path where the Java model and mapping files are stored, and the entity class and t_test_userrelated .

  1. Run the Mybatis-Generator plugin in the Maven project to perform reverse engineering.

First enter the directory where pom.xml is located, and run the following code on the command line:

mvn mybatis-generator:generate

After running successfully, you can find the automatically generated Java class and Mapper interface files in the target location (target/generated-sources by default), for example:

public class User {
    
    
    private Integer id;
    private String username;
    private String password;
    // getters 和 setters 略
}

public interface UserMapper {
    
    
    int deleteByPrimaryKey(Integer id);
    // ...
}

In addition, Mybatis reverse engineering also supports custom plug-ins, templates and other functions.

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/131166827