Mybatis-XML复杂映射基础讲解

复杂映射XML方式

为了快速学习,快速上手,这里只对常用基本映射方式讲解,如需学习全特性,请参考结果映射章节结果映射章节

一对一
  • 关键标签:<association>
<!--id: 是上下文唯一id,<select resultMap="id">引用,type:类似select标签中resultType-->
<resultMap id="orderMap" type="Order">
    <!--mybatis根据id标签去重,指定id有助于提高性能,不指定使用result也可以-->
    <id property="id" column="id"/>
    <!--property:java类属性,column:SQL返回列名,可以通过SQL别名,自定义column-->
    <result property="ordertime" column="ordertime"/>
    <result property="total" column="total"/>
    <!--association 指定接收类型为一个复杂对象,javaType=指定接收数据的java对象,columnPrefix指定别名前缀,mybatis会自动将内容column追加前缀去匹配SQL结果-->
    <association property="user" javaType="User" columnPrefix="u_">
        <!--colum,id重复可以起别名-->
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="password" column="password"/>
        <result property="birthday" column="birthday"/>
    </association>
</resultMap>
<select id="findAll" resultMap="orderMap">
    SELECT
        o.*,
        u.id u_id,  <!--id和user表id重复,起别名-->
        u.username u_username, <!--起别名,u_前缀,方便映射-->
        u.`password` u_password,
        u.birthday u_birthday
    FROM orders o
                LEFT JOIN `user` u ON u.id=o.uid;
</select>

接收实体类:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值