Mybatis最入门---ResultMaps高级用法(上)

[一步是咫尺,一步即天涯]

接上文,我们基本的单表查询使用上文中的方式已经能够达到目的。但是,我们日常的业务中也存在着多表关联查询,结果是复杂的数据集合等等。本文我们就来介绍ResultMaps的高级用法,本文,我们先介绍基本的概念,具体用法实例在下一篇中专门演示给大家。敬请期待!

--------------------------------------------------------------------------------------------------------------------------------------------------------

1.首先,我们先看看一个常见的博客页面的组成,如下:

a.页面上能够展示的部分:正文,标题,日期,作者,评论正文,评论时间,评论人等等

b.页面之外的部分:用户名,用户id,用户密码,用户基本信息(电话,邮箱,地址,兴趣,特长,等等)

2.将我们页面上的信息从数据库中查出来的SQL语句转化为Mapper文件中的语句,可能是如下内容:

<select id="selectBlogDetails" resultMap="detailedBlogResultMap">
  select
       B.id as blog_id,
       B.title as blog_title,
       B.author_id as blog_author_id,
       A.id as author_id,
       A.username as author_username,
       A.password as author_password,
       A.email as author_email,
       A.bio as author_bio,
       A.favourite_section as author_favourite_section,
       P.id as post_id,
       P.blog_id as post_blog_id,
       P.author_id as post_author_id,
       P.created_on as post_created_on,
       P.section as post_section,
       P.subject as post_subject,
       P.draft as draft,
       P.body as post_body,
       C.id as comment_id,
       C.post_id as comment_post_id,
       C.name as comment_name,
       C.comment as comment_text,
       T.id as tag_id,
       T.name as tag_name
  from Blog B
       left outer join Author A on B.author_id = A.id
       left outer join Post P on B.id = P.blog_id
       left outer join Comment C on P.id = C.post_id
       left outer join Post_Tag PT on PT.post_id = P.id
       left outer join Tag T on PT.tag_id = T.id
  where B.id = #{id}
</select>
其对应着非常复杂的结果集合,Mapper文件可能长这个样子,如下:

<!-- Very Complex Result Map -->
<resultMap id="detailedBlogResultMap" type="Blog">
  <constructor>
    <idArg column="blog_id" javaType="int"/>
  </constructor>
  <result property="title" column="blog_title"/>
  <association property="author" javaType="Author">
    <id property="id" column="author_id"/>
    <result property="username" column="author_username"/>
    <result property="password" column="author_password"/>
    <result property="email" column="author_email"/>
    <result property="bio" column="author_bio"/>
    <result property="favouriteSection" column="author_favourite_section"/>
  </association>
  <collection property="posts" ofType="Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    <association property="author" javaType="Author"/>
    <collection property="comments" ofType="Comment">
      <id property="id" column="comment_id"/>
    </collection>
    <collection property="tags" ofType="Tag" >
      <id property="id" column="tag_id"/>
    </collection>
    <discriminator javaType="int" column="draft">
      <case value="1" resultType="DraftPost"/>
    </discriminator>
  </collection>
</resultMap>
对于初学者而言,看到这样的一份XML文件,我想内心一定是崩溃的!但是,不要担心,我们日常开发,很少能够遇到这样的场景,并且,相信通过我们一步一步的解释这个配置文档,以后各位也能够运用自如。

在上面的resultMap中存在很多的子元素,下面我们来逐一解释:

“constructor”:类在实例化时,用来注入结果到构造方法中。

“idArg”:ID参数,标记结果作为ID,可以帮助提高整体的效率。

“arg”:注入到构造方法的一个不同结果。

“id”:这个id,类似于数据库的主键,能够帮助提高整体的效率
“result”:即结果字段,其中包括java对象的属性值,和数据库列名

“association”:复杂类型的结果关联,结果映射能够关联自身,或者关联另一个结果集

“collection”:复杂类型的集合,

  • 4
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值