Mybatis关联关系

Posted Renduo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis关联关系相关的知识,希望对你有一定的参考价值。

一对多

这里用到了两张表(订单表以及订单项表)

订单表:

 

订单项表:

一对多无非就是一个订单对应多个订单项

 

我们先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item

 

<table schema="" tableName="t_hibernate_order" domainObjectName="Order"
               enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false">

        </table>

        <table schema="" tableName="t_hibernate_order_item" domainObjectName="OrderItem"
               enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false">
        </table>

 

 

 

OrderVo:

 

package com.psy.model.vo;

import com.psy.model.Order;
import com.psy.model.OrderItem;

import java.util.ArrayList;
import java.util.List;

public class OrderVo extends Order {
    private List<OrderItem> orderItems =new ArrayList<>();

    public List<OrderItem> getOrderItems() {
        return orderItems;
    }

    public void setOrderItems(List<OrderItem> orderItems) {
        this.orderItems = orderItems;
    }
}

 

OrderItemVo:

 

package com.psy.model.vo;

import com.psy.model.Order;
import com.psy.model.OrderItem;

public class OrderItemVo extends OrderItem {
    private Order order;

    public Order getOrder() {
        return order;
    }

    public void setOrder(Order order) {
        this.order = order;
    }
}

 


OrderMapper:
OrderVo selectByOid(@Param("oid") Integer oid);
OrderMapper.xml(订单是一):
<resultMap id="OrderVoMap" type="com.psy.model.vo.OrderVo" >
    <result property="orderId" column="order_id"></result>
    <result property="orderNo" column="order_no"></result>
    <collection property="orderItems" ofType="com.psy.model.OrderItem">
      <result property="orderItemId" column="order_item_id"></result>
      <result property="productId" column="product_id"></result>
      <result property="quantity" column="quantity"></result>
      <result property="oid" column="oid"></result>
    </collection>
  </resultMap>

<select id="selectByOid" resultMap="OrderVoMap" parameterType="java.lang.Integer">
select * from t_hibernate_order o,t_hibernate_order_item oi
where o.order_id = oi.oid
and o.order_id = #{oid}
</select>

OrderItemMapper:

 

OrderItemVo selectByOrderItemId(@Param("orderItemId") Integer orderItemId);

 

OrderItemMapper.xml (订单项是多):

 

<resultMap id="OrderItemVoMap" type="com.psy.model.vo.OrderItemVo" >
    <result property="orderItemId" column="order_item_id"></result>
    <result property="productId" column="product_id"></result>
    <result property="quantity" column="quantity"></result>
    <result property="oid" column="oid"></result>
    <association property="order" javaType="com.psy.model.Order">
      <result property="orderId" column="order_id"></result>
      <result property="orderNo" column="order_no"></result>
    </association>
  </resultMap>

<select id="selectByOrderItemId" resultMap="OrderItemVoMap" parameterType="java.lang.Integer">
    select * from t_hibernate_order o,t_hibernate_order_item oi
    where o.order_id = oi.oid
    and oi.order_item_id = #{orderItemId}
  </select>

 

One2ManyService:
package com.psy.service;

import com.psy.model.vo.OrderItemVo;
import com.psy.model.vo.OrderVo;

public interface One2ManyService {
    OrderVo selectByOid(Integer oid);

    OrderItemVo selectByOrderItemId(Integer orderItemId);
}

One2ManyServiceImpl:

package com.psy.service.impl;

import com.psy.mapper.OrderItemMapper;
import com.psy.mapper.OrderMapper;
import com.psy.model.vo.OrderItemVo;
import com.psy.model.vo.OrderVo;
import com.psy.service.One2ManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class One2ManyServiceImpl implements One2ManyService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public OrderVo selectByOid(Integer oid) {
        return orderMapper.selectByOid(oid);
    }

    @Override
    public OrderItemVo selectByOrderItemId(Integer orderItemId) {

        return orderItemMapper.selectByOrderItemId(orderItemId);
    }
}

创建测试类One2ManyServiceImplTest:

 

package com.psy.service.impl;

import com.psy.SpringBaseTest;
import com.psy.model.OrderItem;
import com.psy.model.vo.OrderItemVo;
import com.psy.model.vo.OrderVo;
import com.psy.service.One2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class One2ManyServiceImplTest extends SpringBaseTest {

    @Autowired
    private One2ManyService one2ManyService;

    @Test
    public void selectByOid() {
        OrderVo orderVo = one2ManyService.selectByOid(1);
        System.out.println(orderVo);
        for (OrderItem orderItem : orderVo.getOrderItems()) {
            System.out.println(orderItem);
        }
    }

    @Test
    public void selectByOrderItemId() {
        OrderItemVo orderItemVo = one2ManyService.selectByOrderItemId(1);
        System.out.println(orderItemVo);
        System.out.println(orderItemVo.getOrder());
    }
}

 

测试结果:

 

 

 

 


 

多对多

用到三张表(书籍表、书籍类别表、中间表)

 

 

多对多就是指一本书可以有多个类别,一个类别又可以对应多本书

 

同样的先使用逆向工程生成

<table schema="" tableName="t_hibernate_book" domainObjectName="Hbook"
               enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false">

        </table>

        <table schema="" tableName="t_hibernate_category" domainObjectName="Category"
               enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false">
        </table>

        <table schema="" tableName="t_hibernate_book_category" domainObjectName="HbookCategory"
               enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false">
        </table>

HbookVo:

package com.psy.model.vo;

import com.psy.model.Hbook;

import java.util.ArrayList;
import java.util.List;

public class HbookVo extends Hbook {
    private List categories =new ArrayList<>();

    public List getCategories() {
        return categories;
    }

    public void setCategories(List categories) {
        this.categories = categories;
    }
}

CategoryVo:

package com.psy.model.vo;

import com.psy.model.Category;
import com.psy.model.Hbook;

import java.util.ArrayList;
import java.util.List;

public class CategoryVo extends Category {
    private List<Hbook> hbooks =new ArrayList<>();

    public List<Hbook> getHbooks() {
        return hbooks;
    }

    public void setHbooks(List<Hbook> hbooks) {
        this.hbooks = hbooks;
    }
}

 

HbookCategoryMapper:
 HbookVo queryByBid(@Param("bid")Integer bid);
    CategoryVo queryByCid(@Param("cid") Integer cid);

HbookCategoryMapper.xml:
<resultMap id="HbookVoMap" type="com.psy.model.vo.HbookVo" >
    <result property="bookId" column="book_id"></result>
    <result property="bookName" column="book_name"></result>
    <result property="price" column="price"></result>
    <collection property="category" ofType="com.psy.model.Category">
      <result property="categoryId" column="category_id"></result>
      <result property="categoryName" column="category_name"></result>
    </collection>
  </resultMap>

  <resultMap id="CategoryVoMap" type="com.psy.model.vo.CategoryVo" >
    <result property="categoryId" column="category_id"></result>
    <result property="categoryName" column="category_name"></result>
    <collection property="hbooks" ofType="com.psy.model.Hbook">
      <result property="bookId" column="book_id"></result>
      <result property="bookName" column="book_name"></result>
      <result property="price" column="price"></result>
    </collection>
  </resultMap>

<select id="queryByBid" resultMap="HbookVoMap" parameterType="java.lang.Integer">
    select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
     where b.book_id = bc.bid and bc.cid = c.category_id and b.book_id = #{bid}

  </select>
  <select id="queryByCid" resultMap="CategoryVoMap" parameterType="java.lang.Integer">
    select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
     where b.book_id = bc.bid and bc.cid = c.category_id and c.category_id=#{cid}
  </select>
HbookCategoryService:
package com.psy.service;

import com.psy.model.vo.CategoryVo;
import com.psy.model.vo.HbookVo;

public interface HbookCategoryService {
    HbookVo queryByBid(Integer bid);

    CategoryVo queryByCid(Integer cid);
}

HbookCategoryServiceImpl:
package com.psy.service.impl;

import com.psy.mapper.HbookCategoryMapper;
import com.psy.model.vo.CategoryVo;
import com.psy.model.vo.HbookVo;
import com.psy.service.HbookCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class HbookCategoryServiceImpl implements HbookCategoryService {
    @Autowired
    private HbookCategoryMapper hbookCategoryMapper;

    @Override
    public HbookVo queryByBid(Integer bid) {
        return hbookCategoryMapper.queryByBid(bid);
    }

    @Override
    public CategoryVo queryByCid(Integer cid) {

        return hbookCategoryMapper.queryByCid(cid);
    }
}

创建测试类HbookCategoryServiceImplTest:

 

package com.psy.service.impl;

import com.psy.SpringBaseTest;
import com.psy.model.Category;
import com.psy.model.Hbook;
import com.psy.model.vo.CategoryVo;
import com.psy.model.vo.HbookVo;
import com.psy.service.HbookCategoryService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

public class HbookCategoryServiceImplTest extends SpringBaseTest {

    @Autowired
    private HbookCategoryService hbookCategoryService;

    @Test
    public void queryByBid() {
        HbookVo hbookVo = hbookCategoryService.queryByBid(1);
        System.out.println(hbookVo);

        for (Category category : hbookVo.getCategory()) {
            System.out.println(category);
        }

    }

    @Test
    public void queryByCid() {
        CategoryVo categoryVo = hbookCategoryService.queryByCid(1);
        System.out.println(categoryVo);
        for (Hbook hbook : categoryVo.getHbooks()) {
            System.out.println(hbook);
        }
    }
    }

 

测试结果:

 

 


 
 

 

以上是关于Mybatis关联关系的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot与Mybatis整合(包含generate自动生成代码工具,数据库表一对一,一对多,关联关系中间表的查询)

MyBatis_关联关系查询

mybatis一对一关联关系映射

Mybatis之关联关系(一对多多对多)

mybatis xml数据层框架应用--Mybatis关系映射之一对多关系映射

mybatis xml数据层框架应用--Mybatis关系映射之一对一关系映射