Mybatis generator generating Mybatis DAO interface layer *mapper.xml and corresponding entity classes

Source: Internet
Author: User

http://www.cnblogs.com/qixiaoyizhan/p/7597315.html "Preface"

Using Mybatis-generator to automatically generate DAO, Model, mapping related files, the role of Mybatis-generator is to act as a code generator, and using code generators not only simplifies our workload, Productivity is improved, and the probability of code errors can be greatly reduced. In the SSM (Spring,springmvc,mybatis) Three-tier architecture, we have to use MyBatis for mapper files, where we use mybatis-generator to generate our own dependent files.

"Get the Way"

Mybatis-generator on the official website has a ZIP package can be downloaded, but is a foreign server, the network situation is not good users can not smooth download, here I have downloaded a good zip package and configuration examples uploaded in my file, the pro can be directly downloaded here, the link is as follows:

Https://files.cnblogs.com/files/qixiaoyizhan/mybatis-generator-core-1.3.5.zip

Download the zip package to unzip to any location, and then we can see the file directory:

  

The jar package is the generator code, Generatorconfig.xml is the build configuration file, SRC is the build directory.

"Implementation Conditions"

Because the generator is a jar package, if you use this tool, you must have a JDK in your system and configure the environment variables.

If the JDK is installed we can open the CMD console in any location to write command java-version to view:

  

This shows the version number of the JDK, which indicates that the environment variables are already configured.

Configuration of the "implementation process" 1.generatorconfig.xml file

First we make the configuration of the Generatorconfig.xml to build the file, we open the Generatorconfig.xml file, set the build parameters.

 1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <! DOCTYPE generatorconfiguration 3 Public "-//mybatis.org//dtd mybatis Generator Configuration 1.0//en" 4 "Http://mybati S.org/dtd/mybatis-generator-config_1_0.dtd "> 5 <generatorConfiguration> 6 <!--database Driver--7 &LT;CLASSP Athentry location= "Mysql-connector-java-5.1.21-bin.jar"/> 8 <context id= "Db2tables" TargetRuntime= "MyBatis 3 "> 9 <commentgenerator>10 <property name=" Suppressdate "value=" true "/>11 & Lt;property name= "Suppressallcomments" value= "true"/>12 </commentgenerator>13 <!--database link Address account password- ->14 <jdbcconnection driverclass= "Com.mysql.jdbc.Driver" connectionurl= "jdbc:mysql://localhost:3306/db_test ? usessl=false "userid=" Db_test "password=" 123456 ">15 </jdbcconnection>16 <javaTypeResolver> <property name= "Forcebigdecimals" value= "false"/>18 </javatyPeresolver>19 <!--generate model class storage location-->20 <javamodelgenerator targetpackage= "Data.entities" TARGETPR oject= "src" >21 <property name= "Enablesubpackages" value= "true"/>22 <property name= "Tri Mstrings "value=" true "/>23 </javamodelgenerator>24 <!--generate Map file storage location-->25 <sqlmapge Nerator targetpackage= "mapper" targetproject= "src" >26 <property name= "Enablesubpackages" value= "true"/&G  T;27 </sqlmapgenerator>28 <!--Generate DAO class storage location-->29 <javaclientgenerator type= "Xmlmapper"         Targetpackage= "Dao" targetproject= "src" >30 <property name= "Enablesubpackages" value= "true"/>31 </javaclientgenerator>32 <!--generate corresponding table and class name-->33 <table tablename= "student" domainobjectname= " Student "enablecountbyexample=" false "enableupdatebyexample=" false "enabledeletebyexample=" false " Enableselectbyexample= "false" SelectbyexamplequeRyid= "false" ></table>34 </context>35 </generatorConfiguration> 

The file setting parameters are explained in the inside, and no longer repeat them here.

2. Calling Mybatis-generator-core-1.3.5.jar for code generation

Open the cmd command line in this folder (you can press and hold shift+ right mouse button to open the command line here)

  

Then enter the command in the window that opens:

Java-jar Mybatis-generator-core-1.3.2.jar-configfile Generatorconfig.xml-overwrite

After the code executes successfully, we can see that the code has been generated complete:

Open our SRC directory to see the generated code.

"may be experiencing problems"

The JDK environment variable is not well-configured, causing the code to execute unsuccessfully, and follow the online steps to configure the environment variable.

Database connection error, database string, account or password error.

"System Outlook"

The generator needs to be manually opened to configure the file, but also need to do the command line execution, not conducive to the operation, the subsequent will be changed to use the interface mode of operation.

Mybatis generator generating Mybatis DAO interface layer *mapper.xml and corresponding entity classes

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.