MyBatis ResultMap

一、示列

<resultMap id="detailedBlogResultMap" type="pw.Blog">
  <!-- 
  id 和 result 元素都将一个列的值映射到一个简单数据类型(String, int, double, Date 等)的属性或字段;
  这两者之间的唯一不同是,id 元素表示的结果将是对象的标识属性,这会在比较对象实例时用到。
  这样可以提高整体的性能,尤其是进行缓存	和嵌套结果映射(也就是连接映射)的时候。
  column--数据表字段
  property--java类属性
  -->
  <id column="id" property="objectID" jdbcType="INTEGER"/>
  <result column="blog_title" property="title" jdbcType="VARCHAR"/>
  <!--
  用于在实例化类时,注入结果到构造方法中
  idArg - ID 参数;标记出作为 ID 的结果可以帮助提高整体性能
  arg - 将被注入到构造方法的一个普通结果
   -->
  <constructor>
    <idArg column="blog_id" javaType="int" name="id"/>
    <arg column="age" javaType="_int" name="age" />
    <arg column="username" javaType="String" name="username" />
  </constructor>
  
  <!-- 映射一个java对象 -->
  <association property="author" javaType="pw.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="pw.Post">
    <id property="id" column="post_id"/>
    <result property="subject" column="post_subject"/>
    
    <!-- select 的值为<select>标签的id -->
    <association property="author" javaType="pw.Author" select="selectAuthor"/>
    <collection property="comments" ofType="Comment">
      <id property="id" column="comment_id"/>
    </collection>
    <collection property="tags" ofType="pw.Tag" >
      <id property="id" column="tag_id"/>
    </collection>
    
    <!-- 
    鉴别器,相当于Java 语言中的 switch 语句
    resultType-- 的值为 <resultMap>标签的id
    -->
    <discriminator javaType="int" column="draft">
      <case value="1" resultType="carResult"/>
      <case value="2" resultType="truckResult">
      <result property="boxSize" column="box_size" />
      <result property="extendedCab" column="extended_cab" />
    </case>
    </discriminator>
  </collection>
  <!-- select的值为<select>标签的id -->
 <collection property="posts" javaType="ArrayList" column="id" ofType="Post" select="selectPostsForBlog"/>
</resultMap>

二、一对一关联查询

<mapper namespace="com.lcb.mapping.userMapper">  
    <!--association  一对一关联查询 -->  
    <select id="getClass" parameterType="int" resultMap="ClassesResultMap">  
        select * from class c,teacher t where c.teacher_id=t.t_id and c.c_id=#{id}  
    </select>  
 
    <resultMap type="com.lcb.user.Classes" id="ClassesResultMap">  
        <!-- 实体类的字段名和数据表的字段名映射 -->  
        <id property="id" column="c_id"/>  
        <result property="name" column="c_name"/>  
        <association property="teacher" javaType="com.lcb.user.Teacher">  
            <id property="id" column="t_id"/>  
            <result property="name" column="t_name"/>  
        </association>  
    </resultMap>  
</mapper>     

三、一对多关联查询

<mapper namespace="com.lcb.mapping.userMapper">  
    <!--collection  一对多关联查询 -->  
    <select id="getClass2" parameterType="int" resultMap="ClassesResultMap2">  
        select * from class c,teacher t,student s where c.teacher_id=t.t_id and c.c_id=s.class_id and c.c_id=#{id}  
    </select>  
 
    <resultMap type="com.lcb.user.Classes" id="ClassesResultMap2">  
        <id property="id" column="c_id"/>  
        <result property="name" column="c_name"/>  
        <association property="teacher" javaType="com.lcb.user.Teacher">  
            <id property="id" column="t_id"/>  
            <result property="name" column="t_name"/>  
        </association>  
 
        <collection property="student" ofType="com.lcb.user.Student">  
            <id property="id" column="s_id"/>  
            <result property="name" column="s_name"/>  
        </collection>  
    </resultMap>  
</mapper> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

书香水墨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值