Reverse engineering of mybatis in IntelliJ idea


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

    <! -- add mybatis generator Maven plugin -- >

configuration file

Create the generatorconfig.xml and files in the Src / main / resources directory of Maven project


Be careful:
1. Jdbc.driverlocation in 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


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-// MyBatis Generator Configuration 1.0//EN"


    <! -- import property configuration -- >
    <properties resource=""></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 -- >
            <! -- remove auto generated comments or not true: yes -- >
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>

        <! -- database connection of JDBC: driver class, link address, user name, password -- >

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

        <! -- 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"
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>

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

        <! -- 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"
            <property name="enableSubPackages" value="true"/>

        <! -- data table generation tablename: table name; domainobjectname: corresponding do -- >
        <table tableName="user" domainObjectName="user"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"


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: Official address: 2: UnionPay payment Test address: Official address: 2: Basic parameters required by the interface The interface uses get parameters. After the interface parameters are directly put into the interface address, the […]