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 Generator Configuration 1.0//EN"
    <!-- 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">
        <property name="suppressDate" value="true"/>
        <!-- Remove automatically generated comments or not true: Yes, false:no -->
        <property name="suppressAllComments" value="true"/>
    <!--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://" userId="liuchao" password="123">
        <property name="forceBigDecimals" value="false"/>
    <!-- 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"/>
    <!-- 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"
    <!-- 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"/>

    <!-- 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"/>

II. Configure pom.xml

<!-- stay<project>Add the following configuration under the label -->
                <!-- This path is what we create generatorConfig.xml The full path of the file. Don't mix it up -->
	   <id>Generate MyBatis Artifacts</id>

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:

Topics: Mybatis xml Database MySQL