Automatically generate Dao.Model.Mapping related files using Mybatis-gererator

Source: Internet
Author: User

One. Use Mybatis-gererator to automatically generate Dao.Model.Mapping-related files in MyEclipse using MAVEN projects

1. About Mybatis-generator download can go to this address: https://github.com/mybatis/generator/releases

2. Find a download profile that matches maven and configure the matching files to be in the Pom.xml file under the MAVEN project

  

3. Copy the following two in the Generatorconfig.xml file, to the MAVEN project created in MyEclipse, modify the generatorconfig.xml part of the path, modify the following

<?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> <context id=" Context1 " > <!--generate a root configuration file (JEBC) connection four features--<jdbcconnection connectionurl= "Jdbc:oracle:thin: @localhost: 1521:orcl" Dr iverclass= "Oracle.jdbc.OracleDriver" password= "Tiger" userid= "Scott"/> <!--generate the MyBatis code of the EMP table, generate an EMP entity class, use the table name as the class name, and the name of the original property name Targetpackage="Specify the generated package name": Indicates the package name to be generated Targetproject= "in which project to generate": Indicates which project to generate under src/main/Java--<javamodelgenerator targetpackage= "cn.et.mybatis.lesson04.genernator.entity" targetproject= "D:\javaEE1\ Mybatis\src "/> <!--code to generate an interface map Targetpackage="Specify generated package name": Indicates the build of the specified package name Targetproject= "in which project to generate": Indicates which project to generate under src/main/Java--<sqlmapgenerator targetpackage= "Cn.et.mybatis.lesson04.genernator.dao" targetproject= "D:\javaEE1\ Mybatis\src "/> <!--A mapping file that represents the Java client code generator primarily generates XML or annotation type= "Xmlmapper": Represents the interface that generates the Java base map, and these interfaces will depend on the generated XML file type= "Annotatedmapper": Represents the interface that generates the Java base map, and these interfaces will be dependent on annotations Targetpackage="Specify generated package name": Indicates the build of the specified package name Targetproject= "in which project to generate": Indicates which project to generate under src/main/Java--<javaclientgenerator targetpackage= "Cn.et.mybatis.lesson04.genernator.dao" targetproject= "D:\javaEE1\ Mybatis\src "type=" Xmlmapper "/> <!--represents the table schema to be generated= "scheme name": The scheme, each user has a unique scheme, in fact, the scheme name equals the user name TableName= "Table Name": Indicates which table is to be generated--<table Schema= "Scott" Tablename= "EMP" enablecountbyexample= "false"Enableselectbyexample= "false" enableupdatebyexample= "false"Enabledeletebyexample= "false" > </table> </context></generatorConfiguration>

4. Because the above files cannot be run directly in MyEclipse, you need to configure them under the Plugins tab in Pom.xml, add the application, and configure the following:

<!--Add Application plug-in <plugin> <groupId>org.mybatis.generator</groupId> &lt ;artifactid>mybatis-generator-maven-plugin</artifactid> <!--This version must be the same as the Mybatis-generator version added above--&gt          ; <version>1.3.3</version> <!--represents the directory of the build file--<configuration> <con Figurationfile>generatorconfig.xml</configurationfile> </configuration> <!--represents the ja of JDBC R Package Path-<dependencies> <dependency> <groupid>oracle</gr Oupid> <artifactId>oracle</artifactId> <version>1.2.17</ Version> <scope>system</scope> <systempath>c:\app\administrator \product\11.2.0\dbhome_1\jdbc\lib\ojdbc6.jar</systempath> </dependency> </depende    Ncies>    </plugin> 

5. Run right-click on the project-"Run as--" Maven build--> Enter the page, enter mybatis-generator-generate--in goals "Run runs successfully

Two. Use Mybatis-gererator to automatically generate Dao.Model.Mapping-related files using the MAVEN project in eclipse

1. Use the jar package to generate the configuration file automatically, https://github.com/mybatis/generator/releases----"Download" Mybatis-generator-eclipse-site-1.3.3.201606241937.zip "jar package.

2. Add the downloaded jar package Help-->install New software--> into the interface, select Add, Name box enter the name "Custom", the Location box enter the full path of the above downloaded jar package, click OK---- > Cancel the sites during install to find required Stftword option under the Install interface, select the MyBatis gererator option, and click the next option Last page Select I Accept the terms of the License Agreement option---> Pop-up box select Yes and click Done.

3. Create a simple Maven Properject project right-click-->new-->other--> search maven--> Select Maver properject-->next--> Select Create. The first item-->next--> creates the Maver project.

4. After creating the project, enter the project, select Edit Pom.xml file, add download path profile and download URL: maven.aliyun.com----> Search MyBatis---> enter the selection mybatis--> Select the stable version of the profile---> copy the relevant code for the configuration--in the <dependencies> tag in the paste Pom.xml file

5. Select the MAVEN project you created, right-click new-->other--> to open the mybatis--> option mybatis gererator Configuration file--> Select Next---> The finish--> will generate the Generatorconfig.xml file under the SRC directory.

6. Select Edit Generatorconfig.xml file code into the play:

<?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> <context id=" Context1 " > <!--generate a root configuration file (JEBC) connection four features--<jdbcconnection connectionurl= "Jdbc:oracle:thin: @localhost: 1521:orcl" Dr iverclass= "Oracle.jdbc.OracleDriver" password= "Tiger" userid= "Scott"/> <!--generate the MyBatis code of the EMP table, generate an EMP entity class, use the table name as the class name, and the name of the original property name Targetpackage="Specify the generated package name": Indicates the package name to be generated Targetproject= "in which project to generate": Indicates which project to generate under src/main/Java--<javamodelgenerator targetpackage= "cn.et.mybatis.entity" targetproject= "Mybatis/src/main/java"/> &L t;! --code to generate an interface map Targetpackage="Specify generated package name": Indicates the build of the specified package name Targetproject= "in which project to generate": Indicates which project to generate under src/main/Java--<sqlmapgenerator targetpackage= "Cn.et.mybatis.dao" targetproject= "Mybatis/src/main/java"/> <!--A mapping file that represents the Java client code generator primarily generates XML or annotation type= "Xmlmapper": Represents the interface that generates the Java base map, and these interfaces will depend on the generated XML file type= "Annotatedmapper": Represents the interface that generates the Java base map, and these interfaces will be dependent on annotations Targetpackage="Specify generated package name": Indicates the build of the specified package name Targetproject= "in which project to generate": Indicates which project to generate under src/main/Java--<javaclientgenerator targetpackage= "???" Targetproject= "???" Type= "Xmlmapper"/> <!--represents the table schema to be generated= "scheme name": The scheme, each user has a unique scheme, in fact, the scheme name equals the user name TableName= "Table Name": Indicates which table is to be generated--<table Schema= "Scott" tablename= "emp" > </table> </context></generatorConfiguration>

6.2 Generating an annotation file

Copy the above generatorconfig.xml file renaming Generatorconfig_anno.xml file and make the following modifications

<?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> <context id=" Context1 " > <!--generate a root configuration file (JEBC) connection four features--<jdbcconnection connectionurl= "Jdbc:oracle:thin: @localhost: 1521:orcl" Dr iverclass= "Oracle.jdbc.OracleDriver" password= "Tiger" userid= "Scott"/> <!--generate the MyBatis code of the EMP table, generate an EMP entity class, use the table name as the class name, and the name of the original property name Targetpackage="Specify the generated package name": Indicates the package name to be generated Targetproject= "in which project to generate": Indicates which project to generate under src/main/Java--<javamodelgenerator targetpackage= "cn.et.mybatis1.entity" targetproject= "Mybatis/src/main/java"/> & lt;! --code to generate an interface map Targetpackage="Specify generated package name": Indicates the build of the specified package name Targetproject= "in which project to generate": Indicates which project to generate under src/main/Java--<sqlmapgenerator targetpackage= "Cn.et.mybatis1.dao" targetproject= "Mybatis/src/main/java"/> <!--A mapping file that represents the Java client code generator primarily generates XML or annotation type= "Xmlmapper": Represents the interface that generates the Java base map, and these interfaces will depend on the generated XML file type= "Annotatedmapper": Represents the interface that generates the Java base map, and these interfaces will be dependent on annotations Targetpackage="Specify generated package name": Indicates the build of the specified package name Targetproject= "in which project to generate": Indicates which project to generate under src/main/Java--<javaclientgenerator targetpackage= "Cn.et.mybatis1.dao" targetproject= "Mybatis/src/main/java" type= " Xmlmapper "/> <!--represents the table schema to be generated= "scheme name": The scheme, each user has a unique scheme, in fact, the scheme name equals the user name TableName= "Table Name": Indicates which table is to be generated--<table Schema= "Scott" Tablename= "EMP" enablecountbyexample= "false"Enableselectbyexample= "false" enableupdatebyexample= "false"Enabledeletebyexample= "false" > </table> </context></generatorConfiguration>

7. Add the JDBC Jar package under the project

Lookup path for the JDBC Jar package: C:\app\Administrator\product\11.2.0\dbhome_1\jdbc\lib

Select to copy the Ojdbc6.jar package

Create a Lib directory under Project, paste, add to build path

8. Select Generatorconfig.xml---Right click---Select Generate MyBatis .... Run

9. Build Complete

10. Optional processing, if the generated XML file too much content, you can customize the generated content, you need to make the following changes in the table label of the above 6.generatorconfig.xml

<table schema= "Scott" Tablename= "EMP" enablecountbyexample= "false"        enableselectbyexample= "false" Enableupdatebyexample= "false"        enabledeletebyexample= "false" >    </table>

Delete the above 8 generated code

Repeat the 8th option above and run again

Automatically generate Dao.Model.Mapping related files using Mybatis-gererator

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.