Mybatis(关联关系映射)

目录

1.一对多关联关系

2.多对多关联关系

3.总结


1.一对多关联关系

Order

package com.xbb.ssm.model;
 
import lombok.Data;
 
//@Data相当于添加了set、get方法,同时添加了tostring方法
//@AllArgsConstructor
//@NoArgsConstructor
@Data
public class Order {
    private Integer orderId;
 
    private String orderNo;
 
 
}

OrderItem:

package com.xbb.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.xbb.ssm.model.vo;
 
import com.xbb.ssm.model.Order;
import com.xbb.ssm.model.OrderItem;
 
import java.util.List;
 

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.xbb.ssm.model.vo;
 
import com.xbb.ssm.model.Order;
import com.xbb.ssm.model.OrderItem;
 

public class OrderItemVo extends OrderItem {
    private Order order;
 
    public Order getOrder() {
        return order;
    }
 
    public void setOrder(Order order) {
        this.order = order;
    }
}

OrderBiz:

package com.xbb.ssm.biz;
 
import com.xbb.ssm.model.Order;
import com.xbb.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:


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;
    }
}

2.多对多关联关系

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.xbb.ssm.mapper;
 
import com.xbb.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.xbb.ssm.mapper;
 
import com.xbb.ssm.model.HbookCategory;
import com.xbb.ssm.model.vo.CategoryVo;
import com.xbb.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.xbb.ssm.mapper;
 
import com.xbb.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.xbb.ssm.model.vo;
 
import com.xbb.ssm.model.Category;
import com.xbb.ssm.model.Hbook;
 
import java.util.List;
  
public class HbookVo extends Hbook {
    private List<Category> categories;
 
    public List<Category> getCategories() {
        return categories;
    }
 
    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }
}
HbookCategoryBiz:
package com.xbb.ssm.biz;
 
import com.xbb.ssm.model.HbookCategory;
import com.xbb.ssm.model.vo.CategoryVo;
import com.xbb.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.xbb.ssm.biz.impl;
 
import com.xbb.ssm.biz.HbookCategoryBiz;
import com.xbb.ssm.mapper.HbookCategoryMapper;
import com.xbb.ssm.model.HbookCategory;
import com.xbb.ssm.model.vo.CategoryVo;
import com.xbb.ssm.model.vo.HbookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 

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

3.总结

一对多的关系 
         property: 指的是集合属性的值, ofType:指的是集合中元素的类型 

多对一的关系
        property: 指的是属性的值, javaType:指的是属性的类型

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值