Day19-封装mybaties输出结果

简单类型
mapper 文件:
<select id="countStudent" resultType="int">
 select count(*) from student
</select>


对象类型
mapper 文件:
resultType:类全限定名称
<select id="selectById" resultType="com.dt.domain.Student">
 select id,name,age from student where id=#{studentId}
</select>

resultMap

01-介绍

resultMap 可以自定义 sql 的结果和 java 对象属性的映射关系。更灵活的把列值赋值给指定属性。 常用在列名和 java 对象属性名不一样的情况。

02-使用方式:

1.先定义 resultMap,指定列名和属性的对应关系。

2.在中把 resultType 替换为 resultMap。

接口方法:
List<Student> selectUseResultMap(QueryParam param);
mapper 文件:
<!-- 创建 resultMap
 id:自定义的唯一名称,在<select>使用
 type:期望转为的 java 对象的全限定名称或别名
-->
<resultMap id="studentMap" type="com.dt.domain.Student">
 <!-- 主键字段使用 id -->
 <id column="id" property="id" />
 <!--非主键字段使用 result-->
 <result column="name" property="name"/>
 <result column="age" property="age" />
</resultMap>
<!--resultMap: resultMap 标签中的 id 属性值-->
<select id="selectUseResultMap" resultMap="studentMap">
 select id,name,age from student where name=#{queryName} or
age=#{queryAge}
</select>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值