Eclipse uses the Maven generator plugin to automatically generate the required code for MyBatis

Source: Internet
Author: User
Tags generator

One, the Maven pom.xml file introduces the plugin plugin

<plugins>

<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--mybatis-generator Profile storage Address--
<configurationFile>src/test/resources/generator/mybatis-generator.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
</dependencies>
</plugin>

<plugins>

Second, create the Mybatis-generator.xml configuration file

Note: The Mybatis-generator.xml file creation location matches the configuration file specified in the plugin plugin in the Pom.xml file

Create a Mybatis-generator.xml 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-driven
<context id= "Generatortables" targetruntime= "MyBatis3" >
<property name= "Autodelimitkeywords" value= "false"/>
<property name= "javafileencoding" value= "UTF-8"/>
<property name= "Javaformatter"
Value= "Org.mybatis.generator.api.dom.DefaultJavaFormatter"/>
<property name= "Xmlformatter"
Value= "Org.mybatis.generator.api.dom.DefaultXmlFormatter"/>
<plugin type= "Org.mybatis.generator.plugins.MapperConfigPlugin" >
<property name= "FileName" value= "Mybatis-config.xml"/>
<property name= "Targetpackage" value= "/"/>
<property name= "Targetproject" value= "Src/main/resources"/>
</plugin>
<!--<plugin type= "Org.mybatis.generator.plugins.RenameExampleClassPlugin" >
<property name= "searchstring" value= "Example"/> <property name= "replacestring"
Value= "Criteria"/> </plugin>--
<plugin type= "Org.mybatis.generator.plugins.SerializablePlugin"/>
<commentGenerator>
<property name= "Suppressallcomments" value= "true"/>
<property name= "Suppressdate" value= "true"/>
</commentGenerator>

<!--database connection address--

<jdbcconnection driverclass= "Com.mysql.jdbc.Driver"
Connectionurl= "Jdbc:mysql://192.168.0.1:3306/test" userid= "root"
password= "Root" >
</jdbcConnection>
<javaTypeResolver>
<property name= "Forcebigdecimals" value= "false"/>
</javaTypeResolver>
<!--specify where the model is automatically generated--
<javamodelgenerator targetpackage= "Com.test.dal.dao.mybatis.model"
targetproject= "Src/main/java" >
<property name= "Enablesubpackages" value= "true"/>
<property name= "Trimstrings" value= "true"/>
</javaModelGenerator>
<!--specify where to automatically generate mapper--
<sqlmapgenerator targetpackage= "Com.test.dal.dao.mybatis.mapper"
targetproject= "Src/main/java" >
<property name= "Enablesubpackages" value= "true"/>
</sqlMapGenerator>
<!--specify where to automatically generate the Mapper interface--
<javaclientgenerator type= "Xmlmapper"
Targetpackage= "Com.test.dal.dao.mybatis" targetproject= "Src/main/java" >
<property name= "Enablesubpackages" value= "true"/>
</javaClientGenerator>
<!--
<table tablename= "Student" domainobjectname= "student"
Enablecountbyexample= "false" enableupdatebyexample= "false"
Enabledeletebyexample= "false" enableselectbyexample= "false"
Selectbyexamplequeryid= "false" >
</table>
-
<!--
<table tablename= "Teacher" domainobjectname= "teacher"
Enablecountbyexample= "false" enableupdatebyexample= "false"
Enabledeletebyexample= "false" enableselectbyexample= "false"
Selectbyexamplequeryid= "false" >
<property name= "Useactualcolumnnames" value= "true"/>
<property name= "mergeable" value= "true"/>
</table>
-

</context>
</generatorConfiguration>

Note: Please see the mybatis-generator open source document or Google search for specific configuration tab content


third, generate code

(1) If it is in Eclipse, select the project or the Pom.xml file,

Click the right mouse button first select Run As-->maven build...-->

In the Goals box, type: mybatis-generator:generate

(2) If you enter the MAVEN command at the command line

Note: This command must be running under the current project directory:

MVN mybatis-generator:generate

Note: If using generator has been generated once code, then the next time you modify the database table structure, generate the corresponding table again, be sure to delete the original ***mapper.xml file and other corresponding files, otherwise in * * * Multiple code will be generated in the Mapper.xml file and will be an error during operation.

After the code is generated, the merit is farewell.







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.