老师,我对resultMap还不是很明白

老师,我对resultMap还不是很明白

@Results(id="all",value = {
@Result(id = true,column = "id",property = "id"),
@Result(column = "title",property = "title"),
@Result(column = "cid",property = "cid"),
@Result(column = "image_path",property = "imagePath"),
@Result(column = "price",property = "price"),
@Result(column = "taste",property = "taste"),
@Result(column = "sweetness",property = "sweetness"),
@Result(column = "weight",property = "weight"),
@Result(column = "size",property = "size"),
@Result(column = "material",property = "material"),
@Result(column = "status",property = "status"),
@Result(column = "ctitle",property = "catalog.title")
})
@ResultMap("all")

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕小脸 2021-03-31 10:26:56

同学你好,

1、由于设置了@Many注解,在解析select * from catalog where id的结果集时,将其中的id值又传给了selectByPid方法,然后将其查询结果List赋给了select方法返回的Catalog对象的children属性中,所以这里要使用children

http://img1.sycdn.imooc.com//climg/6063dc7909812d1808290058.jpg

2、@Many是一对多的注解,表示将selectById的查询结果映射到select查询中。

http://img1.sycdn.imooc.com//climg/6063db6009d6a59a12200481.jpg

祝学习愉快~


好帮手慕小脸 2021-03-30 16:55:17

同学你好,@ResultMap注解的使用是引用已定义好的@Results,@ResultMap中只有一个属性value,用于指定要引用的@Results,value值为@Results的id值,如下所示:

http://img1.sycdn.imooc.com//climg/6062e4aa093f1a0804990410.jpg

祝学习愉快~

  • 提问者 复java_n #1

    那results呢?

    关联外键这两个的用法?

    @Result(column = "ctitle",property = "catalog.title")

    @Result(column = "id",property = "children",many = @Many(select = "selectByPid")

    和注解@one它们分别怎么呀? 

    2021-03-30 18:19:26
  • 好帮手慕小脸 回复 提问者 复java_n #2

    同学你好,

    1、当数据库字段名和实体类所对应的属性名字不一致的时候,通过@Results将他们对应起来。其中column是数据库字段名,property是实体类的属性名。id表示是否是主键;

    2、@One注解表示一对一关联查询,select后面接的是方法,如:

    http://img1.sycdn.imooc.com//climg/6062fc3909840d0904020032.jpg

    ​祝学习愉快~

    2021-03-30 19:02:18
  • 提问者 复java_n 回复 好帮手慕小脸 #3

    @Result(column = "id",property = "children",many = @Many(select = "selectByPid")

    在这条语句中数据库字段名为id,实体类属性名为什么为children,而不是children.id,这个many的作用是什么?


    2021-03-30 21:08:00
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师