Idea uses the MyBatis Generator plug-in to quickly generate code

Keywords: Mybatis Database MySQL xml

;

1. Add in

Add the following dependencies to the pom.xml file.

Note that this is a plugin

<plugin>
     <groupId>org.mybatis.generator</groupId>
     <artifactId>mybatis-generator-maven-plugin</artifactId>
     <version>1.4.0</version>
</plugin>

2. Write generatorConfig.xml

Place the file under the root of resources

<?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>
    <!-- 1. Database driven:Select the database driver package on your local hard disk-->
    <classPathEntry
            location="C:\Users\lenovo\.m2\repository\mysql\mysql-connector-java\5.1.49\mysql-connector-java-5.1.49.jar"/>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- Remove automatically generated comments or not true: Yes: false:no -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!-- 2. Database links URL,User name, password -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/datadb?characterEncoding=utf8" userId="root"
                        password="root">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 3. Package name and location of the build model-->
        <javaModelGenerator targetPackage="com.example.demo.model" targetProject="D:/project/demo/generator/src/">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 4. Package name and location of the build mapping file-->
        <sqlMapGenerator targetPackage="com.example.demo.mapper" targetProject="D:/project/demo/generator/src/">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 5. generate DAO Package name and location for-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.dao" targetProject="D:/project/demo/generator/src/">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 6. Table to generate tableName Is the table or view name in the database domainObjectName Is the entity class name-->
        <table tableName="alexa" domainObjectName="Alexa" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="develop" domainObjectName="Develop" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="ios" domainObjectName="Ios" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="num" domainObjectName="Num" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="total" domainObjectName="Total" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>
        <table tableName="users" domainObjectName="User" enableCountByExample="false"
               enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false"></table>

    </context>
</generatorConfiguration>

  1. Please modify the corresponding configuration information according to your local environment
  2. targetProject must be real
  3. The table configuration to be generated depends on the tables in your own database

3. Run maven

4. Implementation effect

Remove the generator directory by moving the generated code to the path in the project

Posted by Benjigga on Tue, 05 May 2020 14:01:18 -0700