您的当前位置:首页正文

关联关系映射

2024-12-02 来源:个人技术集锦


一、一对多

注: property: 指的是属性的值, ofType:指的是集合中元素的类型

public class Order {
    private Integer orderId;
 
    private String orderNo;
 
    public Order(Integer orderId, String orderNo) {
        this.orderId = orderId;
        this.orderNo = orderNo;
    }
 
    public Order() {
        super();
    }
 
    public Integer getOrderId() {
        return orderId;
    }
 
    public void setOrderId(Integer orderId) {
        this.orderId = orderId;
    }
 
    public String getOrderNo() {
        return orderNo;
    }
 
    public void setOrderNo(String orderNo) {
        this.orderNo = orderNo;
    }
}

OrderVo类

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

OrderItem类

package com.ssm.model;
 
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;
    }
}

配置文件

<resultMap id="OrderVoMap" type="com.javaxl.ssm.model.vo.OrderVo" >
  <result property="orderId" column="order_id"></result>
  <result property="orderNo" column="order_no"></result>
  <collection property="orderItems" ofType="com.javaxl.ssm.model.OrderItem">
    <result property="orderItemId" column="order_item_id"></result>
    <result property="oid" column="oid"></result>
    <result property="productId" column="product_id"></result>
    <result property="quantity" column="quantity"></result>
  </collection>
</resultMap>
 
<select id="queryOrderVoByOrderId" 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 = #{orderId}
</select>

 更新实体类

package com.ssm.model;
 
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
 
/**
 * 对应t_hibernate_order表
 */
//相当于添加了set/get方法 同时添加了toSpring方法
//@NoArgsConstructor
//@AllArgsConstructor
@Data
public class Order {
    private Integer orderId;
 
    private String orderNo;
 
}

 一对多:

<resultMap id="OrderVoMap" type="com.javaxl.ssm.model.vo.OrderItemVo" >
  <result property="orderItemId" column="order_item_id"></result>
  <result property="oid" column="oid"></result>
  <result property="productId" column="product_id"></result>
  <result property="quantity" column="quantity"></result>
  <association property="order" javaType="com.javaxl.ssm.model.Order">
    <result property="orderId" column="order_id"></result>
    <result property="orderNo" column="order_no"></result>
  </association>
</resultMap>
 
<select id="queryOrderItemVoByOrderItemId" 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.order_item_id = #{orderItemId}
</select>
package com.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;
    }
}

接口类

package com.zxw.ssm.mapper;
 
import com.zxw.ssm.model.OrderItem;
import com.zxw.ssm.model.vo.OrderItemVo;
import org.apache.ibatis.annotations.Param;
 
public interface OrderItemMapper {
 
    OrderItemVo queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
 
}

实现类

package com.zxw.ssm.Biz;
 
import com.zxw.ssm.mapper.OrderItemMapper;
import com.zxw.ssm.model.OrderItem;
import com.zxw.ssm.model.vo.OrderItemVo;
import org.springframework.beans.factory.annotation.Autowired;
 
 
@Service
public class OrderItemBizImpl implements OrderItemBiz {
    @Autowired
    private OrderItemMapper orderItemMapper;
 
 
    @Override
    public OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId) {
        return orderItemMapper.queryOrderItemVoByOrderItemId(orderItemId);
    }
 
 
 
}

测试

package com.ssm.Biz.impl;
 
import com.ssm.Biz.OrderItemBiz;
import com.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;
 
import static org.junit.Assert.*;
 
 
@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());
    }
}

效果

二、多对多 

注:property: 指的是属性的值, javaType:指的是属性的类型

配置文件
 

<resu

ltMap id="HbookVoMap" type="com.dengxiyan.ssm.model.vo.HbookVo">
    <result property="bookId" column="book_id"></result>
    <result property="bookName" column="book_name"></result>
    <collection property="categories" ofType="com.dengxiyan.ssm.model.Category">
      <result property="categoryId" column="category_id"></result>
      <result property="categoryName" column="category_name"></result>
    </collection>
  </resultMap>
 
  <resultMap id="CategoryVoMap" type="com.dengxiyan.ssm.model.vo.CategoryVo">
    <result property="categoryId" column="category_id"></result>
    <result property="categoryName" column="category_name"></result>
    <collection property="hbooks" ofType="com.dengxiyan.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>

实体类

package com.ssm.model;
 
import lombok.ToString;
 
@ToString
public class Hbook {
    private Integer bookId;
 
    private String bookName;
 
    private Float price;
 
    public Hbook(Integer bookId, String bookName, Float price) {
        this.bookId = bookId;
        this.bookName = bookName;
        this.price = price;
    }
 
    public Hbook() {
        super();
    }
 
    public Integer getBookId() {
        return bookId;
    }
 
    public void setBookId(Integer bookId) {
        this.bookId = bookId;
    }
 
    public String getBookName() {
        return bookName;
    }
 
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
 
    public Float getPrice() {
        return price;
    }
 
    public void setPrice(Float price) {
        this.price = price;
    }
}

实体子类 

package com.ssm.model.vo;
 
import com.ssm.model.Category;
import com.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;
    }
}
package com.ssm.model.vo;
 
import com.ssm.model.Hbook;
 
import java.util.List;
 
 
public class CategoryVo {
    private List<Hbook> hbooks;
 
    public List<Hbook> getHbooks() {
        return hbooks;
    }
 
    public void setHbooks(List<Hbook> hbooks) {
        this.hbooks = hbooks;
    }
}

接口


@Service
public class HbookCategoryBizImpl implements HbookCategoryBiz {
   @Autowired
   private HbookCategoryMapper hbookCategoryMapper;
    
 
    public HbookVo queryByBookId(Integer bookId) {
        return hbookCategoryMapper.queryByBookId(bookId);
    }
 
    @Override
    public CategoryVo queryByCid(Integer cid) {
        return hbookCategoryMapper.queryByCid(cid);
    }
}




public interface HbookCategoryBiz {
    
 
    HbookVo queryByBookId(Integer bookId);
 
    CategoryVo queryByCid(Integer cid);
 
}

实现类


@Service
public class HbookCategoryBizImpl implements HbookCategoryBiz {
   @Autowired
 
   private HbookCategoryMapper hbookCategoryMapper;
   
 
 
    public HbookVo queryByBookId(Integer bookId) {
        return hbookCategoryMapper.queryByBookId(bookId);
    }
 
    @Override
    public CategoryVo queryByCid(Integer cid) {
        return hbookCategoryMapper.queryByCid(cid);
    }
}

 开始测试

@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);
        for (Category category : hbookVo.getCategories()) {
            System.out.println(category);
        }
    }
 
    @Test
    public void queryByCid() {
        CategoryVo categoryVo = hbookCategoryBiz.queryByCid(8);
        System.out.println(categoryVo);
        for (Hbook hbook : categoryVo.getHbooks()) {
            System.out.println(hbook);
        }
    }
}

效果 

 

显示全文