天天看點

MyBatis-Generator在Eclipse上配置及使用

         之前用MyBatis架構的時候,都是手敲的代碼,就感覺到好麻煩的樣子。今天就到網上搜了一下MyBatis自動建構工具,就發現在官網上推薦了MyBatis Generator這個建構工具。官網推薦位址:http://mybatis.github.io/generator/index.html

         那接下來我就來詳細介紹一下MyBatis Generator配置過程及其使用方法。

  • MyBatis-Generator配置:

            1. 安裝MyBatis-Generator插件

1.1 首先,你得有MyBatis Generator這個插件。是以呢,離線安裝MyBatis-Generator插件:                        下載下傳位址:http://download.csdn.net/detail/wild_elegance_k/8999513

1.2 安裝MyBatis-Generator插件:

           将下載下傳的檔案解壓,将“features”、“plugins”拷貝到Eclipse的安裝目錄的相應目錄中即可。

        2. MyBatis-Generator的使用:

重新開機Eclipse,然後在項目中點右鍵,就能看到如圖:

MyBatis-Generator在Eclipse上配置及使用
MyBatis-Generator在Eclipse上配置及使用
MyBatis-Generator在Eclipse上配置及使用
建立一個generatorConfig.xml 之後呢,接下來就要我們來對其進行配置了,那先來說一說在這個xml 檔案中主要的配置項有哪些,或者說哪些配置項是我們必須要填的。
  1. jdbcConnection --- 資料庫連結URL、使用者名、密碼
  2. javaModelGenerator---生成模型的包名和位置,就是mybatis 裡面用的一些entity 類的存放路徑配置
  3. sqlMapGenerator ---生成的映射檔案報名和位置,就是對應mybatis 的寫sql 語句的xml檔案的存放路徑配置
  4. javaClientGenerator---生成DAO的包名和位置,就是mybatis 裡面dao 接口的存放路徑
  5. table ---這個配置項是配置在項目中操作的資料庫表
具體的配置的話,請看下面我的項目中的generator.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="D:\Applications\ProgrammingTools\maven\MavenRepository\mysql\mysql-connector-java\5.1.36\mysql-connector-java-5.1.36.jar" />
	<context id="context1">
	  	<commentGenerator>
	    	<!-- 是否去除自動生成的注釋 true:是 : false:否 -->
	    	<property name="suppressAllComments" value="true"/>
 	 	</commentGenerator>
		<!-- 資料庫連結URL、使用者名、密碼 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/ssm1" userId="root" password="root" />
		<!-- 生成模型的包名和位置 -->
		<javaModelGenerator targetPackage="com.yc.ssm.cinema.entity" targetProject="Cinema/src/main/java" />
		<!-- 生成的映射檔案報名和位置 -->
		<sqlMapGenerator targetPackage="com.yc.ssm.cinema.mapper" targetProject="Cinema/src/main/java" />
		<!-- 生成DAO的包名和位置 -->
		<javaClientGenerator targetPackage="com.yc.ssm.cinema.dao" targetProject="Cinema/src/main/java" type="XMLMAPPER" />
		<!-- 要生成的那些表(更改tableName 和domainObjectName 就可以了) -->
		<table schema="ssm1" tableName="FILMINFO" domainObjectName="FilmInfo" enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
			<columnOverride column="FILMID" property="FILMID" />
			<columnOverride column="FILMNAME" property="FILMNAME" />
			<columnOverride column="TYPEID" property="TYPEID" />
			<columnOverride column="ACTOR" property="ACTOR" />
			<columnOverride column="DIRECTOR" property="DIRECTOR" />
			<columnOverride column="TICKETPRICE" property="TICKETPRICE" />
		</table>
		<table tableName="FILMTYPE" domainObjectName="FiplType" enableCountByExample="false" enableUpdateByExample="false"
			enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false">
			<columnOverride column="TYPEID" property="TYPEID" />
			<columnOverride column="TYPENAME" property="TYPENAME" />
		</table>
	</context>
</generatorConfiguration>
           

特别值得注意的是,一般我們用Eclipse的mybatis -generator插件時,都會是在項目中建立一個generatorConfig.xml 檔案,是以在xml 檔案中的targetProject="" 這一項需要配置的是項目的名字。但是我今天在配置時,我的項目名稱叫Cinema,然後targetProject配置的targetProject="Cinema",然而這并沒有什麼用,在配置好之後點下面的按鈕時,程式一直都是顯示運作成功,但是在項目的包結構中卻遲遲不見檔案。是以這個配置項需要特别值得注意,後來各種找解決辦法,終于找到了正确的配置項:

我就以javaModelGenerator配置為例:

<javaModelGenerator targetPackage="com.yc.ssm.cinema.entity" targetProject="Cinema/src/main/java" />
           
這個配置就是指定targetProject的路徑為Cinema項目下的src/main/java包下面。
MyBatis-Generator在Eclipse上配置及使用
  • MyBatis-Generator使用:
配置好了之後,使用的時候就是一鍵操作的事:
MyBatis-Generator在Eclipse上配置及使用
這個時候如果能看到配置項中指定的包不再為空時,就意味着generate成功了。
MyBatis-Generator在Eclipse上配置及使用

繼續閱讀