The usage of MybatisGenerator, including the installation of the eclipse plug-in, the official website address of the tool download and related detailed tutorials

Mybatis Generator detailed usage

First, install the eclipse plugin

Find in Help--EclipserMarketplace: Mybatis Generator 1.3.6 installation


2. Download the Mybatis Generator tool on the official website

Official website address: https://github.com/mybatis/generator/releases , go in and download the circled part as shown below:

After downloading, unzip it, and copy the two files named features and plugins under the unzipped files to the root directory of the installation eclipse

 

3. Create a new project

New--other--Find as follows

Click next , select the project name where you want to generate the file

Click finish to close. The file generatorConfig.xml will be generated in the springmvcfirst1208 project.

 

Note: In the springmvcfirst1208 project, there must be a database-related jar package and a package mapper/pojo to be generated, because it will be configured in the configuration file in a while.



 

Fourth, configure generatorConfig.xml

<?xml version="1.0"encoding="UTF-8"?>

<!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>

       <!--targetRuntime: This property is used to specify the runtime environment of the generated code, MyBatis3 is the default value -->

       <!--id="context1" The id here is also the default value of context1 -->

       <contextid="context1" targetRuntime="MyBatis3">

              <commentGenerator> 

            <!-- Whether to remove automatically generated comments true : yes: false: no --> 

            <property name="suppressAllComments"value="false" /> 

       </commentGenerator> 

       

       <!-- connectionURL : database address; driverClass : driver class -->

              <jdbcConnectionconnectionURL="jdbc:mysql://localhost:3306/mybatis"

                     driverClass="com.mysql.jdbc.Driver"password="123456" userId="root" />

             

              <!--targetPackage is the target package name, targetProject is the location of the package -->

              <javaModelGeneratortargetPackage="com.itcast.ssm.po"

                     targetProject="mybatisNX/src"/>

                    

              <!--targetPackage places the generated SQL mapping file, targetProject specifies the target project for generating SQL mapping -->

              <sqlMapGeneratortargetPackage="com.itcast.ssm.mapper"

                     targetProject="mybatisNX/src"/>

             

              <!--targetPackage is the target package name, targetProject is the location of the package; type: The object generated by XMLMAPPER is the Java interface of the MyBatis3.x mapper infrastructure -->

              <javaClientGeneratortargetPackage="com.itcast.ssm.mapper"

                     targetProject="mybatisNX/src"type="XMLMAPPER" />

             

              <!-- Table name in the database-->

              <tabletableName="items"></table>

              <tabletableName="orders"></table>

              <tabletableName="orderdetail"></table>

              <tabletableName="user"></table>

 

              <!--<table schema="???" tableName="???"><columnOverride column="???"

                     property="???"/> </table> -->

       </context>

</generatorConfiguration>

 

 

 

 

 

 

The code above may look a bit messy, as shown in the screenshot below:


 

Note: Before configuring generatorConfig.xml , you must build two packages under your project, and then pay attention to whether your package name is written correctly in the above configuration of traditional Chinese medicine; also build the database and table.

 

Main code enlargement:



5. Automatically generate files such as mapper.java and mapper.xml

 

 

After that, you can see the generated files under the package

 

 

There may be omissions here, please bear with me.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325811384&siteId=291194637