使用Eclipse的Generator MyBatis/iBatis Artifacts插件自動生成代碼,與spring集成

生成代碼在項目中使用

使用Eclipse的Generator MyBatis/iBatis Artifacts插件生成代碼截圖如下,此例子使用oracle庫。庫名mydatabase,表爲aa做得測試,在接下來的講解中都以此爲例。

一. 在Eclipse中安裝插件

此例子是使用link方式插件安裝

1. 把mybatisGenerator.rar解壓到我們指定的硬盤中,如:c:/mybatisGenerator


2. 在Eclipse的dropins目錄中新建mybatisGenerator.link文件,如D:\geyouchao\eclipse4.2\dropins  

mybatisGenerator.link文件內容爲

path=c:\\mybatisGenerator

3. 驗證插件是否安裝成功


二. 在Eclipse中創建java項目

1. 創建java項目,如mybatis001。注:此例是使用oracle+ibatis,所以需要導入ojdbc14.jar、ibatis-2.3.0.677.jar兩個jar包(若是生成mybatis,需要mybatis-3.0.6.jar;若是pg數據庫,需要postgresql-9.3-1103.jdbc4.jar)

2. 在mybatis001項目src目錄下,新建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 >
	<context id="me_ibatis" defaultModelType="flat" targetRuntime="Ibatis2Java5" >
    	<commentGenerator>
    	    <property name="suppressAllComments" value="true"/>
    	    <property name="suppressDate" value="true"/>
    	</commentGenerator>
	    <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" 
	        connectionURL="jdbc:oracle:thin:@127.0.0.1:1526:mydatabase" 
	        userId="gyc" password="abc1234" />
	        
	    <javaModelGenerator targetPackage="com.gyc.test.biz.dto" 
	        targetProject="mybatis001\src">
    	</javaModelGenerator>
	    
	    <sqlMapGenerator targetPackage="com.gyc.test.biz.dao.sql" 
	        targetProject="mybatis001\src"/>
	    
	    <javaClientGenerator targetPackage="com.gyc.test.biz.dao" 
	        implementationPackage="com.gyc.test.biz.dao.impl" 
	        targetProject="mybatis001\src" type="SPRING" />

	    <table tableName="aa"
	        enableCountByExample="false"
			enableUpdateByExample="false"
			enableDeleteByExample="false"
			enableSelectByExample="false"
			selectByExampleQueryId="false"
	        /> 
  	</context>
</generatorConfiguration>

3. 右鍵generatorConfig.xml文件,點擊在菜單中點擊Generator MyBatis/iBatis Artifacts(若右鍵菜單中沒有此菜單,說明第一步驟中插件沒有安裝成功),自動生成4個文件。


代碼生成器生成4個文件,分別是Aa.java、Aa_SqlMap.xml、AaDAO.java和AaDAOImpl.java。接下來對生成的代碼簡單修改就可以在我們項目框架使用了。


發佈了27 篇原創文章 · 獲贊 41 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章