学习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);

 

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