ibatis的resultClass

本文通过实例解析了ibatis中resultClass与resultMap的区别。在使用resultClass时,如果数据库字段与类属性名称不一致,会导致映射错误。解决办法是在SQL语句中使用AS关键字重命名字段,或改用resultMap进行自定义映射。实测表明,resultMap在性能上优于resultClass。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ibatis的resultClass与resultMap还是有很大的区别。以下是我碰到的一个问题。

     配置文件写法如下:   

复制代码
1 <sqlMap> 2 <typeAlias alias="notice" type="path.country.basic.entity.Notice"/> 3 <resultMap id="noticeResult" class="notice" > 4 <result property="id" column="MainID"/> 5 <result property="content" column="Notice_Content"/> 6 <result property="isUsed" column="Notice_IsUsed"/> 7 <result property="createMan" column="CreateId"/> 8 <result property="createDate" column="CreateDate"/> 9 <result property="createIp" column="CreateIP" /> 10 <result property="lastModifyMan" column="ModifyId"/> 11 <result property="lastModifyDate" column="ModifyDate" /> 12 <result property="lastModifyIp" column="ModifyIP" /> 13 <result property="manName" column="CreateId" select="getUserNameById"/> 14 </resultMap> 15 <select id="getNewNotice" resultClass="notice"> 16 SELECT * FROM Bse_Notice WHERE Notice_IsUsed='1' 17 </select> 18  </sqlMap>
复制代码

 

 

      在前台调用时,对象是取到了,但是里面的属性值为空。

      查了相关资料才知道:resultclass属于隐身映射,虽然你指定resultclass=“”,具体某一个类,但是select语句得到的结果是一条实力记录,但如果数据库字段与类的属性名字不一致,这个时候就会出现映射错误,有一种方式可以解决就是在写select语句时,给每个字段用as运算符取名字与属性一样:例如:select realname as name...其中realname是字段列名,name是属性字段名。

      当然解决问题还是很容易的,只需要把resultClass改为resultMap就行了。

      在性能方面,resultMap要比resultClass高,所以建议尽量使用resultMap。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kzbpp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值