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

Posted 沦陷

tags:

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

目的:

  Mybatis关系映射之一对多

  Mybatis关系映射之多对多

 


Mybatis关系映射之一对多

  • 一对多 (订单对应多个订单项)

  • 多对一  (订单项对应一个订单)

 

  其是映射关系的基层思维是一样的,只是用法不一样,今天所记录的mybatis关系映射比Hibernate要简单

  之前我记录一篇hibernate映射关系,可以移步(https://www.cnblogs.com/huangting/p/11203498.html

  今天就用订单表和订单项表来演示mybatis一对多和多对一的映射关系

  订单表 t_hibernate_order

     

 订单项表t_hibernate_order_item

 

   

  • 用mybatis-generator插件生成两张表对应的model与mapper

  

 

   创建OrderVo类继承原有的实体类

package com.ht.model;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 黄大娘
 * @company dogson有限公司
 * @create  2019-09-27 19:45
 */
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.ht.model;
/**
 * @author 黄大娘
 * @company dogson有限公司
 * @create  2019-09-27 19:51
 */
public class OrderItemVo extends OrderItem {
    private Order order;

    public Order getOrder() {
        return order;
    }

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

  OrderMapper中设置一个方法来查询两张表(一对多)

package com.ht.mapper;
import com.ht.model.Order;
import com.ht.model.OrderVo;
import org.apache.ibatis.annotations.Param;

public interface OrderMapper {
    int deleteByPrimaryKey(Integer orderId);

    int insert(Order record);

    int insertSelective(Order record);

    Order selectByPrimaryKey(Integer orderId);

    int updateByPrimaryKeySelective(Order record);

    int updateByPrimaryKey(Order record);
    //一对多
    OrderVo selectByOid(@Param("oid") Integer oid);
}
在OrderMapper.xml中配置(一对多)
 <!-- 一对多的关系 -->
 <!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
<resultMap id="OrderVoMap" type="com.ht.model.OrderVo" >
    <result property="orderId" column="order_id"></result>
    <result property="orderNo" column="order_no"></result>
    <collection property="orderItems" ofType="com.ht.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>

<!--sql语句-->
<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.java(多对一)


package com.ht.mapper;

import com.ht.model.OrderItem;
import com.ht.model.OrderItemVo;
import org.apache.ibatis.annotations.Param;

public interface OrderItemMapper {
    int deleteByPrimaryKey(Integer orderItemId);

    int insert(OrderItem record);

    int insertSelective(OrderItem record);

    OrderItem selectByPrimaryKey(Integer orderItemId);

    int updateByPrimaryKeySelective(OrderItem record);

    int updateByPrimaryKey(OrderItem record);
   //多对一
    OrderItemVo selectByOrderItemId(@Param("orderItemId") Integer orderItemId);

}

在OrderItemMapper.xml中配置(多对一)

<!-- 多对一-->
  <resultMap id="OrderItemVoMap" type="com.ht.model.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.ht.model.Order">
      <result property="orderId" column="order_id"></result>
      <result property="orderNo" column="order_no"></result>
    </association>
  </resultMap>

<!--  sql语句-->
  <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>

service层
One2ManyService接口类
package com.ht.service;
import com.ht.model.OrderItemVo;
import com.ht.model.OrderVo;

/**
 * @author 黄大娘
 * @company dogson有限公司
 * @create  2019-09-27 22:16
 */
public interface One2ManyService  {
    OrderVo selectByOid(Integer oid);

    OrderItemVo selectByOrderItemId(Integer orderItemId);
}

One2ManyServiceImpl实现service接口


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

/**
 * @author 黄大娘
 * @company dogson有限公司
 * @create  2019-09-27 22:16
 */
@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.ht.service.ipml;
import com.ht.Test.SpringBaseTest;
import com.ht.model.OrderItem;
import com.ht.model.OrderItemVo;
import com.ht.model.OrderVo;
import com.ht.service.One2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author 黄大娘
 * @company dogson有限公司
 * @create  2019-09-27 22:29
 */
public class One2ManyServiceImplTest extends SpringBaseTest {
    @Autowired
    private One2ManyService one2ManyService;

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

    }

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

    }
}

效果:

一对多:

 

 

 多对一:

 

 

 


 

Mybatis关系映射之多对多

众所周知,普通的书本分类,一个类型有多本书,一本书的类型也可以有多种,那么今天就用书本类型之间来测试多对多
书籍表t_hibernate_book

书籍类别表 t_hibernate_category

  中间表 t_hibernate_book_category

 

 

 

  •  用mybatis-generator插件生成对应的model与mapper

 

 

   HbookVo

  之前一直没注意,还是单独分一个vo文件夹去放vo实体类比较好

  

 

package com.ht.model.vo;
import com.ht.model.Category;
import com.ht.model.Hbook;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 黄大娘
 * @company dogson有限公司
 * @create  2019-09-27 22:53
 */
public class HbookVo extends Hbook {
    private List<Category> category =new ArrayList<>();

    public List<Category> getCategory() {
        return category;
    }

    public void setCategory(List<Category> category) {
        this.category = category;
    }
}
CategoryVo

package com.ht.model.vo;
import com.ht.model.Category;
import com.ht.model.Hbook;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 黄大娘
 * @company dogson有限公司
 * @create  2019-09-27 22:54
 */
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
在接口类中设置方法,以便后面测试
package com.ht.mapper;

import com.ht.model.HbookCategory;
import com.ht.model.vo.CategoryVo;
import com.ht.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;

public interface HbookCategoryMapper {
    int deleteByPrimaryKey(Integer bcid);

    int insert(HbookCategory record);

    int insertSelective(HbookCategory record);

    HbookCategory selectByPrimaryKey(Integer bcid);

    int updateByPrimaryKeySelective(HbookCategory record);

    int updateByPrimaryKey(HbookCategory record);

    HbookVo queryByBid(@Param("bid") Integer bid);

    CategoryVo queryByCid(@Param("cid") Integer cid);
}

HbookCategoryMapper.xml
<!--  配置关系 -->
  <resultMap id="HbookVoMap" type="com.ht.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.ht.model.Category">
      <result property="categoryId" column="category_id"></result>
      <result property="categoryName" column="category_name"></result>
    </collection>
  </resultMap>
  <resultMap id="CategoryVoMap" type="com.ht.model.vo.CategoryVo" >
    <result property="categoryId" column="category_id"></result>
    <result property="categoryName" column="category_name"></result>
    <collection property="hbooks" ofType="com.ht.model.Hbook">
      <result property="bookId" column="book_id"></result>
      <result property="bookName" column="book_name"></result>
      <result property="price" column="price"></result>
    </collection>
  </resultMap>

<!--  sql语句-->
  <select id="queryByBid" resultType="com.ht.model.vo.HbookVo">
    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"  resultType="com.ht.model.vo.CategoryVo">
        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>
Service层
HbookCategoryService
package com.ht.service;

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

/**
 * @author 黄大娘
 * @company dogson有限公司
 * @create  2019-09-27 22:58
 */
public interface HbookCategoryService {
    HbookVo queryByBid(Integer bid);

    CategoryVo queryByCid(Integer cid);
}
HbookCategoryServiceImpl

 去实现接口中的方法

package com.ht.service.ipml;
import com.ht.mapper.HbookCategoryMapper;
import com.ht.model.vo.CategoryVo;
import com.ht.model.vo.HbookVo;
import com.ht.service.HbookCategoryService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author 黄大娘
 * @company dogson有限公司
 * @create  2019-09-27 22:59
 */
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.ht.service.ipml;

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

/**
 * @author 黄大娘
 * @company dogson有限公司
 * @create  2019-09-27 23:00
 */
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之关联关系(一对多多对多)的主要内容,如果未能解决你的问题,请参考以下文章

mybatis 详解------一对一对多多对多

Mybatis 一对一对多多对多

mybatis3.2.7应用_高级映射(一对一对多多对多)

EF基础知识小记五(一对多多对多处理)

MYBATIS05_表结构关系一对一一对多多对多延迟加载

数据库设计(一对一对多多对多)