標籤:
一、安裝MyBatis_Generator外掛程式到eclipse
1、方法1:
下載MyBatis_Generator_1.3.1.zip,解壓後把features和plugins2個檔案夾複製到eclipse安裝目錄下。
2、方法2:
Help->Install New Software輸入外掛程式地址:http://mybatis.googlecode.com/svn/sub-projects/generator/trunk/eclipse/UpdateSite/
具體安裝過程可參考百度。
二、建立generatorConfig.xml
<?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" > <generatorConfiguration> <!-- 指定驅動檔案的路徑(也可以是相對路徑) --> <classPathEntry location="F:\JAVA\java_jar\mysql_driver\mysql-connector-java-5.1.0-bin.jar"/> <context id="DB2Tables"> <!-- suppressDate :false時開啟時間標誌,true時關閉 --> <!-- suppressAllComments :false時開啟注釋,true時關閉注釋 --> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--資料庫URL,使用者名稱、密碼 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/blog" userId="root" password="***" /> <!-- mybatis裡專門用來處理NUMERIC和DECIMAL類型的策略 --> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- 實體類組建組態,目標檔案包和工程,注意maven工程結構目錄下應該帶上工程名和src/main/java--> <javaModelGenerator targetPackage="com.blog.pojo" targetProject="blog/src/main/java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- mapper.xml檔案組建組態,目標檔案包和工程,注意maven工程結構目錄下應該帶上工程名和src/main/java--> <sqlMapGenerator targetPackage="com.blog.mapping" targetProject="blog/src/main/java"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!-- 介面組建組態,目標檔案包和工程,注意maven工程結構目錄下應該帶上工程名和src/main/java--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.blog.dao" targetProject="blog/src/main/java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!-- 指定產生的表: tableName是資料庫中的表名 domainObjectName是實體類名--> <!-- mybatis generator代碼產生器在預設的情況下會產生對於表實體類的一個Examle類,可以更改產生器的配置為false避免產生Examle類 --> <!-- columnOverride:如果不存在則預設按資料庫中欄位類型建立實體類,如果存在則按以下指定的名稱和類型建立實體類欄位, --> <!-- ignoreColumn:在實體類中不建立此屬性 --> <table tableName="blog_user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <!-- <columnOverride column="username" property="name" /> <ignoreColumn column="password"/> <columnOverride column="age" javaType="String"></columnOverride> --> </table> </context></generatorConfiguration>
三、點擊generatorConfig.xml,選擇Generate MyBatis/iBatis Arifacts,產生對應dao,mapper,entity。
附上工程目錄:
mybatis-generator自動產生dao,mapper,entity