对于mybatis查询结果返回类型的理解

这两天一直在纠结resultType与resultMap的使用场景的问题,当然也因为自己之前学的不扎实的原因,导致在做练手项目的时候不理解这里为什么要使用resultType,那里为什么要使用resultMap,看了很多博客,但是没有理解到其中的精髓,今天认真翻了翻直接写的mapper.xml文件,大概根据里面的信息总结了一下:

1.resultType的使用场景一般是在单表查询的时候,如果只是单表查询,一般则使用resultType,但是可以满足字段与属性并不需要一一对应,即查询的字段数与entity的属性个数可以不相同,依然能够得到查询结果。在表的字段名和entity的属性名不相同的时候,有两个解决方法:一是在写sql语句的时候,将查询的字段使用别名,如shop_name as shopName 这样也能够和实体类的属性一一对应;二是可以使用resultMap,而在这里,我认为这就是resultMap的第一个使用场景,解决表的字段名和实体类的属性名不相同的情况下,可以使用resultMap。那么在针对表的字段名和实体类的属性名不相同的情况,还有一种解决办法,就是在mybatis配置文件中配置<setting name="mapUnderscoretoCamelCase" value="true"/>,即开启驼峰命名转换,但是表的字段名和实体类的属性名必须要满足一定的命名规则,即刚刚所写的 shop_name 和shopName,这样就能够满足驼峰命名转换的规则。

2.resultMap的使用场景,一是能够解决刚刚的表字段名和实体类属性名不相同的问题,二就是能够解决多表连接查询的问题了,这也是resultType办不到的,即

<resultMap type="Product" id="productMap">
<id column="product_id" property="productId" />
<result column="product_name" property="productName" />
<result column="product_desc" property="productDesc" />
<result column="img_addr" property="imgAddr" />
<result column="normal_price" property="normalPrice" />
<result column="promotion_price" property="promotionPrice" />
<result column="priority" property="priority" />
<result column="create_time" property="createTime" />
<result column="last_edit_time" property="lastEditTime" />
<result column="enable_status" property="enableStatus" />
<association property="shop" column="shop_id" javaType="Shop">
<id column="shop_id" property="shopId" />
<result column="owner_id" property="ownerId" />
<result column="shop_name" property="shopName" />
</association>
<association property="productCategory" column="product_category_id"
javaType="ProductCategory">
<id column="product_category_id" property="productCategoryId" />
<result column="product_category_name" property="productCategoryName" />
</association>
<collection property="productImgList" column="product_id"
ofType="ProductImg">
<id column="product_img_id" property="productImgId" />
<result column="detail_img" property="imgAddr" />
<result column="img_desc" property="imgDesc" />
<result column="priority" property="priority" />
<result column="create_time" property="createTime" />
<result column="product_id" property="productId" />
</collection>
</resultMap>

<select id="queryProductById" resultMap="productMap"
parameterType="Long">
<!-- 具体的sql -->
SELECT
p.product_id,
p.product_name,
p.product_desc,
p.img_addr,
p.normal_price,
p.promotion_price,
p.priority,
p.create_time,
p.last_edit_time,
p.enable_status,
p.product_category_id,
p.shop_id,
pm.product_img_id,
pm.img_addr AS detail_img,
pm.img_desc,
pm.priority,
pm.create_time
FROM
tb_product p
LEFT JOIN
tb_product_img pm
ON
p.product_id =
pm.product_id
WHERE
p.product_id =
#{productId}
ORDER BY
pm.priority DESC

</select>

这是我项目的一个sql语句,就直接贴过来了,因为在这次查询中也需要查询其他表中的字段,那么这时候就没有一个pojo类来与该查询结果对应,所以就需要使用到resultMap。然后在resultMap标签中有几个标签需要说明,第一个就是association,即联合、联系的意思,这是针对一对一或多对一的查询,而另一个就是collection,这是针对一对多的查询。注意一个问题,association标签中的对应pojo的属性名是javaType,而collection中是ofType,这是需要区分开的。同时,association和collection都是用于一个类中有另一个类作为其域属性的时候来使用的。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值