English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

In-depth Explanation of Using MyBatis Generator to Automatically Generate Code

These days, the code auto-generation feature of MyBatis is needed. Since MyBatis is a semi-automatic ORM framework, the main task is to configure the Mapping mapping file. However, it is easy to make mistakes when writing the mapping file manually, so it is advisable to use the MyBatis generator to automatically generate entity classes, DAO interfaces, and Mapping mapping files. This can save a lot of effort, and you can simply copy the generated code into the project.

1. Directory description

There are many ways to use automatic generation, you can install plugins in eclipse, but the way to be introduced below is very simple, the simplest, no need to install plugins, just download several jar packages, put them in a directory below, as shown in the figure:

src folder: The location of the generated resource files

generationRun.bat: Automatic generation script

generatorConfig - backup.xml: Backup

generatorConfig.xml: Configuration file

mybatis-3.2.6.jar: MyBatis framework jar package

mybatis-generator-core-1.3.2.jar: MyBatis generator jar package

mysql-connector-java-5.1.30.jar: Database driver program jar package

2. Configuration file

<?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 driver (modify to your own version)-->  
  <classPathEntry location="mysql-connector-java-5.1.30.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 connection URL, username, password -->
    <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1:3306/dbname?characterEncoding=utf8" userId="username" password="password"> 
    </jdbcConnection>
    <javaTypeResolver> 
      <property name="forceBigDecimals" value="false"/> 
    </javaTypeResolver> 
    <!-- The package name and location for generating models-->
    <javaModelGenerator targetPackage="POJO" targetProject="src"> 
      <property name="enableSubPackages" value="true"/> 
      <property name="trimStrings" value="true"/> 
    </javaModelGenerator> 
    <!-- The package name and location for generating mapping files--> 
    <sqlMapGenerator targetPackage="Mapping" targetProject="src"> 
      <property name="enableSubPackages" value="true"/> 
    </sqlMapGenerator> 
    <!-- The package name and location for generating DAO-->
    <javaClientGenerator type="XMLMAPPER" targetPackage="IDAO" targetProject="src"> 
      <property name="enableSubPackages" value="true"/> 
    </javaClientGenerator> 
    <!-- The table to be generated: tableName is the table name or view name in the database, domainObjectName is the entity class name-->
    <table tableName="tableName" domainObjectName="ObjectName" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
    </table>  <!-- **-->
  </context> 
</generatorConfiguration> 

3Execute the script

Open the console and enter the directory (my: E:\bl-java\maven-server\MyBatis-generation-code), execute the script:
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

Or you can create a bat file to double-click and execute the script.

@echo off
:: Note: Copy this script to a new text file, and add the file extension .bat.
java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite
echo Find the corresponding folder under the src directory, each table will correspond to three files (entity class, interface, configuration file)
PAUSE

4. One exception: XML Parser Errors occurred

XML Parser Errors occurred:
XML Parser Error on line 42: The content of the element type "context" is incomplete, it must match "(property*,plugin*,commentGenerator#63;,jdbcConnection,javaTypeResolver#63;,javaModelGenerator,sqlMapGenerator#63;,javaClientGenerator#63;,table+)".

Reason: The position of the generatorConfig.xml configuration file is incorrect

Solution: The tag position is at the last one of the child tag (an error will also be reported if there is no table tag)

That's all for this article. Hope it helps with your studies and that you all support the Yell Tutorial.

Declaration: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume relevant legal liability. If you find content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like