標籤:
由於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