关联关系映射

一、一对多关联关系

首先先用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
这两张表对应的model与mapper

如图:
在这里插入图片描述

实体类

OrderVo.java

package com.qwf.ssm.model.vo;

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

import java.util.List;

/**
 * @author qwf
 * @site www.qwf.com
 * @company xxx公司
 * @create 2019-11-21 22:54
 */
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.java

package com.qwf.ssm.model.vo;

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

/**
 * @author qwf
 * @site www.qwf.com
 * @company xxx公司
 * @create 2019-11-21 22:55
 */
public class OrderItemVo extends OrderItem {
    private Order order;

    public Order getOrder() {
        return order;
    }

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

OrderMapper.xml

<resultMap id="OrderVoMap" type="com.qwf.ssm.model.vo.OrderVo" >
    <result property="orderId" column="order_id"></result>
    <result property="orderNo" column="order_no"></result>
    <collection property="orderItems" ofType="com.qwf.ssm.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>
<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>

OrderMapper.java

package com.qwf.ssm.mapper;

import com.qwf.ssm.model.Order;
import com.qwf.ssm.model.vo.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 queryOrderVoByOrderId(@Param("orderId") Integer orderId);
}

OrderItemMapper.xml

<resultMap id="OrderItemVoMap" type="com.qwf.ssm.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>
        <association property="order" javaType="com.qwf.ssm.model.Order">
            <result property="orderId" column="order_id"></result>
            <result property="orderNo" column="order_no"></result>
        </association>
    </resultMap>
<select id="queryOrderItemVoByOrderItemId" 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>

OrderItemMapper.java

package com.qwf.ssm.mapper;

import com.qwf.ssm.model.OrderItem;
import com.qwf.ssm.model.vo.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 queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);

}

One2ManyService.java

package com.qwf.ssm.service;

import com.qwf.ssm.model.vo.OrderItemVo;
import com.qwf.ssm.model.vo.OrderVo;
import org.springframework.stereotype.Service;

/**
 * @author qwf
 * @site www.qwf.com
 * @company xxx公司
 * @create 2019-11-21 23:14
 */
@Service
public interface One2ManyService {
    OrderVo queryOrderVoByOrderId(Integer orderId);

    OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId);
}

One2ManyServiceImpl.java


package com.qwf.ssm.service.impl;

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

/**
 * @author qwf
 * @site www.qwf.com
 * @company xxx公司
 * @create 2019-11-21 23:25
 */
@Service
public class One2ManyServiceImpl implements One2ManyService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderItemMapper orderItemMapper;
    
    @Override
    public OrderVo queryOrderVoByOrderId(Integer orderId) {
        return orderMapper.queryOrderVoByOrderId(orderId);
    }

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

测试代码:

One2ManyServiceImplTest .java

package com.qwf.ssm.service.impl;

import com.qwf.ssm.SpringBaseTest;
import com.qwf.ssm.model.OrderItem;
import com.qwf.ssm.model.vo.OrderItemVo;
import com.qwf.ssm.model.vo.OrderVo;
import com.qwf.ssm.service.One2ManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.*;

/**
 * @author qwf
 * @site www.qwf.com
 * @company xxx公司
 * @create 2019-11-21 23:28
 */
public class One2ManyServiceImplTest extends SpringBaseTest {

    @Autowired
    private One2ManyService one2ManyService;

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

    @Test
    public void queryOrderItemVoByOrderItemId() {
        OrderItemVo orderItemVo = one2ManyService.queryOrderItemVoByOrderItemId(35);
        System.out.println(orderItemVo);
        System.out.println(orderItemVo.getOrder().getOrderNo());
    }
}

图片演示:
在这里插入图片描述
在这里插入图片描述

二、多对多关联关系

首先先用逆向生成工具生成t_hibernate_book、t_hibernate_book_category、t_hibernate_category,这两张表对应的model与mapper

实体类

HbookVo .java

package com.qwf.ssm.model.vo;

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

import java.util.List;

/**
 * @author qwf
 * @site www.qwf.com
 * @company xxx公司
 * @create 2019-11-22 0:04
 */
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 .java

package com.qwf.ssm.model.vo;

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

import java.util.List;

/**
 * @author qwf
 * @site www.qwf.com
 * @company xxx公司
 * @create 2019-11-22 0:04
 */
public class CategoryVo extends Category {
    private List<Hbook> hbooks;

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

    public void setHbooks(List<Hbook> hbooks) {
        this.hbooks = hbooks;
    }
}

HbookCategoryMapper.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.qwf.ssm.mapper.HbookCategoryMapper" >
  <resultMap id="BaseResultMap" type="com.qwf.ssm.model.HbookCategory" >
    <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>

  <resultMap id="HbookVoMap" type="com.qwf.ssm.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="categories" ofType="com.qwf.ssm.model.Category">
      <result property="categoryId" column="category_id"></result>
      <result property="categoryName" column="category_name"></result>
    </collection>
  </resultMap>

  <resultMap id="CategoryVoMap" type="com.qwf.ssm.model.vo.CategoryVo" >
    <result property="categoryId" column="category_id"></result>
    <result property="categoryName" column="category_name"></result>
    <collection property="hbooks" ofType="com.qwf.ssm.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 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.qwf.ssm.model.HbookCategory" >
    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.qwf.ssm.model.HbookCategory" >
    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.qwf.ssm.model.HbookCategory" >
    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.qwf.ssm.model.HbookCategory" >
    update t_hibernate_book_category
    set bid = #{bid,jdbcType=INTEGER},
      cid = #{cid,jdbcType=INTEGER}
    where bcid = #{bcid,jdbcType=INTEGER}
  </update>

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

</mapper>

HbookCategoryMapper.java

package com.qwf.ssm.mapper;

import com.qwf.ssm.model.HbookCategory;
import com.qwf.ssm.model.vo.CategoryVo;
import com.qwf.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);

}

Many2ManyService.java

package com.qwf.ssm.service;

import com.qwf.ssm.model.vo.CategoryVo;
import com.qwf.ssm.model.vo.HbookVo;

/**
 * @author qwf
 * @site www.qwf.com
 * @company xxx公司
 * @create 2019-11-22 0:22
 */
public interface Many2ManyService {
    HbookVo queryByBookId(Integer bookId);

    CategoryVo queryByCid(Integer cid);
}

Many2ManyServiceImpl .java

package com.qwf.ssm.service;

import com.qwf.ssm.mapper.HbookCategoryMapper;
import com.qwf.ssm.model.vo.CategoryVo;
import com.qwf.ssm.model.vo.HbookVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author qwf
 * @site www.qwf.com
 * @company xxx公司
 * @create 2019-11-22 0:23
 */
@Service
public class Many2ManyServiceImpl implements Many2ManyService {
    @Autowired
    private HbookCategoryMapper hbookCategoryMapper;
    @Override
    public HbookVo queryByBookId(Integer bookId) {
        return hbookCategoryMapper.queryByBookId(bookId);
    }

    @Override
    public CategoryVo queryByCid(Integer cid) {
        return hbookCategoryMapper.queryByCid(cid);
    }
}

测试代码:

Many2ManyServiceImplTest.java

package com.qwf.ssm.service;

import com.qwf.ssm.SpringBaseTest;
import com.qwf.ssm.model.Category;
import com.qwf.ssm.model.Hbook;
import com.qwf.ssm.model.vo.CategoryVo;
import com.qwf.ssm.model.vo.HbookVo;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.*;

/**
 * @author qwf
 * @site www.qwf.com
 * @company xxx公司
 * @create 2019-11-22 0:25
 */
public class Many2ManyServiceImplTest extends SpringBaseTest {

    @Autowired
    private Many2ManyService many2ManyService;
    
    @Test
    public void queryByBookId() {
        HbookVo hbookVo =many2ManyService.queryByBookId(8);
        System.out.println(hbookVo.getCategories());
        for (Category category : hbookVo.getCategories()) {
            System.out.println(category);
        }
    }

    @Test
    public void queryByCid() {
        CategoryVo categoryVo = many2ManyService.queryByCid(8);
        System.out.println(categoryVo.getCategoryName());
        for (Hbook hbook : categoryVo.getHbooks()) {
            System.out.println(hbook.getBookName());
        }
    }

}

图片演示:
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值