Reverse engineering of mybatis in IntelliJ idea

Time:2019-12-21

Opening chapter

A practical function of mybatis is reverse engineering, which can generate entity classes in reverse according to the table structure, so as to avoid manual generation errors. Most of the tutorials on the market are very old. Most of them are for mysql5. Here is my experience when executing mysql8.

Introduction of Engineering

The Maven package management tool is used here. Add the following configuration in pom.xml to introduce mybatis.generator

<build>
    <finalName>SpringMVCBasic</finalName>
    <! -- add mybatis generator Maven plugin -- >
    <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 the generatorconfig.xml and generator.properties files in the Src / main / resources directory of 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 refers to the MySQL connector address corresponding to your local Maven library
2. Different from the old version, the 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>

    <! -- specifies the location (absolute path) of the jdbc driver jar package for a specific database
    <classPathEntry location="${jdbc.driverLocation}"/>

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

        <! -- optional, to control comments when creating a class -- >
        <commentGenerator>
            <! -- remove auto generated comments or not true: yes -- >
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

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

        <! -- non required, type processor, conversion control between database type and Java type -- >
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <! -- model model generator, used to generate classes with key, record classes and query example classes
            Targetpackage specifies the package name where the generated model is generated
            Targetproject specifies the path under the project
        -->
        <javaModelGenerator targetPackage="com.ifly.outsourcing.entity"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>


        <! -- the directory where mapper mapping file is generated generates the corresponding sqlmap file for each database table -- >
        <sqlMapGenerator targetPackage="mappers"
                         targetProject="src/main/resources">
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <! -- client code, generating easy-to-use code for model objects and XML configuration files
                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 file and independent 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 attention here is to modify the corresponding javamodelgenerator, sqlmapgenerator and javaclientgenerator to their own generation path. And add your own data table.

Add a run option to IntelliJ idea

Click Run in the menu bar, and create a new configuration with Maven as the option. Name is for your convenience. For example, generator and common line should be written as follows:

mybatis-generator:generate -e

Click Run to generate the corresponding file.

This article was first published in rootrl’s blog

Recommended Today

PHP realizes UnionPay business H5 payment

UnionPay business H5 payment interface document: document address 1: H5 payment interface address: 1: Alipay payment Test address: http://58.247.0.18:29015/v1/netpay/trade/h5-pay Official address: https://api-mop.chinaums.com/ 2: UnionPay payment Test address: http://58.247.0.18:29015/v1/netpay/uac/order Official address: https://api-mop.chinaums.com/ 2: Basic parameters required by the interface The interface uses get parameters. After the interface parameters are directly put into the interface address, the […]