关联关系映射

一对多

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

如图生成:

 

 目录:

CategoryVo.java

package com.zhuling.model.vo;

import com.zhuling.model.Category;
import com.zhuling.model.Hbook;

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

/**
 * @author zhuling
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2019-10-23 20:08
 */
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;
    }
}

HBookVo.java

package com.zhuling.model.vo;

import com.zhuling.model.Category;
import com.zhuling.model.Hbook;

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

/**
 * @author zhuling
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2019-10-23 20:06
 */
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;
    }
}

OrderItemVo.java

package com.zhuling.model.vo;

import com.zhuling.model.Orde;
import com.zhuling.model.OrdeItem;

/**
 * @author zhuling
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2019-10-23 19:01
 */
public class OrderItemVo extends OrdeItem {
    private Orde orde;

    public Orde getOrde() {
        return orde;
    }

    public void setOrde(Orde orde) {
        this.orde = orde;
    }
}

OrderVo.java

package com.zhuling.model.vo;

import com.zhuling.model.Orde;
import com.zhuling.model.OrdeItem;

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

/**
 * @author zhuling
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2019-10-23 18:59
 */
public class OrderVo extends Orde {
    private List<OrdeItem> orderItems=new ArrayList<>();

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

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

CategoryMapper

package com.zhuling.mapper;

import com.zhuling.model.Category;
import com.zhuling.model.vo.CategoryVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

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

    CategoryVo selectByCid(@Param("cid") Integer cid);
}

CategoryMapper.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.zhuling.mapper.CategoryMapper" >
  <resultMap id="BaseResultMap" type="com.zhuling.model.Category" >
    <constructor >
      <idArg column="category_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="category_name" jdbcType="VARCHAR" javaType="java.lang.String" />
    </constructor>
  </resultMap>

  <resultMap id="CategoryVoMap" type="com.zhuling.model.vo.CategoryVo">
    <result property="categoryId" column="category_id"></result>
    <result property="categoryName" column="category_name"></result>
    <!--    <result property="orderItems"></result>-->
    <collection property="hbooks" ofType="com.zhuling.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" >
    category_id, category_name
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from t_hibernate_category
    where category_id = #{categoryId,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from t_hibernate_category
    where category_id = #{categoryId,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zhuling.model.Category" >
    insert into t_hibernate_category (category_id, category_name)
    values (#{categoryId,jdbcType=INTEGER}, #{categoryName,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.zhuling.model.Category" >
    insert into t_hibernate_category
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="categoryId != null" >
        category_id,
      </if>
      <if test="categoryName != null" >
        category_name,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="categoryId != null" >
        #{categoryId,jdbcType=INTEGER},
      </if>
      <if test="categoryName != null" >
        #{categoryName,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zhuling.model.Category" >
    update t_hibernate_category
    <set >
      <if test="categoryName != null" >
        category_name = #{categoryName,jdbcType=VARCHAR},
      </if>
    </set>
    where category_id = #{categoryId,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zhuling.model.Category" >
    update t_hibernate_category
    set category_name = #{categoryName,jdbcType=VARCHAR}
    where category_id = #{categoryId,jdbcType=INTEGER}
  </update>


  <select id="selectByCid" 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

package com.zhuling.mapper;

import com.zhuling.model.HbookCategory;

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

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.zhuling.mapper.HbookCategoryMapper" >
  <resultMap id="BaseResultMap" type="com.zhuling.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>
  <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.zhuling.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.zhuling.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.zhuling.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.zhuling.model.HbookCategory" >
    update t_hibernate_book_category
    set bid = #{bid,jdbcType=INTEGER},
      cid = #{cid,jdbcType=INTEGER}
    where bcid = #{bcid,jdbcType=INTEGER}
  </update>
</mapper>

HBookMapper

package com.zhuling.mapper;


import com.zhuling.model.Hbook;
import com.zhuling.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

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

    HbookVo selectByBid(@Param("bid") Integer bid);
}

HBookMapper.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.zhuling.mapper.HbookMapper" >
  <resultMap id="BaseResultMap" type="com.zhuling.model.Hbook" >
    <constructor >
      <idArg column="book_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="book_name" jdbcType="VARCHAR" javaType="java.lang.String" />
      <arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
    </constructor>
  </resultMap>

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

  <sql id="Base_Column_List" >
    book_id, book_name, price
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from t_hibernate_book
    where book_id = #{bookId,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from t_hibernate_book
    where book_id = #{bookId,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zhuling.model.Hbook" >
    insert into t_hibernate_book (book_id, book_name, price
      )
    values (#{bookId,jdbcType=INTEGER}, #{bookName,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.zhuling.model.Hbook" >
    insert into t_hibernate_book
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="bookId != null" >
        book_id,
      </if>
      <if test="bookName != null" >
        book_name,
      </if>
      <if test="price != null" >
        price,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="bookId != null" >
        #{bookId,jdbcType=INTEGER},
      </if>
      <if test="bookName != null" >
        #{bookName,jdbcType=VARCHAR},
      </if>
      <if test="price != null" >
        #{price,jdbcType=REAL},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zhuling.model.Hbook" >
    update t_hibernate_book
    <set >
      <if test="bookName != null" >
        book_name = #{bookName,jdbcType=VARCHAR},
      </if>
      <if test="price != null" >
        price = #{price,jdbcType=REAL},
      </if>
    </set>
    where book_id = #{bookId,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zhuling.model.Hbook" >
    update t_hibernate_book
    set book_name = #{bookName,jdbcType=VARCHAR},
      price = #{price,jdbcType=REAL}
    where book_id = #{bookId,jdbcType=INTEGER}
  </update>


  <select id="selectByBid" resultMap="HbookVoMap" >
    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=#{bid}
  </select>
</mapper>

ordeItemMapper

package com.zhuling.mapper;

import com.zhuling.model.OrdeItem;
import com.zhuling.model.vo.OrderItemVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface OrdeItemMapper {
    int deleteByPrimaryKey(Integer orderItemId);

    int insert(OrdeItem record);

    int insertSelective(OrdeItem record);

    OrdeItem selectByPrimaryKey(Integer orderItemId);

    int updateByPrimaryKeySelective(OrdeItem record);

    int updateByPrimaryKey(OrdeItem record);

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

ordeItemMapper.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.zhuling.mapper.OrdeItemMapper" >
  <resultMap id="BaseResultMap" type="com.zhuling.model.OrdeItem" >
    <constructor >
      <idArg column="order_item_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="product_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="quantity" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="oid" jdbcType="INTEGER" javaType="java.lang.Integer" />
    </constructor>
  </resultMap>

  <resultMap id="OrderItemVoMap" type="com.zhuling.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>-->
    <collection property="orde" javaType="com.zhuling.model.Orde">
      <result property="orderId" column="order_id"></result>
      <result property="orderNo" column="order_no"></result>
    </collection>
  </resultMap>

  <sql id="Base_Column_List" >
    order_item_id, product_id, quantity, oid
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from t_hibernate_order_item
    where order_item_id = #{orderItemId,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from t_hibernate_order_item
    where order_item_id = #{orderItemId,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zhuling.model.OrdeItem" >
    insert into t_hibernate_order_item (order_item_id, product_id, quantity, 
      oid)
    values (#{orderItemId,jdbcType=INTEGER}, #{productId,jdbcType=INTEGER}, #{quantity,jdbcType=INTEGER}, 
      #{oid,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" parameterType="com.zhuling.model.OrdeItem" >
    insert into t_hibernate_order_item
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="orderItemId != null" >
        order_item_id,
      </if>
      <if test="productId != null" >
        product_id,
      </if>
      <if test="quantity != null" >
        quantity,
      </if>
      <if test="oid != null" >
        oid,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="orderItemId != null" >
        #{orderItemId,jdbcType=INTEGER},
      </if>
      <if test="productId != null" >
        #{productId,jdbcType=INTEGER},
      </if>
      <if test="quantity != null" >
        #{quantity,jdbcType=INTEGER},
      </if>
      <if test="oid != null" >
        #{oid,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zhuling.model.OrdeItem" >
    update t_hibernate_order_item
    <set >
      <if test="productId != null" >
        product_id = #{productId,jdbcType=INTEGER},
      </if>
      <if test="quantity != null" >
        quantity = #{quantity,jdbcType=INTEGER},
      </if>
      <if test="oid != null" >
        oid = #{oid,jdbcType=INTEGER},
      </if>
    </set>
    where order_item_id = #{orderItemId,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zhuling.model.OrdeItem" >
    update t_hibernate_order_item
    set product_id = #{productId,jdbcType=INTEGER},
      quantity = #{quantity,jdbcType=INTEGER},
      oid = #{oid,jdbcType=INTEGER}
    where order_item_id = #{orderItemId,jdbcType=INTEGER}
  </update>

  <select id="selectByOrderItemId" resultMap="OrderItemVoMap"  >
    select * from t_hibernate_order o,t_hibernate_order_item oi where o.order_id=oi.oid
    and oi.order_item_id=#{orderItemId}
  </select>

</mapper>

ordeMapp

package com.zhuling.mapper;

import com.zhuling.model.Orde;
import com.zhuling.model.vo.OrderVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
@Repository
public interface OrdeMapper {
    int deleteByPrimaryKey(Integer orderId);

    int insert(Orde record);

    int insertSelective(Orde record);

    Orde selectByPrimaryKey(Integer orderId);

    int updateByPrimaryKeySelective(Orde record);

    int updateByPrimaryKey(Orde record);

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

ordeMapper.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.zhuling.mapper.OrdeMapper" >
  <resultMap id="BaseResultMap" type="com.zhuling.model.Orde" >
    <constructor >
      <idArg column="order_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
      <arg column="order_no" jdbcType="VARCHAR" javaType="java.lang.String" />
    </constructor>
  </resultMap>

  <resultMap id="OrderVoMap" type="com.zhuling.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.zhuling.model.OrdeItem">
      <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>


  <sql id="Base_Column_List" >
    order_id, order_no
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from t_hibernate_order
    where order_id = #{orderId,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from t_hibernate_order
    where order_id = #{orderId,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.zhuling.model.Orde" >
    insert into t_hibernate_order (order_id, order_no)
    values (#{orderId,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.zhuling.model.Orde" >
    insert into t_hibernate_order
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="orderId != null" >
        order_id,
      </if>
      <if test="orderNo != null" >
        order_no,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="orderId != null" >
        #{orderId,jdbcType=INTEGER},
      </if>
      <if test="orderNo != null" >
        #{orderNo,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.zhuling.model.Orde" >
    update t_hibernate_order
    <set >
      <if test="orderNo != null" >
        order_no = #{orderNo,jdbcType=VARCHAR},
      </if>
    </set>
    where order_id = #{orderId,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.zhuling.model.Orde" >
    update t_hibernate_order
    set order_no = #{orderNo,jdbcType=VARCHAR}
    where order_id = #{orderId,jdbcType=INTEGER}
  </update>

  <select id="selectByOrderId" resultMap="OrderVoMap" >
    select * from t_hibernate_order o,t_hibernate_order_item oi where o.order_id=oi.oid
    and oi.oid=#{orderId}
  </select>
</mapper>

OneToManyService

package com.zhuling.service;

import com.zhuling.model.vo.OrderItemVo;
import com.zhuling.model.vo.OrderVo;

import java.util.List;

/**
 * @author zhuling
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2019-10-23 19:18
 */
public interface OneToManyService {

    List<OrderItemVo> selectByOrderItemId(Integer orderItemId);

    List<OrderVo> selectByOrderId(Integer orderId);
}

OneToManyServiceImpl

package com.zhuling.service.impl;

import com.zhuling.mapper.OrdeItemMapper;
import com.zhuling.mapper.OrdeMapper;
import com.zhuling.model.vo.OrderItemVo;
import com.zhuling.model.vo.OrderVo;
import com.zhuling.service.OneToManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhuling
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2019-10-23 19:34
 */
@Service
public class OneToManyServiceImpl implements OneToManyService {
    @Autowired
    private OrdeMapper ordeMapper;
   @Autowired
   private OrdeItemMapper ordeItemMapper;
    @Override
    public List<OrderItemVo> selectByOrderItemId(Integer orderItemId) {
        return ordeItemMapper.selectByOrderItemId(orderItemId);
    }

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

OneToManyServiceImplTest

package com.zhuling.service.impl;

import com.zhuling.SpringBaseTest;
import com.zhuling.model.OrdeItem;
import com.zhuling.model.vo.OrderItemVo;
import com.zhuling.model.vo.OrderVo;
import com.zhuling.service.OneToManyService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

import static org.junit.Assert.*;

/**
 * @author zhuling
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2019-10-23 19:40
 */
public class OneToManyServiceImplTest extends SpringBaseTest {

    @Autowired
    private OneToManyService oneToManyService;

    @Test
    public void selectByOrderItemId() {
        List<OrderItemVo> orderItemVos = oneToManyService.selectByOrderItemId(36);
        OrderItemVo orderItemVo=orderItemVos.get(0);
        System.out.println(orderItemVo);
        System.out.println(orderItemVo.getOrde());
    }

    @Test
    public void selectByOrderId() {
        List<OrderVo> orderVos = oneToManyService.selectByOrderId(8);
        OrderVo orderVo=orderVos.get(0);
//        System.out.println(orderVos.size());
        System.out.println(orderVo);
        for (OrdeItem orderItem : orderVo.getOrderItems()) {
            System.out.println(orderItem);
        }
    }
}

mybatis的多对多关联关系

ManToManyService

package com.zhuling.service;

import com.zhuling.model.vo.CategoryVo;
import com.zhuling.model.vo.HbookVo;

/**
 * @author zhuling
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2019-10-23 20:24
 */
public interface ManyToManyService {
    CategoryVo selectByCid(Integer cid);
    HbookVo selectByBid(Integer bid);
}

ManToManyServiceImpl

package com.zhuling.service.impl;

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

/**
 * @author zhuling
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2019-10-23 20:25
 */
@Service
public class ManyToManyServiceImpl implements ManyToManyService {
    @Autowired
    private HbookMapper hbookMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public CategoryVo selectByCid(Integer cid) {
        return categoryMapper.selectByCid(cid);
    }

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

ManToManyServiceImplTest

package com.zhuling.service.impl;

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

/**
 * @author zhuling
 * @site www.xiaomage.com
 * @company xxx公司
 * @create  2019-10-23 20:27
 */
public class ManyToManyServiceImplTest extends SpringBaseTest {

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

}

 

到这里就结束了!!!!

 

 

OrderVo
原文地址:https://www.cnblogs.com/BAYOUA/p/11732348.html