This article transferred from: http://www.cnblogs.com/yjmyzz/p/4210554.html
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
<plugin> <groupId>Org.mybatis.generator</groupId> <Artifactid>Mybatis-generator-maven-plugin</Artifactid> <version>1.3.2</version> <Configuration> <ConfigurationFile>Src/main/resources/mybatis-generator/generatorconfig.xml</ConfigurationFile> <verbose>True</verbose> <Overwrite>True</Overwrite> </Configuration> <executions> <Execution> <ID>Generate MyBatis Artifacts</ID> <Goals> <goal>Generate</goal> </Goals> </Execution> </executions> <Dependencies> <Dependency> <groupId>Org.mybatis.generator</groupId> <Artifactid>Mybatis-generator-core</Artifactid> <version>1.3.2</version> </Dependency> </Dependencies></plugin>
Where Generatorconfig.xml's position, we adjust according to the actual situation
Second, generatorconfig.xml configuration file
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE generatorconfiguration Public "-//mybatis.org//dtd mybatis Generator Configuration 1.0//en" "Htt P://mybatis.org/dtd/mybatis-generator-config_1_0.dtd "><generatorconfiguration> <Classpathentry Location= "C:/oracle/middleware/wlserver_10.3/server/lib/ojdbc6.jar"/> <ContextID= "my"Targetruntime= "MyBatis3"> <Commentgenerator> < Propertyname= "Suppressdate"value= "false"/> < Propertyname= "Suppressallcomments"value= "true"/> </Commentgenerator> <jdbcconnectionDriverclass= "Oracle.jdbc.driver.OracleDriver"Connectionurl= "Jdbc:oracle:thin:@172.20.16.***:1521:cargo"userId="***"Password="***"/> <JavamodelgeneratorTargetpackage= "Ctas.test.entity"Targetproject= "D:/yangjm/code/ctas/javaee/ctas2ccsp/src/main/java"> < Propertyname= "Enablesubpackages"value= "true"/> < Propertyname= "Trimstrings"value= "true"/> </Javamodelgenerator> <SqlmapgeneratorTargetpackage= "Ctas.test.entity.xml"Targetproject= "D:/yangjm/code/ctas/javaee/ctas2ccsp/src/main/java"> < Propertyname= "Enablesubpackages"value= "true"/> </Sqlmapgenerator> <JavaclientgeneratorTargetpackage= "Ctas.test.mapper"Targetproject= "D:/yangjm/code/ctas/javaee/ctas2ccsp/src/main/java"type= "Xmlmapper"> < Propertyname= "Enablesubpackages"value= "true"/> </Javaclientgenerator> <!--<table tablename= "T_fee_agtbill" domainobjectname= "Feeagentbill" enablecountbyexample= "false" enable Updatebyexample= "false" enabledeletebyexample= "false" enableselectbyexample= "false" Selectbyex Amplequeryid= "false"/> - <TableTableName= "Ctas_fee_base"Domainobjectname= "Feebase"Enablecountbyexample= "false"Enableupdatebyexample= "false"Enabledeletebyexample= "false"Enableselectbyexample= "false"Selectbyexamplequeryid= "false"> <!--<columnrenamingrule searchstring= "^d_" replacestring= ""/> - </Table> </Context></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
"Go" Automatically generate code with Mybatis-generator