Mybatis-Generator外掛程式自動產生Dao、Model、Mapping相關檔案

來源:互聯網
上載者:User

標籤:dtd   project   lib   org   bpa   int   pass   false   1.2   

      最近做項目,mapping 有點多而且容易寫錯,於是試著用了Mybatis-Generator 外掛程式自動產生 dao, domain  mapping 檔案。感覺還挺好用。把相關配置分享,一邊以後做項目的時候直接拿來用。  我用的是eclipse Mybatis-Generator 外掛程式。

      環境:eclipse  Mars.2

      外掛程式:org.mybatis.generator.eclipse.site-1.3.5.201609070108   

      資料庫jar:mysql-connector-java-5.1.26-bin.jar

        資料庫表:

  

CREATE TABLE `teacher` (  `id` bigint(20) NOT NULL DEFAULT ‘0‘ COMMENT ‘主鍵id‘,  `name` varchar(40) NOT NULL DEFAULT ‘‘ COMMENT ‘名稱‘,  `age` smallint(6) NOT NULL DEFAULT ‘0‘ COMMENT ‘年齡‘,  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=‘教師表‘;

 

   關於外掛程式eclipse 自行百度。當安裝成功後(重啟eclipse ,在建立項目裡面有一個mybatis 檔案夾說明外掛程式安裝成功,如),

    首先建立一個Java項目(普通項目 or  web項目)

   點擊建立  ---> 選擇其他----> 選擇mybatis  --->  選擇下一步 ---> 選擇本地項目       會自動產生一個xml  檔案,設定就是在xml 裡面完成的

 

   

 

 

 最終目錄結構如  ,需要說明的是  包必須要自己建立,資料庫的jar 包需要自己添加

    源檔案: mybatis_generatorConfig.properties

    

 1 # 資料庫驅動jar 路徑 2 drive.class.path=F:/Users/zh/workspace/Mybatis_Generator/webapp/WEB-INF/lib/mysql-connector-java-5.1.26-bin.jar 3  4 # 資料庫連接參數 5 jdbc.driver=com.mysql.jdbc.Driver 6 jdbc.url=jdbc:mysql://localhost:3306/articl 7 jdbc.username=root 8 jdbc.password=123 9 10 # 包路徑配置11 model.package=cn.mg.damain12 dao.package=cn.mg.dao13 xml.mapper.package=cn.mg.dao14 #項目名稱15 target.project=Mybatis_Generator

 

generatorConfig.xml 配置:

  

 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     <!-- 設定檔路徑 --> 8     <properties resource="mybatis_generatorConfig.properties" /> 9     <!--資料庫驅動包路徑 -->10     <classPathEntry location="${drive.class.path}" />11 12     <context id="MySQLTables" targetRuntime="MyBatis3">13         <!--關閉注釋 -->14         <commentGenerator>15             <property name="suppressDate" value="true" />16         </commentGenerator>17 18         <!--資料庫連接資訊 -->19         <jdbcConnection driverClass="${jdbc.driver}"20             connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}">21         </jdbcConnection>22          <!--targetPackage 表示包名    targetProject:表示項目名稱 -->23         <!--產生的model 包路徑 -->24         <javaModelGenerator targetPackage="${model.package}"25             targetProject="${target.project}">26             <property name="enableSubPackages" value="ture" />27             <property name="trimStrings" value="true" />28         </javaModelGenerator>29 30         <!--產生xml mapper檔案 路徑 -->31         <sqlMapGenerator targetPackage="${xml.mapper.package}"32             targetProject="${target.project}">33             <property name="enableSubPackages" value="ture" />34         </sqlMapGenerator>35 36         <!-- 產生的Dao介面 的包路徑 -->37         <javaClientGenerator type="XMLMAPPER"38             targetPackage="${dao.package}" targetProject="${target.project}">39             <property name="enableSubPackages" value="ture" />40         </javaClientGenerator>41 42         <!--對應資料庫表名 tableName    domainObjectName 表示產生的domain 名-->43         <table tableName="teacher" domainObjectName="teacher" enableCountByExample="false"44             enableUpdateByExample="false" enableDeleteByExample="false"45             enableSelectByExample="false" selectByExampleQueryId="false">46         </table>47     </context>48 </generatorConfiguration>

 最後右鍵generatorConfig.xml  選擇Generate

 

Mybatis-Generator外掛程式自動產生Dao、Model、Mapping相關檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.