Mybatis - Generator tutorial


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.


1. Add plug-in dependency in pom file:

<!-- mybatis generator Auto generate code plug-in -->

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 Generator Configuration 1.0//EN"
    <!-- 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">
            <property name="suppressDate" value="true"/>
            <!-- Whether to remove automatically generated comments true: Yes: false:no -->
            <property name="suppressAllComments" value="true"/>
        <!--Database links URL,User name, password -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://" userId="root" password="111111">
            <property name="forceBigDecimals" value="false"/>
        <!-- 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"/>
        <!-- Package name and location of the build mapping file-->
        <sqlMapGenerator targetPackage="com.ideasfly.dao.mapper" targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        <!-- generate DAO Package name and location for-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.ideasfly.dao.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="dicom_file" domainObjectName="DicomFile" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/>

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