[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"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">


    <!--Extract version number for unified management-->

        <!--mysql Database driver package-->
        <!--Junit Unit test tools-->
        <!--mybatis-generator-core Reverse generation Java Code-->

            <!--mybatis Code generation plug-in-->

Step 2: create the db.properties property file

jdbc.driverLocation=Bring in local rack package path
jdbc.url=jdbc:mysql://localhost:3306 / database name? Characterencoding = utf8 & useunicode = true & usessl = false & servertimezone = UTC

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"

    <!--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 -->
            <!--Whether to remove automatically generated comments. true: yes  false: no-->
            <property name="suppressDate" value="true" />

        <!--jdbc Database connection for -->

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

        <!-- 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"/>

        <!--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"/>

        <!-- 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"/>

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

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

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

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