Mybatis Reverse Engineering in Intellij Idea

Keywords: Java JDBC Mybatis Maven MySQL

Beginning

A useful feature of Mybatis is reverse engineering, which can reverse-generate entity classes based on table structure to avoid manual errors.Most of the tutorials on the market are very old, mostly for mysql5. Here's my experience with mysql8.

Introducing Engineering

Using the maven package management tool, add the following configuration to pom.xml to introduce mybatis.generator

<build>
    <finalName>SpringMVCBasic</finalName>
    <!-- Add to mybatis-generator-maven-plugin Plug-in unit -->
    <plugins>
      <plugin>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.2</version>
        <configuration>
          <verbose>true</verbose>
          <overwrite>true</overwrite>
        </configuration>
      </plugin>
    </plugins>
  </build>

configuration file

Create new generatorConfig.xml and generator.properties files in the src/main/resources directory under the maven project

generator.properties

jdbc.driverLocation=F:\\maven-repository\\mysql\\mysql-connector-java\\8.0.16\\mysql-connector-java-8.0.16.jar
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8
jdbc.userId=test
jdbc.password=test123

Be careful:
1, jdbc.driverLocation in generator.properties points to the corresponding mysql-connector address of your local maven Library
2, unlike older versions, here driversClass is com.mysql.cj.jdbc.Driver

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="generator.properties"></properties>

    <!--Specify a specific database's jdbc drive jar Location of the package(Absolute path)-->
    <classPathEntry location="${jdbc.driverLocation}"/>

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

        <!-- optional,Designed to create class When, control the comment -->
        <commentGenerator>
            <!--Whether to remove automatically generated comments true:yes-->
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--jdbc Database connection: driver class, link address, user name, password-->
        <jdbcConnection
                driverClass="${jdbc.driverClass}"
                connectionURL="${jdbc.connectionURL}"
                userId="${jdbc.userId}"
                password="${jdbc.password}">
        </jdbcConnection>

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

        <!-- Model Model Generator,Used to generate keys with primary keys key Classes, record classes, and queries for Example class
            targetPackage     Specify the generated model The package name where the build is located
            targetProject     Specify the path under this project
        -->
        <javaModelGenerator targetPackage="com.ifly.outsourcing.entity"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>


        <!--Mapper The directory in which the map file is generated generates the corresponding table for each database SqlMap file -->
        <sqlMapGenerator targetPackage="mappers"
                         targetProject="src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- Client code to generate easy-to-use targeting Model Object and XML Code for profile
                type="ANNOTATEDMAPPER",generate Java Model And note-based Mapper object
                type="MIXEDMAPPER",Generate annotation-based Java Model And corresponding Mapper object
                type="XMLMAPPER",generate SQLMap XML Files and stand-alone Mapper Interface
        -->

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

        <!-- Data table generation tableName:Table Name; domainObjectName:Corresponding DO -->
        <table tableName="user" domainObjectName="user"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

    </context>
</generatorConfiguration>

Note: The main focus here is to modify the corresponding javaModelGenerator, sqlMapGenerator, and javaClientGenerator for their own build paths.And add your own data tables.

Add a Run Run option to Intellij IDEA

Click run on the menu bar to create a new configurations with the option maven. name is convenient for you to see. For example, generator, commnd line note that it says:

mybatis-generator:generate -e

Click run to generate the corresponding file.

This article was first published in Rootrl's blog

Posted by Jeller on Thu, 07 Nov 2019 06:30:34 -0800