Mybatis关联关系映射

目录

一、一对多关联关系

二、多对多关联关系


一、一对多关联关系

首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item

这两张表对应的model与mapper

Model: 

Order:

package com.zwc.ssm.model;

import lombok.Data;

//@Data相当于添加了set、get方法,同时添加了tostring方法
//@AllArgsConstructor
//@NoArgsConstructor
@Data
public class Order {
    private Integer orderId;

    private String orderNo;


}

OrderItem:

package com.zwc.ssm.model;

import lombok.ToString;

@ToString
public class OrderItem {
    private Integer orderItemId;

    private Integer productId;

    private Integer quantity;

    private Integer oid;

    public OrderItem(Integer orderItemId, Integer productId, Integer quantity, Integer oid) {
        this.orderItemId = orderItemId;
        this.productId = productId;
        this.quantity = quantity;
        this.oid = oid;
    }

    public OrderItem() {
        super();
    }

    public Integer getOrderItemId() {
        return orderItemId;
    }

    public void setOrderItemId(Integer orderItemId) {
        this.orderItemId = orderItemId;
    }

    public Integer getProductId() {
        return productId;
    }

    public void setProductId(Integer productId) {
        this.productId = productId;
    }

    public Integer getQuantity() {
        return quantity;
    }

    public void setQuantity(Integer quantity) {
        this.quantity = quantity;
    }

    public Integer getOid() {
        return oid;
    }

    public void setOid(Integer oid) {
        this.oid = oid;
    }
}

OderVo:

package com.zwc.ssm.model.vo;

import com.zwc.ssm.model.Order;
import com.zwc.ssm.model.OrderItem;

import java.util.List;

/**
 * @authorzwc
 * @site www.javazwc.com
 * @company xxx公司
 * @create  2022-08-15 19:03
 * vo类不仅仅包含当前表信息,还能够包含相关联表的信息
 *
 * 当前订单会有一个或多个订单项,多个订单项必须要集合进行接受
 */
public class OrderVo extends Order {
//    通过订单号查询订单的详细信息及对应的所有订单项信息

      private List<OrderItem> orderItems;

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

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

OrderItemVo:

package com.zwc.ssm.model.vo;

import com.zwc.ssm.model.Order;
import com.zwc.ssm.model.OrderItem;

/**
 * @authorzwc
 * @site www.javazwc.com
 * @company xxx公司
 * @create  2022-08-15 19:08
 *
 * 某一个订单项一定属于某个订单的
 */
public class OrderItemVo extends OrderItem {
    private Order order;

    public Order getOrder() {
        return order;
    }

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

OrderMapper.xml:

OrderItemMapper.xml:

需求一:通过订单号查询本次订单的详细信息及对应的所有订单详细

OrderBiz:

package com.zwc.ssm.biz;

import com.zwc.ssm.model.Order;
import com.zwc.ssm.model.vo.OrderVo;

public interface OrderBiz {
    int deleteByPrimaryKey(Integer orderId);

    int insert(Order record);

    int insertSelective(Order record);

    Order selectByPrimaryKey(Integer orderId);

    OrderVo queryOrderVoByOrderId(Integer orderId);

    int updateByPrimaryKeySelective(Order record);

    int updateByPrimaryKey(Order record);
}

 OrderBizimpl:


 * @authorzwc
 * @site www.javazwc.com
 * @company xxx公司
 * @create  2022-08-15 19:32
 */
@Service
public class OrderBizImpl implements OrderBiz {
    @Autowired

    private OrderMapper orderMapper;

    @Override
    public int deleteByPrimaryKey(Integer orderId) {
        return 0;
    }

    @Override
    public int insert(Order record) {
        return 0;
    }

    @Override
    public int insertSelective(Order record) {
        return 0;
    }

    @Override
    public Order selectByPrimaryKey(Integer orderId) {
        return null;
    }

    @Override
    public OrderVo queryOrderVoByOrderId(Integer orderId) {
        return orderMapper.queryOrderVoByOrderId(orderId);
    }

    @Override
    public int updateByPrimaryKeySelective(Order record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Order record) {
        return 0;
    }
}

OrderBizimplTest: 

package com.zwc.ssm.biz.impl;

import com.zwc.ssm.biz.OrderBiz;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @authorzwc
 * @site www.javazwc.com
 * @company xxx公司
 * @create  2022-08-15 19:33
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
public class OrderBizImplTest {

    @Autowired
    private OrderBiz orderBiz;

    @Test
    public void queryOrderVoByOrderId() {
        System.out.println(orderBiz.queryOrderVoByOrderId(9));
    }
}

运行结果: 

需求2:通过订单项ID查询出订单项详细信息及所属订单

OrderitemBIz:

package com.zwc.ssm.biz;

import com.zwc.ssm.model.OrderItem;
import com.zwc.ssm.model.vo.OrderItemVo;
import org.apache.ibatis.annotations.Param;

public interface OrderItemBiz {
    int deleteByPrimaryKey(Integer orderItemId);

    int insert(OrderItem record);

    int insertSelective(OrderItem record);

    OrderItem selectByPrimaryKey(Integer orderItemId);

    OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId);

    int updateByPrimaryKeySelective(OrderItem record);

    int updateByPrimaryKey(OrderItem record);
}

 OrderItemBizimpl:

package com.zwc.ssm.biz.impl;

import com.zwc.ssm.biz.OrderItemBiz;
import com.zwc.ssm.mapper.OrderItemMapper;
import com.zwc.ssm.model.OrderItem;
import com.zwc.ssm.model.vo.OrderItemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @authorzwc
 * @site www.javazwc.com
 * @company xxx公司
 * @create  2022-08-15 20:34
 */
@Service
public class OrderItemBizImpl implements OrderItemBiz {
    @Autowired

    private OrderItemMapper orderItemMapper;

    @Override
    public int deleteByPrimaryKey(Integer orderItemId) {
        return 0;
    }

    @Override
    public int insert(OrderItem record) {
        return 0;
    }

    @Override
    public int insertSelective(OrderItem record) {
        return 0;
    }

    @Override
    public OrderItem selectByPrimaryKey(Integer orderItemId) {
        return null;
    }

    @Override
    public OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId) {
        return orderItemMapper.queryOrderItemVoByOrderItemId(orderItemId);
    }

    @Override
    public int updateByPrimaryKeySelective(OrderItem record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(OrderItem record) {
        return 0;
    }
}

OrderItemBizimplTest:

package com.zwc.ssm.biz.impl;

import com.zwc.ssm.biz.OrderItemBiz;
import com.zwc.ssm.model.vo.OrderItemVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @authorzwc
 * @site www.javazwc.com
 * @company xxx公司
 * @create  2022-08-15 21:13
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
public class OrderItemBizImplTest {
@Autowired
    private OrderItemBiz orderItemBiz;

    @Test
    public void queryOrderItemVoByOrderItemId() {
        OrderItemVo orderItemVo = orderItemBiz.queryOrderItemVoByOrderItemId(9);
        System.out.println(orderItemVo);
      
    }
}

运行结果: 

 

package com.zwc.ssm.biz.impl;

import com.zwc.ssm.biz.OrderItemBiz;
import com.zwc.ssm.model.vo.OrderItemVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @authorzwc
 * @site www.javazwc.com
 * @company xxx公司
 * @create  2022-08-15 21:13
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
public class OrderItemBizImplTest {
@Autowired
    private OrderItemBiz orderItemBiz;

    @Test
    public void queryOrderItemVoByOrderItemId() {
        OrderItemVo orderItemVo = orderItemBiz.queryOrderItemVoByOrderItemId(43);
        System.out.println(orderItemVo);
        //通过订单详细拿到订单
        System.out.println(orderItemVo.getOrder());

    }
}

运行结果: 

二、多对多关联关系

HbookCategoryMapper:

<resultMap id="HbookVoMap" type="com.zwc.ssm.model.vo.HbookVo">
    <result property="bookId" column="book_id"></result>
    <result property="bookName" column="book_name"></result>
    <collection property="categories" ofType="com.zwc.ssm.model.Category">
      <result property="categoryId" column="category_id"></result>
      <result property="categoryName" column="category_name"></result>
    </collection>
  </resultMap>

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

  <select id="queryByBookId" 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 = #{bookId}
 </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>

HbookMapper:

package com.zwc.ssm.mapper;

import com.zwc.ssm.model.Hbook;

public interface HbookMapper {
    int deleteByPrimaryKey(Integer bookId);

    int insert(Hbook record);

    int insertSelective(Hbook record);

    Hbook selectByPrimaryKey(Integer bookId);

    int updateByPrimaryKeySelective(Hbook record);

    int updateByPrimaryKey(Hbook record);
}
HbookCategoryMapper:
package com.zwc.ssm.mapper;

import com.zwc.ssm.model.HbookCategory;
import com.zwc.ssm.model.vo.CategoryVo;
import com.zwc.ssm.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 queryByBookId(@Param("bookId") Integer bookId);

    CategoryVo queryByCid(@Param("cid") Integer cid);
}
CategoryMapper:
package com.zwc.ssm.mapper;

import com.zwc.ssm.model.Category;

public interface CategoryMapper {
    int deleteByPrimaryKey(Integer categoryId);

    int insert(Category record);

    int insertSelective(Category record);

    Category selectByPrimaryKey(Integer categoryId);

    int updateByPrimaryKeySelective(Category record);

    int updateByPrimaryKey(Category record);
}

HbookVo:

package com.zwc.ssm.model.vo;

import com.zwc.ssm.model.Category;
import com.zwc.ssm.model.Hbook;

import java.util.List;

/**
 * @authorzwc
 * @site www.javazwc.com
 * @company xxx公司
 * @create  2022-08-16 0:01
 */
public class HbookVo extends Hbook {
    private List<Category> categories;

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

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }
}
CategoryVo:
package com.zwc.ssm.model.vo;

import com.zwc.ssm.model.Category;
import com.zwc.ssm.model.Hbook;

import java.util.List;

/**
 * @authorzwc
 * @site www.javazwc.com
 * @company xxx公司
 * @create  2022-08-16 0:03
 */
public class CategoryVo  extends Category {
    private List<Hbook> hbooks;

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

    public void setHbooks(List<Hbook> hbooks) {
        this.hbooks = hbooks;
    }
}
HbookCategoryBiz:
package com.zwc.ssm.biz;

import com.zwc.ssm.model.HbookCategory;
import com.zwc.ssm.model.vo.CategoryVo;
import com.zwc.ssm.model.vo.HbookVo;

public interface HbookCategoryBiz {
    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 queryByBookId(Integer bookId);

    CategoryVo queryByCid(Integer cid);
}
HbookCategoryBizImpl:
package com.zwc.ssm.biz.impl;

import com.zwc.ssm.biz.HbookCategoryBiz;
import com.zwc.ssm.mapper.HbookCategoryMapper;
import com.zwc.ssm.model.HbookCategory;
import com.zwc.ssm.model.vo.CategoryVo;
import com.zwc.ssm.model.vo.HbookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @authorzwc
 * @site www.javazwc.com
 * @company xxx公司
 * @create  2022-08-16 0:09
 */
@Service
public class HbookCategoryBizImpl implements HbookCategoryBiz {
    @Autowired

    private HbookCategoryMapper hbookCategoryMapper;

    @Override
    public int deleteByPrimaryKey(Integer bcid) {
        return 0;
    }

    @Override
    public int insert(HbookCategory record) {
        return 0;
    }

    @Override
    public int insertSelective(HbookCategory record) {
        return 0;
    }

    @Override
    public HbookCategory selectByPrimaryKey(Integer bcid) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(HbookCategory record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(HbookCategory record) {
        return 0;
    }

    @Override
    public HbookVo queryByBookId(Integer bookId) {

        return hbookCategoryMapper.queryByBookId(bookId);
    }

    @Override
    public CategoryVo queryByCid(Integer cid) {

        return hbookCategoryMapper.queryByCid(cid);
    }
}

 

需求一:根据书籍ID,查询书籍信息及所属所有类别信息

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 = #{bookId}
HbookCategoryBizImplTest:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
public class HbookCategoryBizImplTest {
    @Autowired

    private HbookCategoryBiz hbookCategoryBiz;

    @Test
    public void queryByBookId() {
        HbookVo hbookVo = hbookCategoryBiz.queryByBookId(8);
        System.out.println(hbookVo);
        hbookVo.getCategories().forEach(System.out::println);
    }

 运行结果

需求二:根据类别ID,查询类别信息及所包含书籍信息 

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}
package com.zwc.ssm.biz.impl;

import com.zwc.ssm.biz.HbookCategoryBiz;
import com.zwc.ssm.model.vo.CategoryVo;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * @authorzwc
 * @site www.javazwc.com
 * @company xxx公司
 * @create  2022-08-16 0:11
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext-mybatis.xml"})
public class HbookCategoryBizImplTest {
    @Autowired

    private HbookCategoryBiz hbookCategoryBiz;

   

    @Test
    public void queryByCid() {
        CategoryVo categoryVo = hbookCategoryBiz.queryByCid(8);
        System.out.println(categoryVo);
        categoryVo.getHbooks().forEach(System.out::println);
    }
}

运行结果: 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值