Springboot+mybatis project - automatically create DaoMapper class and mapper.xml through database table and entity class

Keywords: Mybatis xml Java MySQL

Recently, I practiced the springboot project, and found that the implementation of mapper.xml and DaoMapper interface class methods of mybatis framework is too arduous. Although it improves the flexibility, when there are too many tables, it is very tiring to write this part alone, so I found out how to create it through automation tools.

First, we need to add plug-ins to the pom.xml file

        <plugins>
            <!-- mybatis generator Auto generate code plug-in -->
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.5</version>
                <configuration>
                    <!--Location of profile-->
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
        </plugins>

Create the generatorConfig.xml file in the corresponding directory, as follows

<?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>

    <!-- Database driven:Select the database driver package on your local hard disk-->
    <classPathEntry location="C:/Users/fox/.m2/repository/mysql/mysql-connector-java/8.0.18/mysql-connector-java-8.0.18.jar" />

    <!-- Configure where data sources and generated code reside -->
    <context id="default" targetRuntime="MyBatis3">

        <!-- Specify generated java Coding of documents,Chinese may be garbled when not directly generated to the project -->
        <property name="javaFileEncoding" value="UTF-8"/>

        <!-- Generated entities Bean,Will achieve Serializable -->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin" />

        <!-- optional,Aimed at creating class Control comments when -->
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--Database connection information: driver class, connection address, user name, password -->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/robo?serverTimezone=UTC"
                        userId="root"
                        password="root">
            <property name="nullCatalogMeansCurrent" value="true"/>
            <property name="characterEncoding" value="utf-8" />
        </jdbcConnection>

        <!--java Type analysis-->
        <javaTypeResolver>
            <!--default false,hold JDBC DECIMAL and NUMERIC Type resolves to Integer
                //If true, resolve JDBC DECIMAL and NUMERIC types to Java. Math. BigDecimal -- >
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- generate entity Package name and location of class-->
        <javaModelGenerator targetPackage="com.yobotics.springboot_demo.entity"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- Generate corresponding mapper.xml Package name and location of the file-->
        <sqlMapGenerator targetPackage="mapper"
                         targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!--Generate corresponding Mapper Package name and location of interface class file,The type of profile is xml-->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.yobotics.springboot_demo.dao"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>

        <!-- Datasheet or view and entity class Bean Mapping -->
        <table schema="" tableName="t_user" domainObjectName="User"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>
        <table schema="" tableName="t_user_infomation" domainObjectName="UserInfomation"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

    </context>
</generatorConfiguration>

Then execute the configuration file below to create the DaoMapper interface class and mapper.xml file.

38 original articles published, 25 praised, 60000 visitors+
Private letter follow

Posted by ebgames56 on Mon, 20 Jan 2020 07:40:03 -0800