ibator的使用

ibator是與ibatis配套的代碼生成工具,運行方包括命令行、ant任務和在eclipse中運行等。這裏介紹在eclipse中如何使用該工具。

  一、在eclipse中安裝ibator

  1、版本檢查:新版本的ibator對eclipse和jdk有版本要求,在安裝ibator前需要先檢查這兩個工具的版本。

  2、安裝:在eclipse中單擊help->install software->add "http://ibatis.apache.org/tools/ibator" . 然後一直下一步,在許可頁面選擇同意,點擊finish即可完成安裝。

  二、在eclipse中使用ibator

  ibator可以幫你自動生成map文件、與數據庫表對應的Java實體以及dao;ibator是xml驅動的代碼生成工具,在使用ibator前,需要準備ibator需要的xml文件,該文件爲ibator的配置文件,它告訴ibator使用什麼樣的驅動、連接那個數據庫、生成的Java實體類文件的包名、生成的dao文件的包名、ibatis配置文件的包名等等。

  一個簡單的配置文件如下:

  <?xml version="1.0" encoding="UTF-8" ?>

  <!DOCTYPE ibatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Apache iBATIS Ibator Configuration 1.0//EN" "http://ibatis.apache.org/dtd/ibator-config_1_0.dt d" >

  <ibatorConfiguration>

  <classPathEntry location="E:\libs\mysql-jdbc\5.1.11\mysql-connecto r-java-5.1.11-bin.jar"/>

  <ibatorContext id="context1">

  <jdbcConnection driverClass="com.mysql.jdbc.Driver"

  connectionURL="jdbc:mysql://localhost:3306/ibatis" userId="tibetjungle"

  password="########" />

  <javaModelGenerator targetPackage="org.tibetjungle.ibator"

  targetProject="IbatisDemo\src" />

  <sqlMapGenerator targetPackage="sqlmap"

  targetProject="IbatisDemo" />

  <daoGenerator targetPackage="org.tibetjungle.ibatis.dao"

  targetProject="IbatisDemo" type="IBATIS" />

  <table schema="ibatis" tableName="tax_rate">

  </table>

  </ibatorContext>

  </ibatorConfiguration>

  這個配置文件將連接我本地的一個mysql數據庫,javaModelGenerator告訴ibatis將生成的Java實體類放在我IbatisDemo項目的src文件夾下;sqlMapGenerator指示ibator生成的sqlmap文件存放在何處以及文件夾名;daoGenerator屬性告訴ibator生成的dao類的存儲位置、報名和dao的類型;table屬性指示需要映射的數據庫表。

  準備好這些後,在xml文件上輕鬆的單擊右鍵,選擇“Generate iBatis Artifacts",所有文件都輕鬆生成。

  這裏需要注意,在eclipse中使用時,如果沒有在配置文件中使用classPathEntry節點將包含驅動的jar文件包含進來,ibator會報找不到驅動的異常;另外,ibatis dao已經從2.3中不建議使用,所以daoGenerator的type屬性建議用Spring或者另外其它兩個:IBATIS-CI, IBATIS-SI。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章