一对多与多对多

Mybatis关系映射之一对多
一对多 (订单对应多个订单项)
多对一 (订单项对应一个订单)

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

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

今天就用订单表和订单项表来演示mybatis一对多和多对一的映射关系
需要的两张表
在这里插入图片描述
在这里插入图片描述
用mybatis-generator插件生成两张表对应的model与mapper
在这里插入图片描述
创建OrderVo类继承原有的实体类

package com.hutao.model.vo;

import com.hutao.model.Order;
import com.hutao.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.hutao.model.vo;

import com.hutao.model.Order;
import com.hutao.model.OrderItem;

public class OrderItemVo extends OrderItem {
    private Order order;

    public Order getOrder() {
        return order;
    }

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

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

package com.hutao.mapper;

import com.hutao.model.Order;
import com.hutao.model.vo.OrderVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
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);

    List<OrderVo> selectByOrderId(@Param("orderId")Integer orderId);
}

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

<!-- 一对多的关系 -->
 <!-- property: 指的是集合属性的值, ofType:指的是集合中元素的类型 -->
  <resultMap id="OrderVoMap" type="com.hutao.model.vo.OrderVo">
<result property="orderId" column="order_id"></result>
<result property="orderNo" column="order_no"></result>
<!--<result property="orderItems"></result>-->
    <collection property="orderItems" ofType="com.hutao.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>
//SQL语句
  <select id="selectByOrderId" resultMap="OrderVoMap" parameterType="java.lang.Integer" >
    select * from t_hibernate_order o,t_hibernate_order_item oi where o.order_id=oi.oid
    and oi.oid=#{orderId}
  </select>

</mapper>

OrderItemMapper.java(多对一)

package com.hutao.mapper;

import com.hutao.model.OrderItem;
import com.hutao.model.vo.OrderItemVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
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);

    List<OrderItemVo> selectByOrderItemId(@Param("orderItemId")Integer orderItemId);
}

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

<resultMap id="OrderItemVoMap" type="com.hutao.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>
    <!--<result property="orderItems"></result>-->
    <association property="order" javaType="com.hutao.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>

service层
OneToManyService接口类

package com.hutao.service;

import com.hutao.model.vo.OrderItemVo;
import com.hutao.model.vo.OrderVo;
import org.apache.ibatis.annotations.Param;

import java.util.List;

public interface OneToManyService {

    List<OrderVo> selectByOrderId(Integer orderId);
    List<OrderItemVo> selectByOrderItemId(Integer orderItemId);
}

OneToManyServiceImpl实现service接口

package com.hutao.service.impl;

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

import java.util.List;
@Service
public class OneToManyServiceImpl implements OneToManyService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public List<OrderVo> selectByOrderId(Integer orderId) {
        return orderMapper.selectByOrderId(orderId);
    }

    @Override
    public List<OrderItemVo> selectByOrderItemId(Integer orderItemId) {
        return orderItemMapper.selectByOrderItemId(orderItemId);
    }
}

测试 :

OneToManyServiceImplTest

package com.hutao.service.impl;

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

        import java.util.List;
@Service
public class OneToManyServiceImpl implements OneToManyService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;

    @Override
    public List<OrderVo> selectByOrderId(Integer orderId) {
        return orderMapper.selectByOrderId(orderId);
    }

    @Override
    public List<OrderItemVo> selectByOrderItemId(Integer orderItemId) {
        return orderItemMapper.selectByOrderItemId(orderItemId);
    }
}

HbookVo

package com.hutao.model.vo;

import com.hutao.model.Category;
import com.hutao.model.Hbook;

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

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

    public List<Category> getCategories() {
        return categories;
    }

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

CategoryVo

package com.hutao.model.vo;

import com.hutao.model.Category;
import com.hutao.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;
    }
}

BookCategoryMapper
在接口类中设置方法,以便后面测试

package com.hutao.mapper;

import com.hutao.model.BookCategory;

public interface BookCategoryMapper {
    int deleteByPrimaryKey(Integer bcid);

    int insert(BookCategory record);

    int insertSelective(BookCategory record);

    BookCategory selectByPrimaryKey(Integer bcid);

    int updateByPrimaryKeySelective(BookCategory record);

    int updateByPrimaryKey(BookCategory record);
}

BookCategoryMapper.xml

  <?xml version="1.0" encoding="UTF-8" ?>
  <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  <mapper namespace="com.hutao.mapper.BookCategoryMapper" >
    <resultMap id="BaseResultMap" type="com.hutao.model.BookCategory" >
      <constructor >
        <idArg column="bcid" jdbcType="INTEGER" javaType="java.lang.Integer" />
        <arg column="bid" jdbcType="INTEGER" javaType="java.lang.Integer" />
        <arg column="cid" jdbcType="INTEGER" javaType="java.lang.Integer" />
      </constructor>
    </resultMap>
    <sql id="Base_Column_List" >
      bcid, bid, cid
    </sql>
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
      select
      <include refid="Base_Column_List" />
      from t_hibernate_book_category
      where bcid = #{bcid,jdbcType=INTEGER}
    </select>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
      delete from t_hibernate_book_category
      where bcid = #{bcid,jdbcType=INTEGER}
    </delete>
    <insert id="insert" parameterType="com.hutao.model.BookCategory" >
      insert into t_hibernate_book_category (bcid, bid, cid
        )
      values (#{bcid,jdbcType=INTEGER}, #{bid,jdbcType=INTEGER}, #{cid,jdbcType=INTEGER}
        )
    </insert>
    <insert id="insertSelective" parameterType="com.hutao.model.BookCategory" >
      insert into t_hibernate_book_category
      <trim prefix="(" suffix=")" suffixOverrides="," >
        <if test="bcid != null" >
          bcid,
        </if>
        <if test="bid != null" >
          bid,
        </if>
        <if test="cid != null" >
          cid,
        </if>
      </trim>
      <trim prefix="values (" suffix=")" suffixOverrides="," >
        <if test="bcid != null" >
          #{bcid,jdbcType=INTEGER},
        </if>
        <if test="bid != null" >
          #{bid,jdbcType=INTEGER},
        </if>
        <if test="cid != null" >
          #{cid,jdbcType=INTEGER},
        </if>
      </trim>
    </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.hutao.model.BookCategory" >
      update t_hibernate_book_category
      <set >
        <if test="bid != null" >
          bid = #{bid,jdbcType=INTEGER},
        </if>
        <if test="cid != null" >
          cid = #{cid,jdbcType=INTEGER},
        </if>
      </set>
      where bcid = #{bcid,jdbcType=INTEGER}
    </update>
    <update id="updateByPrimaryKey" parameterType="com.hutao.model.BookCategory" >
      update t_hibernate_book_category
      set bid = #{bid,jdbcType=INTEGER},
        cid = #{cid,jdbcType=INTEGER}
      where bcid = #{bcid,jdbcType=INTEGER}
    </update>
  </mapper>

Service层
ManyToManyService

package com.hutao.service;

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

public interface ManyToManyService {

    HbookVo selectByBid(Integer bid);

    CategoryVo selectByCid(Integer cid);

}

去实现接口中的方法
ManyToManyServiceimpl

package com.hutao.service.impl;

import com.hutao.mapper.CategoryMapper;
import com.hutao.mapper.HbookMapper;
import com.hutao.model.vo.CategoryVo;
import com.hutao.model.vo.HbookVo;
import com.hutao.service.ManyToManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ManyToManyServiceImpl implements ManyToManyService {

    @Autowired
    private HbookMapper hbookMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public HbookVo selectByBid(Integer bid) {
        return hbookMapper.selectByBid(bid);
    }

    @Override
    public CategoryVo selectByCid(Integer cid) {

        return categoryMapper.selectByCid(cid);
    }
}

测试:

package com.hutao.service.impl;

import com.hutao.model.Category;
import com.hutao.model.Hbook;
import com.hutao.model.vo.CategoryVo;
import com.hutao.model.vo.HbookVo;
import com.hutao.service.ManyToManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.*;

public class ManyToManyServiceImplTest extends com.hutao.SpringJunitBaseTest {

    @Autowired
    private ManyToManyService manyToManyService;

    @Test
    public void selectByBid() {
        HbookVo hbookVo = manyToManyService.selectByBid(8);
        System.out.println(hbookVo);
        for (Category category : hbookVo.getCategories()){
            System.out.println(category);
        }
    }

    @Test
    public void selectByCid() {
        CategoryVo categoryVo = this.manyToManyService.selectByCid(8);
        System.out.println(categoryVo);
        for (Hbook hbook : categoryVo.getHbooks()){
            System.out.println(hbook);
        }
    }
}

结果
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值