Reverse engineering of mybatis

Time:2021-10-20

Reverse engineering of mybatis

1: What is retrograde engineering.

Mybatis is an excellent persistence layer framework, which supports customized SQL, stored procedures and advanced mapping. Mybatis avoids almost all JDBC code and manually setting parameters and getting result sets. Mybatis can use simple XML or annotations to configure and map native information, and map interfaces and Java POJOs (plain old Java objects) to records in the database.

When there are many database tables, the mapper that repeatedly creates POJO objects and simple database tables (crud) is inefficient. The official gives the use of mybatis generator to reverse generate POJO and mapper files according to database tables, which is very convenient for development.

2: Simple tutorial

  • Add a plug-in to Maven’s pom.xml
<packaging>jar</packaging>
<dependencies>

        <!--  Add dependency on mybatis -- >
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.miemiedev</groupId>
            <artifactId>mybatis-paginator</artifactId>
        </dependency>
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
        </dependency>
        <!-- MySql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--  Connection pool -- >
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
        </dependency>

        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.6</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!--  Mybatis reverse engineering -- >
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <!-- Location of configuration file -- >
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>
  • Create the configuration file generatorconfig.xml
<?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>
    <!-- Introducing database configuration files for easy modification -- >
    <properties resource="jdbc.properties"/>
    <!-- The database driver file needs to be modified todo -- >
    <classPathEntry
            location="/Volumes/Tool/JAVA_Tool/maven/Maven_Repository/mysql/mysql-connector-java/5.1.32/mysql-connector-java-5.1.32.jar"/>

    <context id="context" targetRuntime="MyBatis3Simple">
        <commentGenerator>
            <!--  Whether to remove automatically generated comments. True: Yes; False: no -- >
            <property name="suppressAllComments" value="true"/>
            <property name="suppressDate" value="false"/>
        </commentGenerator>
        <!-- Database connection information: driver class, connection address, user name, password -- >
        <jdbcConnection driverClass="${jdbc.driverClassName}"
                        connectionURL="${jdbc.url}"
                        userId="${jdbc.username}"
                        password="${jdbc.password}"/>
        <!-- The default is false. JDBC decimal and numeric types are resolved to integer, -- >
        <!-- When true, resolve JDBC decimal and numeric types to Java. Math. BigDecimal -- >
        <!--  Not required, type processor, conversion control between database type and Java type -- >
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- The location where Po class is generated configures the generated entity package -- >
        <!-- Targetpackage: the location of the generated entity package, which is stored in the SRC directory by default -- >
        <!-- Targetproject: relative path splicing result Src / main / Java / one / domain -- >
        <javaModelGenerator targetPackage="one.domain" targetProject="src/main/java">
            <!--  Enablesuppackages: whether to use schema as the suffix of the package -- >
            <property name="enableSubPackages" value="false"/>
            <!--  Spaces before and after the value returned from the database is cleaned -- >
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!--  The location and name of the mapping file corresponding to the entity package are stored in the SRC directory by default, the same as -- >
        <sqlMapGenerator targetPackage="MapperXml" targetProject="src/main/resources">
            <!--  Enablesuppackages: whether to use schema as the suffix of the package -- >
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <!-- Targetpackage: location generated by mapper interface -- >
        <javaClientGenerator targetPackage="one.mapper" type="XMLMAPPER" targetProject="src/main/java">
            <!--  Enablesuppackages: whether to use schema as the suffix of the package -- >
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        <!-- Configuration table -- >
        <!-- Schema: not required -- >
        <!-- Tablename: table name todo -- >
        <!--enableCountByExample、enableSelectByExample、enableDeleteByExample、enableUpdateByExample、selectByExampleQueryId:-->
        <!-- Remove automatically generated annotation examples -- >
        <table tableName="tb_user" enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false"/>
    </context>
</generatorConfiguration>
  • Run the generator class to generate

I have finished the project and can download it to run