使用Mybatis-Generator自動生成Dao、Model、Mapping相關文件

Mybatis屬於半自動ORM,在使用這個框架中,工作量最大的就是書寫Mapping的映射文件,由於手動書寫很容易出錯,我們可以利用Mybatis-Generator來幫我們自動生成文件。

1、相關文件

關於Mybatis-Generator的下載可以到這個地址:https://github.com/mybatis/generator/releases

由於我使用的是Mysql數據庫,這裏需要在準備一個連接mysql數據庫的驅動jar包

以下是相關文件截圖:

使用Mybatis-Generator自動生成Dao、Model、Mapping相關文件

2.配置generatorConfig文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"&gt;
<generatorConfiguration>

需要修改文件配置的地方我都已經把註釋標註出來了,這裏的相關路徑(如數據庫驅動包,生成對應的相關文件位置可以自定義)不能帶有中文。 上面配置文件中的:
tableName和domainObjectName爲必選項,分別代表數據庫表名和生成的實力類名,其餘的可以自定義去選擇(一般情況下均爲false)。 生成語句文件: java -jar mybatis-generator-core-1.3.7.jar -configfile generatorConfig.xml -overwrite 3、使用方法 在該目錄按住window鍵+R,進入cmd 命令,cd到相應的目錄,注意,目錄是存放mybatis-generator-core-1.3.7.jar和generatorConfig.xml文件目錄,如圖所示 ![](https://s1.51cto.com/images/blog/201809/28/c2a44a5c9311333929b389b7e9d44a4a.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)![](https://s1.51cto.com/images/blog/201809/28/30fdb9a8747e5823e41fed4225ca13f3.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=) 最後看下生成的效果圖 ![](https://s1.51cto.com/images/blog/201809/28/1108a9cfd04376e3f0ea19923df98547.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=) ![](https://s1.51cto.com/images/blog/201809/28/05d61e71826c541aed98eef6f93e4b4f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=) ![](https://s1.51cto.com/images/blog/201809/28/8f112dea1111d20c519e50595429dcdc.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章