Automatically generate code with Mybatis-generator

Source: Internet
Author: User

There are three ways to use Mybatis-generator: command line, Eclipse plugin, maven plugin. Personally, Maven plugin is the most convenient, can be used in Eclipse/intellij idea and other Ides can be common.

The following is from the official website: (But the official website Www.mybatis.org recently, seems to have hung up)

First, add plugin in Pom.xml

 1 <plugin> 2 <groupId>org.mybatis.generator</groupId> 3 <artifactid>mybatis-generator-mav En-plugin</artifactid> 4 <version>1.3.2</version> 5 <configuration> 6 <configur Ationfile>src/main/resources/mybatis-generator/generatorconfig.xml</configurationfile> 7 <verbose >true</verbose> 8 <overwrite>true</overwrite> 9 </configuration>10 <executio Ns>11 <execution>12 <id>generate MyBatis artifacts</id>13 &LT;GOALS&G t;14 <goal>generate</goal>15 </goals>16 </execution>17 &lt ;/executions>18 <dependencies>19 <dependency>20 <groupid>org.mybatis.generato R</groupid>21 <artifactid>mybatis-generator-core</artifactid>22 <version>1       .3.2</version>23  </dependency>24 </dependencies>25 </plugin> 

Where Generatorconfig.xml's position, we adjust according to the actual situation

Second, generatorconfig.xml configuration file

 1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <! DOCTYPE generatorconfiguration 3 Public "-//mybatis.org//dtd mybatis Generator Configuration 1.0//en" 4 "H Ttp://mybatis.org/dtd/mybatis-generator-config_1_0.dtd "> 5 6 <generatorConfiguration> 7 <classpathentry 8 location= "C:/oracle/middleware/wlserver_10.3/server/lib/ojdbc6.jar"/> 9 <context id= "my" TargetRun              Time= "MyBatis3" >10 <commentgenerator>11 <property name= "Suppressdate" value= "false"/>12 <property name= "Suppressallcomments" value= "true"/>13 </commentgenerator>14 T;jdbcconnection driverclass= "Oracle.jdbc.driver.OracleDriver" connectionurl= "jdbc:oracle:thin:@ 172.20.16.***:1521:cargo "userid=" * * * "password=" * * * */>18 <javamodelgenerator t Argetpackage= "ctas.test.entity" targetproject= "d:/yangjm/code/Ctas/javaee/ctas2ccsp/src/main/java ">21 <property name=" Enablesubpackages "value=" true "/>22 <property name= "Trimstrings" value= "true"/>23 </javamodelgenerator>24 R targetpackage= "Ctas.test.entity.xml" targetproject= "d:/yangjm/code/ctas/javaee/ctas2ccsp/src/ Main/java ">27 <property name=" Enablesubpackages "value=" true "/>28 </sqlmapgenerator>29 <javaclientgenerator targetpackage= "Ctas.test.mapper" targetproject= "D:/yangj M/code/ctas/javaee/ctas2ccsp/src/main/java "type=" Xmlmapper ">32 <property name=" EnableSubPackages "value = "true"/>33 </javaclientgenerator>34 <!--<table tablename= "T_fee_agtbill" Domainobjectna Me= "Feeagentbill" enablecountbyexample= "false" enableupdatebyexample= "false" Notoginseng Enabledelet Ebyexample= "false" EnablEselectbyexample= "false" Selectbyexamplequeryid= "false"/>-->39 <table tablename= "CTAS                _fee_base "Domainobjectname=" Feebase "enablecountbyexample=" false "enableupdatebyexample=" false "42              Enabledeletebyexample= "false" enableselectbyexample= "false" Selectbyexamplequeryid= "false" >44 <!--<columnrenamingrule searchstring= "^d_" replacestring= ""/>--&gt ; </table>47 </context>49 </generatorConfiguration>

Several points:
A) due to the need to connect DB during the build process, the 3rd line specifies the location of the drive jar package

b) 15-17 Behavior connection string

c) 19-33 line specifies the location of the entity entity class, the MyBatis map XML file, the Mapper interface.

d) 40-46 behavior specifically to generate the table, if there are multiple tables, copy this paragraph, change the table name to

Third, the mode of use

MVN mybatis-generator:generate

If it is in IntelliJ environment, direct mouse click can

Finally, the directory structure diagram is given:

Finally give some tips:

A) When building a table, the field names suggest "_" to separate multiple words, such as: Awb_no, rec_id ..., the generated entity, the property name will become a beautiful hump named, namely: Awbno, RecId

b) in Oracle, numeric fields, if specified precision, such as number (12,2), the default generated entity property is the BigDecimal type, if you do not specify the precision, such as: Numbers (9), refers to the default generation of a long type

c) nvarchar/nvarchar2,mybatis-generator in Oracle will be recognized as object type, it is recommended not to use NVARCHAR2, instead of VARCHAR2

Automatically generate code with Mybatis-generator

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.