【意识流_Mybatis】关联映射

关联映射

在进行多表查询的时候,表和表的关系有一对一、一对多、和多对多的关系,如何用mybatis匹配他们之间的关系,查询出需要的数据,这就是关联映射所要解决的问题

构建表

需要演示关联映射,第一步先得构建表
在这里插入图片描述

分析表间关系

在这里插入图片描述

mapper文件resultMap的配置

需要进行关联映射,就需要对返回值resultMap进行配置,前面我们在查询单表的时候,如果查询出来表中的字段和实体类的属性值不匹配可以使用resultMap进行配置,在这里resultMap将是我们需要重点配置的东西

  • 一对一
    如果是两张表的一对一关系,我们可以用前面我们最开始使用的扩展类来解决这个问题。如果同时查询显示两个表中的内容,可以在一个实体类中扩展另一个实体类中的字段,然后将扩展后的实体类作为返回值进行处理,也就是说这里的返回值还是使用了以前使用的resultType
    • 扩展类
public class OrdersExpend  extends  Orders{

    private String username;
    private String sex;
    private String address;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}
  • association
    在mybatis的关联映射里面提供了association标签来支持多表的一对一关系,而这里就需要使用resultMap
  <resultMap id="xxx" type="xxx.Orders">
      <id column="id" property="id"/>
      <result column="user_id" property="userid"/>
      <result column="number" property="number"/>
      <result column="createtime" property="createtime"/>
      <result column="note" property="note"/>
      <!--1:1订单关联的用户信息-->
      <association property="user" javaType="xxx.User">
          <id column="user_id" property="id"/>
          <result column="username" property="username"/>
          <result column="sex" property="sex"/>
          <result column="address" property="address"/>
      </association>
  </resultMap>

上面resultMap的id就是你在statement中需要使用的id名,type就是主表的实体类的类名(相对路径),association中的property就是在实体类中添加的另一个实体类的类名,后面javaType是该类的相对路径

  • statement
   <select id="xxx" resultMap="xxx">
      select
          orders.*,
          user.username,
          user.sex,
          user.address
      from
          orders,user
      where
          orders.user_id = user.id;
  </select>

这里的id后面动态实例化对象mapper所要调用的方法名,resultMap与上面的对应

  • 一对多
    一对多就是在一张表的的字段在另一张表中可能出现多个数据与之对应,这时候要使用collection
    <resultMap id="xxx" type="xxx.Orders">
        <id column="id" property="id"/>
        <result column="number" property="number"/>
        <result column="createtime" property="createtime"/>
        <result column="note" property="note"/>
        <!--1:m-->
        <collection property="orderdetailList" ofType="xxx.Orderdetail">
            <id column="orderdtail_id" property="id"/>
            <result column="orderdetail_itemsId" property="itemsId"/>
            <result column="orderdetail_itemsNum" property="itemsNum"/>
            <result column="orders_id" property="ordersId"/>
        </collection>
    </resultMap>

其中与一对一不同的是,使用了collection它的类型要用ofType定义,其中collection的property是在实体类中添加的另一个关联实体类的属性名

    <select id="xxx" resultMap="xxx">
       select
        orders.*,
        orderdetail.id as orderdtail_id,
        orderdetail.items_id as orderdetail_itemsId,
        orderdetail.items_num as orderdetail_itemsNum,
        orderdetail.orders_id
    from
	    orders,orderdetail
    where
	    orderdetail.orders_id = orders.id;
    </select>
  • 多对多
    多对多其实就是对一对多(collection)的嵌套

根据上面分析的关系发现user表和items表是多对多的关系,但是它们本身直接直接没有关系,这就需要其它两张表作为媒介进行关联

    <resultMap id="xxx" type="xxx.User">
        <!--用户信息-->
        <id column="user_id" property="id"/>
        <result column="username" property="username"/>
        <result column="sex" property="sex"/>
        <result column="address" property="address"/>
        <!--订单信息-->
        <collection property="ordersList" ofType="xxx.Orders">
            <id column="id" property="id"/>
            <result column="user_id" property="userid"/>
            <result column="number" property="number"/>
            <result column="createtime" property="createtime"/>
            <result column="note" property="note"/>
            <!--订单明细 1: m-->
            <collection property="orderdetailList" ofType="xxx.Orderdetail">
                <id column="orderdtail_id" property="id"/>
                <result column="orderdetail_itemsId" property="itemsId"/>
                <result column="orderdetail_itemsNum" property="itemsNum"/>
                <result column="orders_id" property="ordersId"/>
                <!--商品信息-->
                <association property="items" javaType="xxx.Items">
                    <id column="itemsId" property="id"/>
                    <result column="itemsName" property="name"/>
                    <result column="itemsPrice" property="price"/>
                    <result column="itemsCreateTime" property="createtime"/>
                </association>
            </collection>
        </collection>
    </resultMap>

如何进行关联,很重要一点就是表和表之间要先有关系,关系在哪里体现,就是它们的主外键关系

再对上面的配置进行分析:resultMap的第二行<id column="user_id" property="id"/>是user表的主键;user表又与订单表是一对多关系,而两个表的关联又是user表的主键id是orders表的外键user_id,而底下collection的第二行<id column="id" property="id"/>,同样对于后面表的分析也是如此,column是查出来表的字段名,property是对应所在实体类中的属性名,以此来达到关联的目的

<select id="xxx" resultMap="xxx">
       select
            orders.*,
            user.username,
            user.sex,
            user.address,
            orderdetail.id as orderdtail_id,
            orderdetail.items_id as orderdetail_itemsId,
            orderdetail.items_num as orderdetail_itemsNum,
            orderdetail.orders_id,
            items.id as itemsId,
            items.name as itemsName,
            items.price as itemsPrice,
            items.createtime as itemsCreateTime
        from
	        orders,user,orderdetail,items
        where
            orders.user_id = user.id 
        and 
            orderdetail.orders_id = orders.id 
        and 
            orderdetail.items_id = items.id;
</select>

同样这里的id是所要调用的方法名,resultMap与上面对应

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值