前言
大家都知道Mybatis屬於半自動ORM,在使用這個框架中,工作量最大的就是書寫Mapping的映射文件,並且手動書寫很容易出錯,那麼今天來介紹一下使用Mybatis-Generator來幫我們自動生成文件。如果大家有更好實現方式歡迎留言一起探討哦,讓大家開發起來更爽更便捷~~~
第一步:下載mybatis-generator工具包
GitHub地址:https://github.com/yundianzixun/mybatis-generator-1.35,如下圖所示:
第二步:修改配置信息
generatorConfig.xml
br> PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorconfiguration>
<classpathentry>
<context>
<commentgenerator>
<property>
<property>
/<commentgenerator>
<jdbcconnection> connectionURL="數據庫URL" userId="數據庫用戶名" password="數據庫密碼">
/<jdbcconnection>
<javatyperesolver>
<property>
/<javatyperesolver>
<javamodelgenerator>
<property>
<property>
/<javamodelgenerator>
<sqlmapgenerator>
<property>
/<sqlmapgenerator>
<javaclientgenerator>
<property>
/<javaclientgenerator>
<table> enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
/<table>
/<context>/<generatorconfiguration>
修改點1:數據庫配置
<jdbcconnection> connectionURL="數據庫URL" userId="數據庫用戶名" password="數據庫密碼"> /<jdbcconnection>
修改點2:生成model類存放位置
#com.itunion.wxshop.model 可修改為自己項目映射目錄
<javamodelgenerator>
<property>
<property>
/<javamodelgenerator>
修改點3:生成mapping文件存放位置
#targetPackage 報名可以修改
<sqlmapgenerator>
<property>
/<sqlmapgenerator>
修改點4:生產Dao類存放位置
#targetPackage 目錄可修改
<javaclientgenerator>
<property>
/<javaclientgenerator>
修改點5:生成對應表及類名
#對應自己的表信息(可copy多個)<table> enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false">
/<table>
第三步:控制檯執行生成命令(必須要安裝好jdk哦)
- 進入mybatis-generator工具 lib 目錄
xxx-2:~ lin$ cd /Users/lin/Downloads/JavaCode/mybatis-generator-core-1.3.5wx-shop/lib
- 執行命令
xxx-2:~ lin$ cd /Users/lin/Downloads/JavaCode/mybatis-generator-core-1.3.5wx-shop/lib
xxx-2:lib lin$ java -jar mybatis-generator-core-1.3.5.jar -configfile generatorConfig.xml -overwrite
MyBatis Generator finished successfully.
xxx-2:lib lin$
- 執行結果
MyBatis Generator finished successfully.
- 結果查看
第四步:將生成的文件放到自己項目中
generatorConfig.xml 文件裡面的項目路徑配置好了 直接copy就可以用,如果沒有配置好 那麼生成的內容還需要手工修改。
關注我們
更多精彩內容請關注“IT實戰聯盟”哦~~~
閱讀更多 IT實戰聯盟 的文章