MyBatis---使用MyBatis Generator產生Dto、Dao、Mapping

來源:互聯網
上載者:User

標籤:

由於MyBatis屬於一種半自動的ORM架構,所以主要的工作將是書寫Mapping對應檔,但是由於手寫對應檔很容易出錯,所以查資料發現有現成的工具可以自動產生底層模型類、Dao介面類甚至Mapping對應檔。

產生代碼需要的檔案和jar包:

(檔案:http://download.csdn.net/detail/u012909091/7206091)

其中有mybatis架構的jar包,資料庫驅動程式jar包以及MyBatis產生器jar包。其中的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 <generatorConfiguration>   6 <!--資料庫驅動--> 7     <classPathEntry  location="mysql-connector-java-5.1.25-bin.jar"/>   8     <context id="DB2Tables"  targetRuntime="MyBatis3">   9         <commentGenerator>  10             <property name="suppressDate" value="true"/>  11             <!--是否去除自動產生的注釋 true:是 : false:否 -->12             <property name="suppressAllComments" value="true"/>  13         </commentGenerator>  14         <!--資料庫連結URL,使用者名稱、密碼 -->15         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.7.201:12340/program_shopcart" userId="sa" password="1234">  16         </jdbcConnection>  17         <javaTypeResolver>  18             <property name="forceBigDecimals" value="false"/>  19         </javaTypeResolver>  20         <!--產生模型的包名和位置-->21         <javaModelGenerator targetPackage="wyp.db.test.domain" targetProject="src">  22             <property name="enableSubPackages" value="true"/>  23             <property name="trimStrings" value="true"/>  24         </javaModelGenerator>  25         <!--產生對應檔的包名和位置-->26         <sqlMapGenerator targetPackage="wyp.db.test.mapping" targetProject="src">  27             <property name="enableSubPackages" value="true"/>  28         </sqlMapGenerator>  29         <!--產生DAO的包名和位置-->30         <javaClientGenerator type="XMLMAPPER" targetPackage="wyp.db.test.IDao" targetProject="src">  31             <property name="enableSubPackages" value="true"/>  32         </javaClientGenerator>  33         <!-- 要產生的表 tableName是資料庫中的表名或視圖名 domainObjectName是實體類名-->34         <table tableName="account_group" domainObjectName="Account_group" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>35         <table tableName="account_grouppermissionassign" domainObjectName="Account_grouppermissionassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>36         <table tableName="account_permission" domainObjectName="Account_permission" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>37         <table tableName="account_role" domainObjectName="Account_role" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>38         <table tableName="account_rolepermissionassign" domainObjectName="Account_rolepermissionassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>39         <table tableName="account_user" domainObjectName="Account_user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>40         <table tableName="account_usergroupassign" domainObjectName="Account_usergroupassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>41         <table tableName="account_userpermissionassign" domainObjectName="Account_userpermissionassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>42         <table tableName="account_userroleassign" domainObjectName="Account_userroleassign" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>43     </context>  44 </generatorConfiguration>  
generatorConfig.xml

 

 當以上這些完成之後,只需要開啟控制台,進入lib目錄下,執行指令碼:

java -jar mybatis-generator-core-1.3.2.jar -configfile generatorConfig.xml -overwrite

 即可。

 

MyBatis---使用MyBatis Generator產生Dto、Dao、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.