Mybatis resultMap经验

Mybatis中的resultMap使用经验:
先来看一个官方的例子:
<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>
官方在这个例子中,对很多人来说有四个标签比较陌生,constructor,discriminator这两个我的使用频率不高,根据字面意思也可以看来,第一个是构造方法,这个在实际的业务中基本上是没有用的,这个在后面再说;第二个实际上和java中的 switch比较相似,根据value返回返回特定的结果集;association 这个标签可以看成是一个单一对象,注意这里使用的是javaType属性;collection这个标签从字面上就知道是一个集合,这里使用的属性是ofType,只要不使用错,就没有问题了,查询出来的结果会自动注入到Type中。后面这两个标签也都支持resultMap属性,所以也可以在其他地方定义好,引用正确就行了。以上是我的一个经验,有什么问题大家可以和我留言交流!
 

转载于:https://www.cnblogs.com/cyqjava/p/9026074.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值