隨筆-10  評論-23  文章-32  trackbacks-0
            現(xiàn)在我們著手來寫一個ibatis的簡單例子.
            如果你是使用eclipse開發(fā)項目的話,那么,有一個eclipse的插件ibator,可以通過配置自動生成java代碼sqlmap等,蠻好用。下面先做一個使用ibator插件的例子。
            1.使用link方式在線安裝ibator。
              eclipse菜單  help-->
                           software updates-->
                           add site-->(填入在線安裝地址:http://ibatis.apache.org/tools/ibator
                           -->一直下一步安裝
            2.新建一個web工程,導入相應的包,我使用的數(shù)據(jù)庫是ORACLE,所以我需要導入的包是:
              ojdbc14.jar或classes12.jar,把ibatis需要的jar包都導進去
              此例我導入的jar包如下:

             

            3.由于你安裝了ibator插件,那么你在你的工程某個目錄下單擊右鍵-->new-->other里將會看到
              Apache iBatis Ibator文件夾,下面只有一個選項,如下圖。
             

              點擊以后要求你輸入文件名(這個就是ibator的配置文件):
             

              填入配置文件名稱,可以隨便設置配置文件名字。一般為“ibatorConfig.xml”,點確定后就創(chuàng)建好了一個ibator配置文件的模板。
              代碼如下:
             
          <?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.dtd" >
          <ibatorConfiguration >
            
          <ibatorContext id="context1" >
              
          <jdbcConnection driverClass="???" connectionURL="???" userId="???" password="???" />
              
          <javaModelGenerator targetPackage="???" targetProject="???" />
              
          <sqlMapGenerator targetPackage="???" targetProject="???" />
              
          <daoGenerator targetPackage="???" targetProject="???" type="GENERIC-CI" />
              
          <table schema="???" tableName="???" >
                
          <columnOverride column="???" property="???" />
              
          </table>
            
          </ibatorContext>
          </ibatorConfiguration>
             
              4.將對應配置參數(shù)替換掉上面的“?”號。我自己的替換文件是這樣的:
          <?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.dtd" >
          <ibatorConfiguration >
            
          <classPathEntry location="D:\Program Files\work_soft\apache-maven-2.0.9\repository\com\oracle\ojdbc14\10.2.0.1.0\ojdbc14-10.2.0.1.0.jar"/>
            
          <ibatorContext id="content1">
              
          <jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver" connectionURL="jdbc:oracle:thin:@localhost:1522:zju" userId="ly" password="ly">
                  
          <property name="" value=""/>
              
          </jdbcConnection>
              
          <javaModelGenerator targetPackage="com.model" targetProject="ibatistest">
              
          </javaModelGenerator>
              
          <sqlMapGenerator targetPackage="com.xml" targetProject="ibatistest">
              
          </sqlMapGenerator>
              
          <daoGenerator targetPackage="com.dao" targetProject="ibatistest" type="IBATIS">
              
          </daoGenerator>
              
          <table tableName="TB_USER" domainObjectName="user">
              
          </table>
            
          </ibatorContext>
          </ibatorConfiguration>
               其中<table tableName="TB_USER"...> 這個"TB_USER"是在數(shù)據(jù)庫中事先創(chuàng)建好的表,就是我們要通過ibatis操作的表。
               <table..> 這里如果什么也不寫的話,默認會使用數(shù)據(jù)庫中的字段名產生pojo類</table>

               關于ibatorConfig.xml中配置參數(shù)具體內容可以參考一下apache文檔
               文檔地址:http://ibatis.apache.org/docs/tools/abator/

               5.之后我們只需要在這個配置文件上點擊右鍵-->點擊Generate ibatis artifacts,這樣就應該能生成對應的package和類了.我的生成如下圖:

              
              
               6.在我使用ibator的時候發(fā)生了以下幾處錯誤:
                 1)Exception :getting jdbc Driver   由于我開始的時候將
          <classPathEntry location="D:"Program Files"work_soft"apache-maven-2.0.9"repository"com"oracle"ojdbc14"10.2.0.1.0"ojdbc14-10.2.0.1.0.jar"/>
                   這句放到了jdbcConnection里,而新版本是放到外面的,所以報此錯誤。
                 2)Cannot find source folder ibatistest/src
                   因為端口號沒有配置正確1522配置成1521了,所以報這個問題,網上還有人說是端口號配置正確了防火墻攔截也有可能導致這個問題,那么只需要去把防火墻里的“例外”里添加你數(shù)據(jù)庫使用的端口號就可以了,如果安裝了防火墻軟件也是一樣道理,添加一個例外的端口。
                   做完以上的修改以后先用sqlplus試一下,如果能夠登錄那么就對了,如果不能夠登錄,那么你需要開啟數(shù)據(jù)庫的監(jiān)聽程序,這個比較多內容就不再這里說了,可以去網上查一下如何開啟數(shù)據(jù)庫的監(jiān)聽程序。
                 3)Cannot find source folder ibatistest/src,由于我開始的時候將<javaModelGenerator targetPackage="com.model" targetProject="ibatistest/src">里的targetProject的值設置為ibatistest/src,但是我沒有創(chuàng)建這個文件夾,所以就報這個錯誤了,如果你沒有創(chuàng)建任何源文件夾那么就是用你的工程名字就好了。
                 4)
          Invalid name specified: com/dao  由于我把com.dao寫成com/dao所以說是無效的包名.



             
          posted on 2009-07-22 11:34 liuyimx 閱讀(6966) 評論(0)  編輯  收藏 所屬分類: ibatis

          只有注冊用戶登錄后才能發(fā)表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 大连市| 新泰市| 宜都市| 和硕县| 平邑县| 潍坊市| 马边| 洛宁县| 台前县| 连州市| 得荣县| 衡阳市| 休宁县| 多伦县| 霸州市| 梓潼县| 金寨县| 永靖县| 邵东县| 江华| 潜江市| 漾濞| 武鸣县| 突泉县| 枣庄市| 晋中市| 汝城县| 财经| 萝北县| 绥江县| 通道| 宁安市| 射阳县| 贺州市| 那坡县| 普定县| 米易县| 正安县| 河池市| 夏邑县| 宿松县|