Mybatis - Generator tutorial

abstract

Mybatis generator can help us automatically generate a lot of structured code, such as Entity, Mapper interface and Xml file corresponding to each table, which can save a lot of tedious work.

preparation

1. Add plug-in dependency in pom file:

<!-- mybatis generator Auto generate code plug-in -->
<plugin>
    <groupId>org.mybatis.generator</groupId>
    <artifactId>mybatis-generator-maven-plugin</artifactId>
    <version>1.3.2</version>
    <configuration>
        <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
        <overwrite>true</overwrite>
        <verbose>true</verbose>
    </configuration>
</plugin>

2. Put the mybatis generator configuration file into the resource/generator directory. The file name is generatorConfig.xml. The content is 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="D:\maven-repository\mysql\mysql-connector-java\8.0.11\mysql-connector-java-8.0.11.jar"/>
    <context id="DB2Tables"  targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- Whether to remove automatically generated comments true: Yes: false:no -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--Database links URL,User name, password -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/lan?serverTimezone=UTC" userId="root" password="111111">
        </jdbcConnection>
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- Package name and location of the build model-->
        <javaModelGenerator targetPackage="com.ideasfly.dao.entity" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- Package name and location of the build mapping file-->
        <sqlMapGenerator targetPackage="com.ideasfly.dao.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- generate DAO Package name and location for-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.ideasfly.dao.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="dicom_file" domainObjectName="DicomFile" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>
    </context>
</generatorConfiguration>

The parts to be modified are:
a. The location of the package file of the database driver, namely mysql-connector-java-x.jar package, can be found in the maven warehouse or downloaded separately.
b. The url of the database, user name and password, etc.
c. Package name and location of the build model.
d. The package name and location of the build mapping file.
e. The package name and location where the DAO is generated.
f. Add the table to build.

3. Run the plug-in to generate code. The plug-in is located under Plugins in Maven project, as shown in the figure, and then run.

 

4. View entity and mapper, and it's a success

Tags: Java Mybatis Database MySQL

Posted on Mon, 11 May 2020 09:40:55 -0700 by robinjohn