SSM Framework - Automatic Code Creation Using MyBatis Generator

Keywords: Mybatis xml Database MySQL

MyBatis is a semi-automatic ORM framework, so the main task is to configure Mapping mapping files. But because handwritten mapping files are easy to make mistakes, MyBatis generators can be used to automatically generate entity classes, DAO interfaces and Mapping mapping files. This saves a lot of effort, copy ing the generated code into the project.

There are many ways to install plug-ins in eclipse using auto-generation, but the way I'm going to introduce below is very easy and simple. It doesn't need to install plug-ins, just a few jar packages and put them under a directory.


The files and jar packages needed to generate the code:


(Download address of the above file: http://download.csdn.net/detail/u012909091/7206091)

There are jar packages for the mybatis framework. data base Driver jar package and MyBatis generator jar package. GeneorConfig. XML is the file that we need to configure. The configuration is as follows:


  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <!DOCTYPE generatorConfiguration    
  3.   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"    
  4.   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">    
  5. <generatorConfiguration>    
  6. <!-- Database Driver-->    
  7.     <classPathEntry  location="mysql-connector-java-5.1.25-bin.jar"/>    
  8.     <context id="DB2Tables"  targetRuntime="MyBatis3">    
  9.         <commentGenerator>    
  10.             <property name="suppressDate" value="true"/>    
  11.             <!-- Whether to remove automatically generated comments true: yes :  false:no -->    
  12.             <property name="suppressAllComments" value="true"/>    
  13.         </commentGenerator>    
  14.         <!--Database Links URL,User name, password -->    
  15.         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://125.221.1.1/db_124" userId="dem" password="dem">    
  16.         </jdbcConnection>    
  17.         <javaTypeResolver>    
  18.             <property name="forceBigDecimals" value="false"/>    
  19.         </javaTypeResolver>    
  20.         <!-- Package name and location of the generated model-->    
  21.         <javaModelGenerator targetPackage="test.domain" targetProject="src">    
  22.             <property name="enableSubPackages" value="true"/>    
  23.             <property name="trimStrings" value="true"/>    
  24.         </javaModelGenerator>    
  25.         <!-- Generate the package name and location of the mapping file-->    
  26.         <sqlMapGenerator targetPackage="test.mapping" targetProject="src">    
  27.             <property name="enableSubPackages" value="true"/>    
  28.         </sqlMapGenerator>    
  29.         <!-- generate DAO The package name and location of-->    
  30.         <javaClientGenerator type="XMLMAPPER" targetPackage="test.IDao" targetProject="src">    
  31.             <property name="enableSubPackages" value="true"/>    
  32.         </javaClientGenerator>    
  33.         <!-- Tables to be generated tableName Is the table name or view name in the database domainObjectName Is the entity class name-->    
  34.         <table tableName="user_info_t" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>  
  35.     </context>    
  36. </generatorConfiguration>    


When all this is done, just open the console, enter the lib directory, and execute the script:

Java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

That's all.


After generation, you can find the corresponding folder in the src directory, and each table corresponds to three files (entity class, interface, configuration file).

Posted by zrocker on Tue, 26 Mar 2019 03:27:28 -0700