Mybatis generator: a plug-in for mybatis to automatically generate entity code

Posted by cupboy on Wed, 01 Jan 2020 14:09:45 +0100

To use the Mybatis plug-in in maven project to automatically produce entity classes and mapping files, the following steps are required:

1. Create generatorConfig.xml file

(1) create plug-in file

(2) configure the path of relevant production files and the configuration of relevant parameters

2. Configure pom.xml

(1) add related dependency

(2) add plug-ins and specify the location of generatorConfig.xml file

3. Execute the mybatis plug-in command: mybatis generator: generate

Project directory structure

I. create generatorConfig.xml file first

<?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. Don't write the path wrong-->
    <classPathEntry  location="C:\Users\Administrator\.m2\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar"/>
    <context id="sqlserverTables"  targetRuntime="MyBatis3">
    <commentGenerator>
        <property name="suppressDate" value="true"/>
        <!-- Remove automatically generated comments or not true: Yes, false:no -->
        <property name="suppressAllComments" value="true"/>
    </commentGenerator>
    <!--Database links URL,User name, password“ test"Indicates the database name. Do not fill in the user and password incorrectly! -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/test" userId="liuchao" password="123">
    </jdbcConnection>
    <javaTypeResolver>
        <property name="forceBigDecimals" value="false"/>
    </javaTypeResolver>
    <!-- The package name and location of the generation model. The path can be specified by customization-->
    <javaModelGenerator targetPackage="com.liuchao.pojo" targetProject="src/main/java">
        <property name="enableSubPackages" value="true"/>
        <property name="trimStrings" value="true"/>
    </javaModelGenerator>
    <!-- The package name and location of the generated mapping file. The path can be customized-->
    <sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
        <property name="enableSubPackages"
                  value="true"/>
    </sqlMapGenerator>
    <!-- generate DAO The package name and location of. The path can be customized-->
    <javaClientGenerator type="XMLMAPPER" targetPackage="com.liuchao.mapper" targetProject="src/main/java">
        <property name="enableSubPackages" value="true"/>
    </javaClientGenerator>

    <!-- Table to generate tableName Is the table or view name in the database domainObjectName Is the entity class name-->
    <table tableName="t_user" domainObjectName="User"
           enableCountByExample="true" enableUpdateByExample="true" enableDeleteByExample="true"
           enableSelectByExample="true" selectByExampleQueryId="true" >
        <property name="useActualColumnNames" value="false"/>
    </table>
</context>
</generatorConfiguration>

II. Configure pom.xml

<!-- stay<project>Add the following configuration under the label -->
<build>
  <plugins>
     <plugin>
	<groupId>org.mybatis.generator</groupId>
	<artifactId>mybatis-generator-maven-plugin</artifactId>
	<version>1.3.2</version>
	<configuration>				      
            <configurationFile>
                <!-- This path is what we create generatorConfig.xml The full path of the file. Don't mix it up -->
                src/main/resources/generator/generatorConfig.xml
            </configurationFile>
	    <verbose>true</verbose>
	    <overwrite>true</overwrite>
        </configuration>
			
        <executions>
	   <execution>
	   <id>Generate MyBatis Artifacts</id>
	   <goals><goal>generate</goal></goals>
	   </execution>
	</executions>
	<dependencies>
         <dependency>
	        <groupId>org.mybatis.generator</groupId>
	        <artifactId>mybatis-generator-core</artifactId>
                <version>1.3.2</version>
         </dependency>
       </dependencies>
     </plugin>
   </plugins>
</build>

3. Execute the Mybatis plug-in command: Mybatis generator: generate

1. Right click the pom.xml file

2. Find Maven build and click

  

3. Click to open a new window and enter the command: mybatis generator: generate

Show SUCCESS to show that the code is produced successfully!

 

4, Build failed: exception analysis

Exception analysis: https://blog.csdn.net/qq_40943363/article/details/82633906

Topics: Mybatis xml Database MySQL