• Mybatis之关联关系映射


    一、一对多关联关系

    1、实体类

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

    ①Order

    package com.xnx.ssm.model;
    
    import lombok.AllArgsConstructor;
    import lombok.Data;
    import lombok.NoArgsConstructor;
    
    //t_hibernate_order
    //相当于添加了set/get方法,同时添加tostring
    //@NoArgsConstructor
    //@AllArgsConstructor
    @Data
    public class Order {
        private Integer orderId;
    
        private String orderNo;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    ②OrderItem

    package com.xnx.ssm.model;
    
    import lombok.Data;
    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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    2、Biz层

    ①OrderBiz

    package com.xnx.ssm.biz;
    
    import com.xnx.ssm.model.Book;
    import com.xnx.ssm.model.BookVo;
    import com.xnx.ssm.model.Order;
    import com.xnx.ssm.model.vo.OrderVo;
    import com.xnx.ssm.util.PageBean;
    import org.apache.ibatis.annotations.Param;
    
    import java.util.List;
    import java.util.Map;
    
    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);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    ②OrderItemBiz

    package com.xnx.ssm.biz;
    
    import com.xnx.ssm.model.OrderItem;
    import com.xnx.ssm.model.vo.OrderItemVo;
    import org.apache.ibatis.annotations.Param;
    
    public interface OrderItemBiz {
        int deleteByPrimaryKey(Integer orderItemId);
    
        int insert(OrderItem record);
    
        int insertSelective(OrderItem record);
    
        OrderItem selectByPrimaryKey(Integer orderItemId);
    
        OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId);
    
        int updateByPrimaryKeySelective(OrderItem record);
    
        int updateByPrimaryKey(OrderItem record);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3、Biz的实现层Impl

    ①OrderBizImpl

    package com.xnx.ssm.biz.impl;
    
    import com.xnx.ssm.biz.OrderBiz;
    import com.xnx.ssm.mapper.OrderMapper;
    import com.xnx.ssm.model.Order;
    import com.xnx.ssm.model.vo.OrderVo;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    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;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    ②OrderItemBizImpl

    package com.xnx.ssm.biz.impl;
    
    import com.xnx.ssm.biz.OrderItemBiz;
    import com.xnx.ssm.mapper.OrderItemMapper;
    import com.xnx.ssm.model.OrderItem;
    import com.xnx.ssm.model.vo.OrderItemVo;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class OrderItemBizImpl implements OrderItemBiz {
        @Autowired
        private OrderItemMapper orderItemMapper;
        @Override
        public int deleteByPrimaryKey(Integer orderItemId) {
            return 0;
        }
    
        @Override
        public int insert(OrderItem record) {
            return 0;
        }
    
        @Override
        public int insertSelective(OrderItem record) {
            return 0;
        }
    
        @Override
        public OrderItem selectByPrimaryKey(Integer orderItemId) {
            return null;
        }
    
        @Override
        public OrderItemVo queryOrderItemVoByOrderItemId(Integer orderItemId) {
            return orderItemMapper.queryOrderItemVoByOrderItemId(orderItemId);
        }
    
        @Override
        public int updateByPrimaryKeySelective(OrderItem record) {
            return 0;
        }
    
        @Override
        public int updateByPrimaryKey(OrderItem record) {
            return 0;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    4、mapper层

    ①OrderMapper.java

    package com.xnx.ssm.mapper;
    
    import com.xnx.ssm.model.Order;
    import com.xnx.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);
    
        OrderVo queryOrderVoByOrderId(@Param("orderId") Integer orderId);
    
        int updateByPrimaryKeySelective(Order record);
    
        int updateByPrimaryKey(Order record);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    ②OrderMapper.xml

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.xnx.ssm.mapper.OrderMapper" >
      <resultMap id="BaseResultMap" type="com.xnx.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.xnx.ssm.model.vo.OrderVo" >
        <result column="order_id" property="orderId">result>
        <result column="order_no" property="orderNo">result>
        <collection property="orderItems" ofType="com.xnx.ssm.model.OrderItem">
          <result column="order_item_id" property="orderItemId">result>
          <result column="product_id" property="productId">result>
          <result column="quantity" property="quantity">result>
          <result column="oid" property="oid">result>
        collection>
      resultMap>
    
      <sql id="Base_Column_List" >
        order_id, order_no
      sql>
    
      <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>
    
      <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.xnx.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.xnx.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.xnx.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.xnx.ssm.model.Order" >
        update t_hibernate_order
        set order_no = #{orderNo,jdbcType=VARCHAR}
        where order_id = #{orderId,jdbcType=INTEGER}
      update>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78

    ③OrderItemMapper.java

    package com.xnx.ssm.mapper;
    
    import com.xnx.ssm.model.OrderItem;
    import com.xnx.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);
    
        OrderItemVo queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);
    
        int updateByPrimaryKeySelective(OrderItem record);
    
        int updateByPrimaryKey(OrderItem record);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    ④OrderItemMapper.xml

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.xnx.ssm.mapper.OrderItemMapper" >
      <resultMap id="BaseResultMap" type="com.xnx.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.xnx.ssm.model.vo.OrderItemVo" >
        <result column="order_item_id" property="orderItemId">result>
        <result column="product_id" property="productId">result>
        <result column="quantity" property="quantity">result>
        <result column="oid" property="oid">result>
        <association property="order" javaType="com.xnx.ssm.model.Order">
          <result column="order_id" property="orderId">result>
          <result column="order_no" property="orderNo">result>
        association>
      resultMap>
    
      <sql id="Base_Column_List" >
        order_item_id, product_id,quantity,oid
      sql>
    
      <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>
    
      <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.xnx.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.xnx.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.xnx.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.xnx.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>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102

    5、测试类

    ①OrderBizImplTest

    package com.xnx.ssm.biz.impl;
    
    import com.xnx.ssm.biz.OrderBiz;
    import com.xnx.ssm.model.Order;
    import com.xnx.ssm.model.vo.OrderVo;
    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 OrderBizImplTest {
        @Autowired
        private OrderBiz orderBiz;
    
        @Test
        public void queryOrderVoByOrderId() {
            OrderVo orderVo = orderBiz.queryOrderVoByOrderId(9);
            System.out.println(orderVo);
            orderVo.getOrderItems().forEach(System.out::println);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    无@Data注解运行效果:
    在这里插入图片描述
    有@Data注解运行效果:
    在这里插入图片描述

    ②OrderItemBizImplTest

    package com.xnx.ssm.biz.impl;
    
    import com.xnx.ssm.biz.OrderItemBiz;
    import com.xnx.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());
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    运行效果:
    在这里插入图片描述

    二、多对多关联关系

    1、实体类

    ①Hbook

    package com.xnx.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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    ②HbookCategory

    package com.xnx.ssm.model;
    
    public class HbookCategory {
        private Integer bcid;
    
        private Integer bid;
    
        private Integer cid;
    
        public HbookCategory(Integer bcid, Integer bid, Integer cid) {
            this.bcid = bcid;
            this.bid = bid;
            this.cid = cid;
        }
    
        public HbookCategory() {
            super();
        }
    
        public Integer getBcid() {
            return bcid;
        }
    
        public void setBcid(Integer bcid) {
            this.bcid = bcid;
        }
    
        public Integer getBid() {
            return bid;
        }
    
        public void setBid(Integer bid) {
            this.bid = bid;
        }
    
        public Integer getCid() {
            return cid;
        }
    
        public void setCid(Integer cid) {
            this.cid = cid;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    2、biz层

    ①HbookCategoryBiz

    package com.xnx.ssm.biz;
    
    import com.xnx.ssm.model.HbookCategory;
    import com.xnx.ssm.model.vo.CategoryVo;
    import com.xnx.ssm.model.vo.HbookVo;
    import org.apache.ibatis.annotations.Param;
    
    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);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    3、biz的实现层impl

    ①HbookCategoryBizImpl

    package com.xnx.ssm.biz.impl;
    
    import com.xnx.ssm.biz.HbookCategoryBiz;
    import com.xnx.ssm.mapper.HbookCategoryMapper;
    import com.xnx.ssm.model.HbookCategory;
    import com.xnx.ssm.model.vo.CategoryVo;
    import com.xnx.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);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    4、mapper

    ①HbookCategoryMapper.java

    package com.xnx.ssm.mapper;
    
    import com.xnx.ssm.model.HbookCategory;
    import com.xnx.ssm.model.vo.CategoryVo;
    import com.xnx.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);
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    ②HbookCategoryMapper.xml

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.xnx.ssm.mapper.HbookCategoryMapper" >
      <resultMap id="BaseResultMap" type="com.xnx.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.xnx.ssm.model.vo.HbookVo">
        <result property="bookId" column="book_id">result>
        <result property="bookName" column="book_name">result>
        <collection property="categories" ofType="com.xnx.ssm.model.Category">
          <result property="categoryId" column="category_id">result>
          <result property="categoryName" column="category_name">result>
        collection>
      resultMap>
    
      <resultMap id="CategoryVoMap" type="com.xnx.ssm.model.vo.CategoryVo">
        <result property="categoryId" column="category_id">result>
        <result property="categoryName" column="category_name">result>
        <collection property="hbooks" ofType="com.xnx.ssm.model.Hbook">
          <result property="bookId" column="book_id">result>
          <result property="bookName" column="book_name">result>
        collection>
      resultMap>
      <sql id="Base_Column_List" >
        bcid, bid, cid
      sql>
    
    
      <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>
    
    
    
      <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.xnx.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.xnx.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.xnx.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.xnx.ssm.model.HbookCategory" >
        update t_hibernate_book_category
        set bid = #{bid,jdbcType=INTEGER},
          cid = #{cid,jdbcType=INTEGER}
        where bcid = #{bcid,jdbcType=INTEGER}
      update>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103

    ③HbookMapper.java

    package com.xnx.ssm.mapper;
    
    import com.xnx.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);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    ④HbookMapper.xml

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    <mapper namespace="com.xnx.ssm.mapper.HbookMapper" >
      <resultMap id="BaseResultMap" type="com.xnx.ssm.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>
      <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.xnx.ssm.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.xnx.ssm.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.xnx.ssm.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.xnx.ssm.model.Hbook" >
        update t_hibernate_book
        set book_name = #{bookName,jdbcType=VARCHAR},
          price = #{price,jdbcType=REAL}
        where book_id = #{bookId,jdbcType=INTEGER}
      update>
    mapper>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    4、vo层

    ①CategoryVo

    package com.xnx.ssm.model.vo;
    
    import com.xnx.ssm.model.Category;
    import com.xnx.ssm.model.Hbook;
    
    import java.util.List;
    
    public class CategoryVo extends Category {
        public List<Hbook> getHbooks() {
            return hbooks;
        }
    
        public void setHbooks(List<Hbook> hbooks) {
            this.hbooks = hbooks;
        }
    
        private List<Hbook> hbooks;
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    ②HbookVo

    package com.xnx.ssm.model.vo;
    
    import com.xnx.ssm.model.Category;
    import com.xnx.ssm.model.Hbook;
    
    import java.util.List;
    
    public class HbookVo extends Hbook {
        public List<Category> getCategories() {
            return categories;
        }
    
        public void setCategories(List<Category> categories) {
            this.categories = categories;
        }
    
        private List<Category> categories;
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    5、测试类

    ①HbookCategoryBizImplTest

    package com.xnx.ssm.biz.impl;
    
    import com.xnx.ssm.biz.HbookCategoryBiz;
    import com.xnx.ssm.model.vo.CategoryVo;
    import com.xnx.ssm.model.vo.HbookVo;
    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 HbookCategoryBizImplTest {
        @Autowired
        private HbookCategoryBiz hbookCategoryBiz;
    
        @Test
        public void queryByBookId() {
            HbookVo hbookVo = hbookCategoryBiz.queryByBookId(8);
            System.out.println(hbookVo);
            hbookVo.getCategories().forEach(System.out::println);
        }
    
        @Test
        public void queryByCid() {
            CategoryVo categoryVo=hbookCategoryBiz.queryByCid(8);
            System.out.println(categoryVo);
            categoryVo.getHbooks().forEach(System.out::println);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    queryByBookId运行效果:
    在这里插入图片描述

    queryByCid运行效果:
    在这里插入图片描述

  • 相关阅读:
    【react】Hooks原理和实战
    Mac远程访问Windows服务器
    【LeetCode】【剑指offer】【数组中出现次数超出一半的数字】
    前端练习--奇奇动漫导航条(通过背景图设置)
    技术架构职责和应该注意哪些
    【Java】Java 高频面试题英语版(1)
    kube-prometheus-stack监控k8s1.24+ docker缺少图像
    gma 2.0.2 (2023.10.15) 更新日志
    网页里面的3D交互展示是怎么做的呢?
    Mozilla Firefox侧边栏和垂直标签在131 Nightly版本中开始试用
  • 原文地址:https://blog.csdn.net/weixin_67677668/article/details/126352804