學習iBATIS --碰到的問題

iBATIS 是一個可以簡化開發持久層的一個工具。


官方地址

 

iBATIS 2.3.0 下載地址

 

今天按照simple-example做了一下。覺得用起來還是很方便的。

做的時候需要注意的問題是:

1.如"<sqlMap resource="com/mydomain/data/Account.xml"/>"這樣的路徑一定要對應。

2.SqlMapConfig.xml裏面的transactionManager一定要配置正確。

 

配置中的對應:

Account.xml

1.Result maps描述了從db裏的列到檢索的結果集之間的一種映射關係,如果檢索時的列名或者列別名和JavaBean裏的properties是一致的,不用Result maps也可以。(<result property="id" column="ACC_ID"/>,其中的property是否應與javabean裏的屬性一致)

 

原文: <!-- Result maps describe the mapping between the columns returned
       from a query, and the class properties.  A result map isn't
       necessary if the columns (or aliases) match to the properties
       exactly. -->

2.<typeAlias alias="Account" type="com.mydomain.domain.Account"/>

可以通過typeAlias給比較常用的而且比較長的名字取一個短的別名

3.<select id="selectAccountById" parameterClass="int" resultClass="Account">
    select
      ACC_ID as id,
      ACC_FIRST_NAME as firstName,
      ACC_LAST_NAME as lastName,
      ACC_EMAIL as emailAddress
    from ACCOUNT
    where ACC_ID = #id#
  </select>

說明:

<1> parameterClass是傳進去的參數,可以是各種對象。

<2> resultClass是檢索後返回的結果集要映射到的JavaBean。

<3> “#id#”id是要傳進來的參數。

調用這段xml的方法是:sqlMapper.queryForObject("selectAccountById", id);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章