mybatis-generator有三種用法:命令行、eclipse插件、maven插件。我的以爲maven插件最方便,能夠在eclipse/intellij idea等ide上能夠通用。java
下面是從官網上的截圖:(不過官網www.mybatis.org 最近一段時間,好象已經掛了)git
1、在pom.xml中添加plugingithub
1 <plugin> 2 <groupId>org.mybatis.generator</groupId> 3 <artifactId>mybatis-generator-maven-plugin</artifactId> 4 <version>1.3.2</version> 5 <configuration> 6 <configurationFile>src/main/resources/mybatis-generator/generatorConfig.xml</configurationFile> 7 <verbose>true</verbose> 8 <overwrite>true</overwrite> 9 </configuration> 10 <executions> 11 <execution> 12 <id>Generate MyBatis Artifacts</id> 13 <goals> 14 <goal>generate</goal> 15 </goals> 16 </execution> 17 </executions> 18 <dependencies> 19 <dependency> 20 <groupId>org.mybatis.generator</groupId> 21 <artifactId>mybatis-generator-core</artifactId> 22 <version>1.3.2</version> 23 </dependency> 24 </dependencies> 25 </plugin>
其中generatorConfig.xml的位置,你們根據實際狀況自行調整sql
2、generatorConfig.xml配置文件mybatis
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration 3 PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 4 "http://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" targetRuntime="MyBatis3"> 10 <commentGenerator> 11 <property name="suppressDate" value="false"/> 12 <property name="suppressAllComments" value="true"/> 13 </commentGenerator> 14 15 <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" 16 connectionURL="jdbc:oracle:thin:@172.20.16.***:1521:CARGO" userId="***" 17 password="***"/> 18 19 <javaModelGenerator targetPackage="ctas.test.entity" 20 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 25 <sqlMapGenerator targetPackage="ctas.test.entity.xml" 26 targetProject="D:/yangjm/Code/CTAS/JAVAEE/CTAS2CCSP/src/main/java"> 27 <property name="enableSubPackages" value="true"/> 28 </sqlMapGenerator> 29 30 <javaClientGenerator targetPackage="ctas.test.mapper" 31 targetProject="D:/yangjm/Code/CTAS/JAVAEE/CTAS2CCSP/src/main/java" type="XMLMAPPER"> 32 <property name="enableSubPackages" value="true"/> 33 </javaClientGenerator> 34 35 <!--<table tableName="T_FEE_AGTBILL" domainObjectName="FeeAgentBill" 36 enableCountByExample="false" enableUpdateByExample="false" 37 enableDeleteByExample="false" enableSelectByExample="false" 38 selectByExampleQueryId="false"/>--> 39 40 <table tableName="CTAS_FEE_BASE" domainObjectName="FeeBase" 41 enableCountByExample="false" enableUpdateByExample="false" 42 enableDeleteByExample="false" enableSelectByExample="false" 43 selectByExampleQueryId="false"> 44 <!--<columnRenamingRule searchString="^D_" 45 replaceString=""/>--> 46 </table> 47 48 </context> 49 </generatorConfiguration>
幾個要點:
a) 由於生成過程當中須要鏈接db,因此第3行指定了驅動jar包的位置oracle
b) 15-17行爲鏈接字符串app
c) 19-33行指定生成「entity實體類、mybatis映射xml文件、mapper接口」的具體位置dom
d) 40-46行爲具體要生成的表,若是有多個表,複製這一段,改下表名便可eclipse
3、使用方式maven
mvn mybatis-generator:generate
若是是在intellij 環境,直接鼠標點擊便可
最後給出目錄結構圖:
最後給一些小技巧:
a) 建表時,字段名稱建議用"_"分隔多個單詞,好比:AWB_NO、REC_ID...,這樣生成的entity,屬性名稱就會變成漂亮的駝峯命名,即:awbNo、recId
b)oracle中,數值形的字段,若是指定精度,好比Number(12,2),默認生成entity屬性是BigDecimal型 ,若是不指定精度,好比:Number(9),指默認生成的是Long型
c)oracle中的nvarchar/nvarchar2,mybatis-generator會識別成Object型,建議不要用nvarchar2,改用varchar2