日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

利用mybatis-generator自动生成代码

發布時間:2024/9/20 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用mybatis-generator自动生成代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

mybatis-generator有三種用法:命令行、eclipse插件、maven插件。個人覺得maven插件最方便,可以在eclipse/intellij idea等ide上可以通用。

下面是從官網上的截圖:(不過官網www.mybatis.org?最近一段時間,好象已經掛了)

一、在pom.xml中添加plugin

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的位置,大家根據實際情況自行調整

二、generatorConfig.xml配置文件

1 <?xml version="1.0" encoding="UTF-8"?>2 <!DOCTYPE generatorConfiguration3 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 <classPathEntry8 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包的位置

b) 15-17行為連接字符串

c) 19-33行指定生成“entity實體類、mybatis映射xml文件、mapper接口”的具體位置

d) 40-46行為具體要生成的表,如果有多個表,復制這一段,改下表名即可

三、使用方式

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

總結

以上是生活随笔為你收集整理的利用mybatis-generator自动生成代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。