您的位置:首页 > 其它

ibatis中resultMap和resultClass的区别

2014-06-20 11:38 405 查看


ibatis中resultMap和resultClass的区别

举例子说明区别:

使用resultMap的:

<typeAlias alias="wp" type="com.shared.Wp"/>

<resultMap id="wpResult" class="wp">

<result property="id" column="id"/>

<result property="name" column="name"/>

<result property="wp" column="wp"/>

<result property="flag" column="flag"/>

</resultMap>

<select id="chakan" resultMap="wpResult">

select user.id,user.name,wpb.wp,wpb.flag

from user,wpb where user.id=wpb.id

</select>

首先requltmap的值是指定的映射字段配置的id值,其次必须是的数据表中的字段和实体类的属性像对应,如果缺少或者不对应的话就会包缺少字段的错误,即查询的语句查询出来的字段必须和映射的字段相互对应,俗话就是select中查询出来多少字段那么映射配置中(result 属性中)就要有多少数据。便于将此数据封装到对象中,便于运输,例如像jsp文件中遍历数据等操作的时候比较好用。

使用requltClass的:

<typeAlias alias="wp" type="com.shared.Wp"/>

<resultMap id="wpResult" class="wp">

<result property="id" column="id"/>

<result property="name" column="name"/>

<result property="wp" column="wp"/>

<result property="flag" column="flag"/>

</resultMap>

<select id="chakan" resultClass="wp">

select user.name,wpb.wp,wpb.flag

from user,wpb where user.id=wpb.id

</select>

首先requestClass指定的值为你映射类的别名,在查询的时候不考虑是否一一对应,只要在你定义的bean类中能找到这个属性就行。这样不能直接读出具体的数据,不便于运输。

这是我自己的想法,不对的地方还请大家多多指正。谢谢了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: