欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

关联关系映射

程序员文章站 2022-04-23 11:49:24
...

一对多关联关系

1.使用的五个数据表如下
关联关系映射

2.通过resources下的generatorConfig插件生成dao、mapper、model

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd" >
<generatorConfiguration>
    <!-- 引入配置文件 -->
    <properties resource="jdbc.properties"/>

    <!--指定数据库jdbc驱动jar包的位置-->
    <classPathEntry location="D:\\tools\\mvn\\mysql\\mysql-connector-java\\5.1.44\\mysql-connector-java-5.1.44.jar"/>

    <!-- 一个数据库一个context -->
    <context id="infoGuardian">
        <!-- 注释 -->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/><!-- 是否取消注释 -->
            <property name="suppressDate" value="true"/> <!-- 是否生成注释代时间戳 -->
        </commentGenerator>

        <!-- jdbc连接 -->
        <jdbcConnection driverClass="${jdbc.driver}"
                        connectionURL="${jdbc.url}" userId="${jdbc.username}" password="${jdbc.password}"/>

        <!-- 类型转换 -->
        <javaTypeResolver>
            <!-- 是否使用bigDecimal, false可自动转化以下类型(Long, Integer, Short, etc.) -->
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 01 指定javaBean生成的位置 -->
        <!-- targetPackage:指定生成的model生成所在的包名 -->
        <!-- targetProject:指定在该项目下所在的路径  -->
        <javaModelGenerator targetPackage="com.xwt.ssm.model"
                            targetProject="src/main/java">
            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
            <!-- 是否对model添加构造函数 -->
            <property name="constructorBased" value="true"/>
            <!-- 是否针对string类型的字段在set的时候进行trim调用 -->
            <property name="trimStrings" value="false"/>
            <!-- 建立的Model对象是否 不可改变  即生成的Model对象不会有 setter方法,只有构造方法 -->
            <property name="immutable" value="false"/>
        </javaModelGenerator>

        <!-- 02 指定sql映射文件生成的位置 -->
        <sqlMapGenerator targetPackage="com.xwt.ssm.mapper"
                         targetProject="src/main/java">
            <!-- 是否允许子包,即targetPackage.schemaName.tableName -->
            <property name="enableSubPackages" value="false"/>
        </sqlMapGenerator>

        <!-- 03 生成XxxMapper接口 -->
        <!-- type="ANNOTATEDMAPPER",生成Java Model 和基于注解的Mapper对象 -->
        <!-- type="MIXEDMAPPER",生成基于注解的Java Model 和相应的Mapper对象 -->
        <!-- type="XMLMAPPER",生成SQLMap XML文件和独立的Mapper接口 -->
        <javaClientGenerator targetPackage="com.xwt.ssm.mapper"
                             targetProject="src/main/java" type="XMLMAPPER">
            <!-- 是否在当前路径下新加一层schema,false路径com.oop.eksp.user.model, true:com.oop.eksp.user.model.[schemaName] -->
            <property name="enableSubPackages" value="false"/>
        </javaClientGenerator>

        <!-- 配置表信息 -->
        <!-- schema即为数据库名 -->
        <!-- tableName为对应的数据库表 -->
        <!-- domainObjectName是要生成的实体类 -->
        <!-- enable*ByExample是否生成 example类 -->
        <!--<table schema="" tableName="t_book" domainObjectName="Book"-->
        <!--enableCountByExample="false" enableDeleteByExample="false"-->
        <!--enableSelectByExample="false" enableUpdateByExample="false">-->
        <!--&lt;!&ndash; 忽略列,不生成bean 字段 &ndash;&gt;-->
        <!--&lt;!&ndash; <ignoreColumn column="FRED" /> &ndash;&gt;-->
        <!--&lt;!&ndash; 指定列的java数据类型 &ndash;&gt;-->
        <!--&lt;!&ndash; <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> &ndash;&gt;-->
        <!--</table>-->

        <table schema="" tableName="t_hibernate_order" domainObjectName="Order"
               enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false">
        </table>
        <table schema="" tableName="t_hibernate_order_item" domainObjectName="OrderItem"
               enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false">
        </table>
        <table schema="" tableName="t_hibernate_book" domainObjectName="Hbook"
               enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false">
        </table>
        <table schema="" tableName="t_hibernate_category" domainObjectName="Category"
               enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false">
        </table>
        <table schema="" tableName="t_hibernate_book_category" domainObjectName="HbookCategory"
               enableCountByExample="false" enableDeleteByExample="false"
               enableSelectByExample="false" enableUpdateByExample="false">
        </table>


    </context>
</generatorConfiguration>

关联关系映射
具体代码如下:

OrderVo.java

package com.xwt.ssm.model.vo;

import com.xwt.ssm.model.Order;
import com.xwt.ssm.model.OrderItem;
import net.sf.ehcache.store.StoreQuery;

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

package com.xwt.ssm.model.vo;

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

public class OrderItemVo extends OrderItem{
    private Order order;

    public Order getOrder() {
        return order;
    }

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

OrderMapper.java

package com.xwt.ssm.mapper;

import com.xwt.ssm.model.Order;
import com.xwt.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);

}

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

  <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.xwt.ssm.model.Order" >
    insert into t_hibernate_order (order_id, order_no)
    values (#{orderId,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.xwt.ssm.model.Order" >
    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.xwt.ssm.model.Order" >
    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.xwt.ssm.model.Order" >
    update t_hibernate_order
    set order_no = #{orderNo,jdbcType=VARCHAR}
    where order_id = #{orderId,jdbcType=INTEGER}
  </update>

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

OrderItemMapper.java

package com.xwt.ssm.mapper;

import com.xwt.ssm.model.OrderItem;
import com.xwt.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);
}

OrderItemMapper.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.xwt.ssm.mapper.OrderItemMapper" >
  <resultMap id="BaseResultMap" type="com.xwt.ssm.model.OrderItem" >
    <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.xwt.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.xwt.ssm.model.Order">
      <result property="orderId" column="order_id"></result>
      <result property="orderNo" column="order_no"></result>
    </association>
  </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.xwt.ssm.model.OrderItem" >
    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.xwt.ssm.model.OrderItem" >
    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.xwt.ssm.model.OrderItem" >
    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.xwt.ssm.model.OrderItem" >
    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="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>
</mapper>

One2Service.java

package com.xwt.ssm.service;

import com.xwt.ssm.model.vo.OrderItemVo;
import com.xwt.ssm.model.vo.OrderVo;
import org.apache.ibatis.annotations.Param;

public interface One2Service {
    OrderVo queryOrderVoByOrderId(Integer orderId);

    OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId);
}

One2ServiceImpl.java

package com.xwt.ssm.service.impl;

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

@Service
public class One2ServiceImpl implements One2Service {
    @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);
    }
}

测试类One2ServiceImplTest.java

package com.xwt.ssm.service.impl;

import com.xwt.ssm.SpingBaseTest;
import com.xwt.ssm.model.OrderItem;
import com.xwt.ssm.model.vo.OrderItemVo;
import com.xwt.ssm.model.vo.OrderVo;
import com.xwt.ssm.service.One2Service;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.*;

public class One2ServiceImplTest extends SpingBaseTest {

    @Autowired
    private One2Service one2Service;
    @Test
    // 通过订单查询所有订单项
    public void queryOrderVoByOrderId() {
        OrderVo orderVo=one2Service.queryOrderVoByOrderId(11);
        System.out.println(orderVo.getOrderNo());
        for (OrderItem orderItem : orderVo.getOrderItems()) {
            System.out.println(orderItem);
        }
    }

    @Test
    // 通过订单项查询所属订单
    public void queryOrderItemVoByOrderItemId() {
        OrderItemVo orderItemVo = one2Service.queryOrderItemVoByOrderItemId(50);
        System.out.println(orderItemVo);
        System.out.println(orderItemVo.getOrder().getOrderNo());
    }
}

运行分别如下:

关联关系映射
关联关系映射

多对多关联关系

HbookVo.java

package com.xwt.ssm.model.vo;

import com.xwt.ssm.model.Category;
import com.xwt.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;
    }
}

CategoryVo .java

package com.xwt.ssm.model.vo;

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

import java.util.List;

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

package com.xwt.ssm.mapper;

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

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.xwt.ssm.mapper.HbookCategoryMapper" >
  <resultMap id="BaseResultMap" type="com.xwt.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.xwt.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.xwt.ssm.model.Category">
      <result property="categoryId" column="category_id"></result>
      <result property="categoryName" column="category_name"></result>
    </collection>
  </resultMap>

  <resultMap id="CategoryVoMap" type="com.xwt.ssm.model.vo.CategoryVo" >
    <result property="categoryId" column="category_id"></result>
    <result property="categoryName" column="category_name"></result>
    <collection property="hbooks" ofType="com.xwt.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.xwt.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.xwt.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.xwt.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.xwt.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>

Many2Service .java

package com.xwt.ssm.service;

import com.xwt.ssm.model.vo.CategoryVo;
import com.xwt.ssm.model.vo.HbookVo;
import org.apache.ibatis.annotations.Param;

public interface Many2Service {
    HbookVo queryByBookId(Integer bookId);

    CategoryVo queryByCid(Integer cid);
}

Many2ServiceImpl .java

package com.xwt.ssm.service.impl;

import com.xwt.ssm.mapper.HbookCategoryMapper;
import com.xwt.ssm.model.Hbook;
import com.xwt.ssm.model.vo.CategoryVo;
import com.xwt.ssm.model.vo.HbookVo;
import com.xwt.ssm.service.Many2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class Many2ServiceImpl implements Many2Service {
    @Autowired
    private HbookCategoryMapper hbookCategoryMapper;

    @Override
    public HbookVo queryByBookId(Integer bookId) {
        return hbookCategoryMapper.queryByBookId(bookId);
    }

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

测试类Many2ServiceImplTest .java

package com.xwt.ssm.service.impl;

import com.xwt.ssm.SpingBaseTest;
import com.xwt.ssm.model.Category;
import com.xwt.ssm.model.Hbook;
import com.xwt.ssm.model.vo.CategoryVo;
import com.xwt.ssm.model.vo.HbookVo;
import com.xwt.ssm.service.Many2Service;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import static org.junit.Assert.*;

public class Many2ServiceImplTest extends SpingBaseTest {

    @Autowired
    private Many2Service many2Service;
    @Test
    // 一本书对应多种类别
    public void queryByBookId() {
        HbookVo hbookVo = many2Service.queryByBookId(8);
        System.out.println(hbookVo.getBookName());
        for (Category category : hbookVo.getCategories()) {
            System.out.println(category.getCategoryName());
        }
    }

    @Test
    // 一种类别对应多本书
    public void queryByCid() {
        CategoryVo category=many2Service.queryByCid(9);
        System.out.println(category.getCategoryName());
        for (Hbook hbook : category.getHbooks()) {
            System.out.println(hbook.getBookName());
        }
    }
}

运行分别如下:
关联关系映射
关联关系映射

相关标签: 关联关系映射