[MyBatis] reverse engineering use

Keywords: Mybatis JDBC Maven xml

Using mybatis reverse engineering, it is very convenient to automatically map with tables in the database, and generate mapper.xml and mapper interface.
Here I use IDEA to demonstrate through maven.

Step 1: configure the pom.xml file
The corresponding shelf package is introduced. The emphasis of reverse engineering is on the mybatis generator core shelf package and mybatis generator Maven plugin plug-in

<?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.xiaojiang.mybatisDemo02</groupId>
    <artifactId>mybatisDemo02</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--Extract version number for unified management-->
    <properties>
        <mybatis.version>3.4.6</mybatis.version>
        <mysql.version>8.0.15</mysql.version>
        <junit.version>4.10</junit.version>
        <generator.version>1.3.2</generator.version>
        <generator-plugin.version>1.3.2</generator-plugin.version>
    </properties>

    <dependencies>
        <!--MyBatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>${mybatis.version}</version>
        </dependency>
        <!--mysql Database driver package-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!--Junit Unit test tools-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <!--mybatis-generator-core Reverse generation Java Code-->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>${generator.version}</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>mybatis02</finalName>
        <plugins>
            <!--mybatis Code generation plug-in-->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>${generator-plugin.version}</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

Step 2: create the db.properties property file

jdbc.driverLocation=Bring in local rack package path
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306 / database name? Characterencoding = utf8 & useunicode = true & usessl = false & servertimezone = UTC
jdbc.username=root
jdbc.password=Password

Next, configure the key configuration file: create 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>

    <!--Import property configuration -->
    <properties resource="db.properties"></properties>

    <!--Specify the jdbc drive jar Location of packages-->
    <classPathEntry location="${jdbc.driverLocation}"/>

    <context id="default" targetRuntime="MyBatis3">
        <!-- optional,Aimed at creating class Control comments when -->
        <commentGenerator>
            <!--Whether to remove automatically generated comments. true: yes  false: no-->
            <property name="suppressDate" value="true" />
        </commentGenerator>

        <!--jdbc Database connection for -->
        <jdbcConnection
                driverClass="${jdbc.driver}"
                connectionURL="${jdbc.url}"
                userId="${jdbc.username}"
                password="${jdbc.password}"
        />

        <!-- Not required, type processor, in database type and java Conversion control between types-->
        <javaTypeResolver >
            <property name="forceBigDecimals" value="false" />
        </javaTypeResolver>

        <!-- Model Model builder,Used to generate a primary key key Class, record class and query of Example class
            targetPackage     Specify generated model Package name where the build is located
            targetProject     Specify the path under the project
        -->
        <javaModelGenerator targetPackage="com.xiaojiang.entities" targetProject="src/main/java">
            <!-- Is it right? model Add constructor -->
            <property name="constructorBased" value="true"/>

            <!-- Whether subpackages are allowed, i.e targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>

            <!-- Established Model Whether the object is immutable or not Model Object will not have setter Method, only construction method -->
            <property name="immutable" value="false"/>

            <!-- Are classes right? CHAR Type of column trim operation -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!--Mapper The directory where the mapping file is generated generates the corresponding SqlMap file -->
        <sqlMapGenerator targetPackage="com.xiaojiang.mapper" targetProject="src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- Client code, easy to use for Model Object and XML Code for profile
                type="ANNOTATEDMAPPER",generate Java Model And annotation based Mapper object
                type="MIXEDMAPPER",Generate annotation based Java Model And corresponding Mapper object
                type="XMLMAPPER",generate SQLMap XML Documentation and independent Mapper Interface
        -->
        <javaClientGenerator targetPackage="com.xiaojiang.dao" targetProject="src/main/java" type="XMLMAPPER">
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

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

This is the initial package structure of the project before using the maven plug-in.

Picture.png
Then click Run - > Edit configurations in the menu to configure the maven tool, and then click Apply.
Picture.png
Step 3: call Maven Projects on the side of IDEA, and then directly click Run or double-click mybatis generator: generate
Picture.png
Finally, we can see the result.
Picture.png

The configuration file of mybatis reverse engineering can be found here Official document of reverse engineering , reverse engineering, in the end, generatorConfig.xml configuration file is the most important.

Another implementation of reverse engineering is attached: Using Java program creation method

Posted by luddeb on Wed, 04 Dec 2019 15:53:41 -0800