springboot integrates mybatis reverse engineering solution

Using method of mybatis reverse engineering

Spring boot integrates mybtis reverse engineering. I use the same principle of idea, spring boot, maven and eclipse.

1. Add required plug-ins and jar packages

<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <dependencies>
        <dependency>
            <groupId> mysql</groupId>
            <artifactId> mysql-connector-java</artifactId>
            <version>5.1.27</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>
    <executions>
        <execution>
            <id>Generate MyBatis Artifacts</id>
            <phase>package</phase>
            <goals>
                <goal>generate</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <!--Allow the generated files to be moved -->
        <verbose>true</verbose>
        <!-- Is it covered? -->
        <overwrite>false</overwrite>
        <!-- Auto generated configuration -->
        <configurationFile>
            src/main/resources/mybatis-generator.xml</configurationFile>
    </configuration>
</plugin>         

2. Add the configuration file "mybatis-generator.xml" in the root directory of the project. The file name must not be wrong, otherwise no exception is found in the report.

Here are the contents of the profile pair

<?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>
    <!--mysql Connect to database jar Choose your local location here-->
    <classPathEntry
            location="D:\apache-maven-3.3.9\maven-repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar"/>
    <context id="testTables" targetRuntime="MyBatis3">
        <commentGenerator>
            <!-- Remove automatically generated comments or not true: Yes, false:no -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--Database connection information: driver class, connection address, user name, password -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=UTC" userId="root"
                        password="tanghh">
        </jdbcConnection>
        <!-- default false,hold JDBC DECIMAL and NUMERIC Type resolves to Integer,by true Shi Ba JDBC DECIMAL and
           NUMERIC Type resolves to java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- targetProject:generate PO Class location -->
        <javaModelGenerator targetPackage="com.orangesay.entity"
                            targetProject="src/main/java">
            <!-- enableSubPackages:Whether to let schema As suffix of package -->
            <property name="enableSubPackages" value="false"/>
            <!-- Spaces before and after values returned from the database are cleaned up -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- targetProject:mapper Location of map file generation
           //If maven project is only a separate project, targetProject="src/main/java"
           //If maven project is a sub module project, targetProject = "name of module", for example:
           targetProject="ecps-manager-mapper",Same below-->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src/main/resources">
            <!-- enableSubPackages:Whether to let schema As suffix of package -->
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>
        <!-- targetPackage: mapper Location of interface generation -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.orangesay.mapper"
                             targetProject="src/main/java">
            <!-- enableSubPackages:Whether to let schema As package suffix -->
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>
        <!-- Specify multiple representations of database tables,More than one can be used. table Label-->
        <table tableName="user"
               enableCountByExample="false"
               enableUpdateByExample="false"
               enableDeleteByExample="false"
               enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
    </context>
</generatorConfiguration>

Note the following:

  • The path of mysql-connector-java-5.1.46.jar is the local absolute path
  • Database connection address, user name and password need to be modified to their own database information
  • The location of pojo class, dao interface and mapper configuration file are all modified to their own
  • The last table tag contains the table name. Multiple tables can be used

3. Run mybatis generator: generate to generate pojo, dao and mapper layers

springboot integrates mybatis reverse engineering and it's finished!!!

Tags: Mybatis MySQL Java Maven

Posted on Sun, 09 Feb 2020 12:55:03 -0500 by michibk