• mybatis之关联关系映射


    Mybatis关联关系映射

    1、一对多

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

    OrderVo
     1 package com.yuan.model.vo;
     2 
     3 import com.yuan.model.Order;
     4 import com.yuan.model.OrderItem;
     5 
     6 import java.util.ArrayList;
     7 import java.util.List;
     8 
     9 public class OrderVo extends Order {
    10 
    11     private List<OrderItem> orderItems = new ArrayList<>();
    12 
    13     public List<OrderItem> getOrderItems() {
    14         return orderItems;
    15     }
    16 
    17     public void setOrderItems(List<OrderItem> orderItems) {
    18         this.orderItems = orderItems;
    19     }
    20 }
    
    

    OrderItemVo

     1 package com.yuan.model.vo;
     2 
     3 import com.yuan.model.Order;
     4 import com.yuan.model.OrderItem;
     5 
     6 public class OrderItemVo extends OrderItem {
     7     private Order order;
     8 
     9     public Order getOrder() {
    10         return order;
    11     }
    12 
    13     public void setOrder(Order order) {
    14         this.order = order;
    15     }
    16 }

    OrderMapper.xml

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
     3 <mapper namespace="com.yuan.mapper.OrderMapper">
     4     <resultMap id="BaseResultMap" type="com.yuan.model.Order">
     5         <constructor>
     6             <idArg column="order_id" jdbcType="INTEGER" javaType="java.lang.Integer"/>
     7             <arg column="order_no" jdbcType="VARCHAR" javaType="java.lang.String"/>
     8         </constructor>
     9     </resultMap>
    10 
    11     <resultMap id="OrderVoMap" type="com.yuan.model.vo.OrderVo">
    12         <result property="orderId" column="order_id"></result>
    13         <result property="orderNo" column="order_no"></result>
    14         <!--<result property="orderItems" column=""></result>-->
    15         <collection property="orderItems" ofType="com.yuan.model.OrderItem">
    16             <result property="orderItemId" column="order_item_id"></result>
    17             <result property="productId" column="product_id"></result>
    18             <result property="quantity" column="quantity"></result>
    19             <result property="oid" column="oid"></result>
    20         </collection>
    21     </resultMap>
    22 
    23     <sql id="Base_Column_List">
    24     order_id, order_no
    25   </sql>
    26     <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
    27         select
    28         <include refid="Base_Column_List"/>
    29         from t_hibernate_order
    30         where order_id = #{orderId,jdbcType=INTEGER}
    31     </select>
    32     <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    33     delete from t_hibernate_order
    34     where order_id = #{orderId,jdbcType=INTEGER}
    35   </delete>
    36     <insert id="insert" parameterType="com.yuan.model.Order">
    37     insert into t_hibernate_order (order_id, order_no)
    38     values (#{orderId,jdbcType=INTEGER}, #{orderNo,jdbcType=VARCHAR})
    39   </insert>
    40     <insert id="insertSelective" parameterType="com.yuan.model.Order">
    41         insert into t_hibernate_order
    42         <trim prefix="(" suffix=")" suffixOverrides=",">
    43             <if test="orderId != null">
    44                 order_id,
    45             </if>
    46             <if test="orderNo != null">
    47                 order_no,
    48             </if>
    49         </trim>
    50         <trim prefix="values (" suffix=")" suffixOverrides=",">
    51             <if test="orderId != null">
    52                 #{orderId,jdbcType=INTEGER},
    53             </if>
    54             <if test="orderNo != null">
    55                 #{orderNo,jdbcType=VARCHAR},
    56             </if>
    57         </trim>
    58     </insert>
    59     <update id="updateByPrimaryKeySelective" parameterType="com.yuan.model.Order">
    60         update t_hibernate_order
    61         <set>
    62             <if test="orderNo != null">
    63                 order_no = #{orderNo,jdbcType=VARCHAR},
    64             </if>
    65         </set>
    66         where order_id = #{orderId,jdbcType=INTEGER}
    67     </update>
    68     <update id="updateByPrimaryKey" parameterType="com.yuan.model.Order">
    69     update t_hibernate_order
    70     set order_no = #{orderNo,jdbcType=VARCHAR}
    71     where order_id = #{orderId,jdbcType=INTEGER}
    72   </update>
    73 
    74     <!--通过一个订单id,查询出订单信息的同时,查询所有的订单项-->
    75     <select id="selectByOrderId" resultMap="OrderVoMap" parameterType="java.lang.Integer">
    76     SELECT * FROM t_hibernate_order o,t_hibernate_order_item oi WHERE o.`order_id`=oi.`oid`
    77 AND oi.oid=#{orderId}
    78   </select>
    79 
    80 
    81 </mapper>

    OrderMapper.java

     1 package com.yuan.mapper;
     2 
     3 import com.yuan.model.Order;
     4 import com.yuan.model.vo.OrderVo;
     5 import org.apache.ibatis.annotations.Param;
     6 import org.springframework.stereotype.Repository;
     7 
     8 import java.util.List;
     9 
    10 @Repository
    11 public interface OrderMapper {
    12     int deleteByPrimaryKey(Integer orderId);
    13 
    14     int insert(Order record);
    15 
    16     int insertSelective(Order record);
    17 
    18     Order selectByPrimaryKey(Integer orderId);
    19 
    20     int updateByPrimaryKeySelective(Order record);
    21 
    22     int updateByPrimaryKey(Order record);
    23 
    24      //通过一个订单id,查询出订单信息的同时,查询所有的订单项
    25     List<OrderVo> selectByOrderId(@Param("orderId") Integer orderId);
    26 
    27 }
    OrderItemMapper.xml
      1 <?xml version="1.0" encoding="UTF-8" ?>
      2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
      3 <mapper namespace="com.yuan.mapper.OrderItemMapper" >
      4   <resultMap id="BaseResultMap" type="com.yuan.model.OrderItem" >
      5     <constructor >
      6       <idArg column="order_item_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
      7       <arg column="product_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
      8       <arg column="quantity" jdbcType="INTEGER" javaType="java.lang.Integer" />
      9       <arg column="oid" jdbcType="INTEGER" javaType="java.lang.Integer" />
     10     </constructor>
     11   </resultMap>
     12 
     13   <resultMap id="OrderItemVoMap" type="com.yuan.model.vo.OrderItemVo">
     14 
     15     <result property="orderItemId" column="order_item_id"></result>
     16     <result property="productId" column="product_id"></result>
     17     <result property="quantity" column="quantity"></result>
     18     <result property="oid" column="oid"></result>
     19     <collection property="order" javaType="com.yuan.model.Order">
     20       <result property="orderId" column="order_id"></result>
     21       <result property="orderNo" column="order_no"></result>
     22     </collection>
     23   </resultMap>
     24 
     25 
     26   <sql id="Base_Column_List" >
     27     order_item_id, product_id, quantity, oid
     28   </sql>
     29   <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
     30     select 
     31     <include refid="Base_Column_List" />
     32     from t_hibernate_order_item
     33     where order_item_id = #{orderItemId,jdbcType=INTEGER}
     34   </select>
     35   <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
     36     delete from t_hibernate_order_item
     37     where order_item_id = #{orderItemId,jdbcType=INTEGER}
     38   </delete>
     39   <insert id="insert" parameterType="com.yuan.model.OrderItem" >
     40     insert into t_hibernate_order_item (order_item_id, product_id, quantity, 
     41       oid)
     42     values (#{orderItemId,jdbcType=INTEGER}, #{productId,jdbcType=INTEGER}, #{quantity,jdbcType=INTEGER}, 
     43       #{oid,jdbcType=INTEGER})
     44   </insert>
     45   <insert id="insertSelective" parameterType="com.yuan.model.OrderItem" >
     46     insert into t_hibernate_order_item
     47     <trim prefix="(" suffix=")" suffixOverrides="," >
     48       <if test="orderItemId != null" >
     49         order_item_id,
     50       </if>
     51       <if test="productId != null" >
     52         product_id,
     53       </if>
     54       <if test="quantity != null" >
     55         quantity,
     56       </if>
     57       <if test="oid != null" >
     58         oid,
     59       </if>
     60     </trim>
     61     <trim prefix="values (" suffix=")" suffixOverrides="," >
     62       <if test="orderItemId != null" >
     63         #{orderItemId,jdbcType=INTEGER},
     64       </if>
     65       <if test="productId != null" >
     66         #{productId,jdbcType=INTEGER},
     67       </if>
     68       <if test="quantity != null" >
     69         #{quantity,jdbcType=INTEGER},
     70       </if>
     71       <if test="oid != null" >
     72         #{oid,jdbcType=INTEGER},
     73       </if>
     74     </trim>
     75   </insert>
     76   <update id="updateByPrimaryKeySelective" parameterType="com.yuan.model.OrderItem" >
     77     update t_hibernate_order_item
     78     <set >
     79       <if test="productId != null" >
     80         product_id = #{productId,jdbcType=INTEGER},
     81       </if>
     82       <if test="quantity != null" >
     83         quantity = #{quantity,jdbcType=INTEGER},
     84       </if>
     85       <if test="oid != null" >
     86         oid = #{oid,jdbcType=INTEGER},
     87       </if>
     88     </set>
     89     where order_item_id = #{orderItemId,jdbcType=INTEGER}
     90   </update>
     91   <update id="updateByPrimaryKey" parameterType="com.yuan.model.OrderItem" >
     92     update t_hibernate_order_item
     93     set product_id = #{productId,jdbcType=INTEGER},
     94       quantity = #{quantity,jdbcType=INTEGER},
     95       oid = #{oid,jdbcType=INTEGER}
     96     where order_item_id = #{orderItemId,jdbcType=INTEGER}
     97   </update>
     98 
     99   <!--通过一个订单项id,查询出订单项信息的同时,查询所所属的订单-->
    100   <select id="selectByOrderItemId" resultMap="OrderItemVoMap" parameterType="java.lang.Integer">
    101     SELECT * FROM t_hibernate_order o,t_hibernate_order_item oi WHERE o.`order_id`=oi.`oid`
    102 AND oi.order_item_id=#{orderItemId}
    103   </select>
    104 
    105 
    106 
    107 </mapper>
    OrderItemMapper.java
     1 package com.yuan.mapper;
     2 
     3 import com.yuan.model.OrderItem;
     4 import com.yuan.model.vo.OrderItemVo;
     5 import org.apache.ibatis.annotations.Param;
     6 import org.springframework.stereotype.Repository;
     7 
     8 import java.util.List;
     9 
    10 @Repository
    11 public interface OrderItemMapper {
    12     int deleteByPrimaryKey(Integer orderItemId);
    13 
    14     int insert(OrderItem record);
    15 
    16     int insertSelective(OrderItem record);
    17 
    18     OrderItem selectByPrimaryKey(Integer orderItemId);
    19 
    20     int updateByPrimaryKeySelective(OrderItem record);
    21 
    22     int updateByPrimaryKey(OrderItem record);
    23 
    24     //通过一个订单项id,查询出订单项信息的同时,查询所所属的订单
    25     List<OrderItemVo> selectByOrderItemId(@Param("orderItemId") Integer orderItemId);
    26 
    27 }

    Service层

    OneToManyService.java

     1 package com.yuan.service;
     2 
     3 import com.yuan.model.vo.OrderItemVo;
     4 import com.yuan.model.vo.OrderVo;
     5 import org.apache.ibatis.annotations.Param;
     6 
     7 import java.util.List;
     8 
     9 public interface OneToManyService {
    10 
    11     //通过一个订单项id,查询出订单项信息的同时,查询所所属的订单
    12     List<OrderItemVo> selectByOrderItemId(Integer orderItemId);
    13 
    14     //通过一个订单id,查询出订单信息的同时,查询所有的订单项
    15     List<OrderVo> selectByOrderId( Integer orderId);
    16 
    17 }

    Service实现类

    OneToManyServiceImpl.java

     1 package com.yuan.service.impl;
     2 
     3 import com.yuan.mapper.OrderItemMapper;
     4 import com.yuan.mapper.OrderMapper;
     5 import com.yuan.model.vo.OrderItemVo;
     6 import com.yuan.model.vo.OrderVo;
     7 import com.yuan.service.OneToManyService;
     8 import org.springframework.beans.factory.annotation.Autowired;
     9 import org.springframework.stereotype.Service;
    10 
    11 import java.util.List;
    12 
    13 @Service
    14 public class OneToManyServiceImpl implements OneToManyService {
    15     @Autowired
    16     private OrderMapper orderMapper;
    17     @Autowired
    18     private OrderItemMapper orderItemMapper;
    19     @Override
    20     public List<OrderItemVo> selectByOrderItemId(Integer orderItemId) {
    21         return orderItemMapper.selectByOrderItemId(orderItemId);
    22     }
    23 
    24     @Override
    25     public List<OrderVo> selectByOrderId(Integer orderId) {
    26         return orderMapper.selectByOrderId(orderId);
    27     }
    28 }

    Test测试层代码

    OneToManyServiceImplTest

     1 package com.yuan.service.impl;
     2 
     3 import com.yuan.SpringBaseTest;
     4 import com.yuan.model.OrderItem;
     5 import com.yuan.model.vo.OrderItemVo;
     6 import com.yuan.model.vo.OrderVo;
     7 import com.yuan.service.OneToManyService;
     8 import org.junit.Test;
     9 import org.junit.runner.RunWith;
    10 import org.springframework.beans.factory.annotation.Autowired;
    11 
    12 import java.util.List;
    13 
    14 import static org.junit.Assert.*;
    15 
    16 public class OneToManyServiceImplTest extends SpringBaseTest {
    17 
    18     @Autowired
    19     private OneToManyService oneToManyService;
    20     @Test
    21     public void selectByOrderItemId() {
    22         List<OrderItemVo> orderItemVos = oneToManyService.selectByOrderItemId(36);
    23         OrderItemVo orderItemVo = orderItemVos.get(0);
    24         //此订单项的信息
    25         System.out.println(orderItemVo);
    26         //此订单项所对应的订单
    27         System.out.println(orderItemVo.getOrder());
    28     }
    29 
    30     @Test
    31     public void selectByOrderId() {
    32 
    33         List<OrderVo> orderVos = oneToManyService.selectByOrderId(8);
    34         OrderVo orderVo = orderVos.get(0);
    35         System.out.println(orderVo);
    36         System.out.println(orderVos.size());
    37         for (OrderItem orderItem : orderVo.getOrderItems()) {
    38             System.out.println(orderItem);
    39         }
    40 
    41     }
    42 }

    测试结果

    2、多对多

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

    HbookVo

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

    CategoryVo

    package com.yuan.model.vo;
    
    import com.yuan.model.Category;
    import com.yuan.model.HBook;
    
    import java.util.ArrayList;
    import java.util.List;
    
    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;
        }
    }

    CategoryMapper.xml

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
     3 <mapper namespace="com.yuan.mapper.CategoryMapper" >
     4   <resultMap id="BaseResultMap" type="com.yuan.model.Category" >
     5     <constructor >
     6       <idArg column="category_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
     7       <arg column="category_name" jdbcType="VARCHAR" javaType="java.lang.String" />
     8     </constructor>
     9   </resultMap>
    10 
    11   <resultMap id="CategoryVoMap" type="com.yuan.model.vo.CategoryVo">
    12     <result property="categoryId" column="category_id"></result>
    13     <result property="categoryName" column="category_name"></result>
    14 
    15     <!--<result property="orderItems" column=""></result>-->
    16     <collection property="hBooks" ofType="com.yuan.model.HBook">
    17       <result property="bookId" column="book_id"></result>
    18       <result property="bookName" column="book_name"></result>
    19       <result property="price" column="price"></result>
    20     </collection>
    21   </resultMap>
    22 
    23   <sql id="Base_Column_List" >
    24     category_id, category_name
    25   </sql>
    26   <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    27     select 
    28     <include refid="Base_Column_List" />
    29     from t_hibernate_category
    30     where category_id = #{categoryId,jdbcType=INTEGER}
    31   </select>
    32   <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    33     delete from t_hibernate_category
    34     where category_id = #{categoryId,jdbcType=INTEGER}
    35   </delete>
    36   <insert id="insert" parameterType="com.yuan.model.Category" >
    37     insert into t_hibernate_category (category_id, category_name)
    38     values (#{categoryId,jdbcType=INTEGER}, #{categoryName,jdbcType=VARCHAR})
    39   </insert>
    40   <insert id="insertSelective" parameterType="com.yuan.model.Category" >
    41     insert into t_hibernate_category
    42     <trim prefix="(" suffix=")" suffixOverrides="," >
    43       <if test="categoryId != null" >
    44         category_id,
    45       </if>
    46       <if test="categoryName != null" >
    47         category_name,
    48       </if>
    49     </trim>
    50     <trim prefix="values (" suffix=")" suffixOverrides="," >
    51       <if test="categoryId != null" >
    52         #{categoryId,jdbcType=INTEGER},
    53       </if>
    54       <if test="categoryName != null" >
    55         #{categoryName,jdbcType=VARCHAR},
    56       </if>
    57     </trim>
    58   </insert>
    59   <update id="updateByPrimaryKeySelective" parameterType="com.yuan.model.Category" >
    60     update t_hibernate_category
    61     <set >
    62       <if test="categoryName != null" >
    63         category_name = #{categoryName,jdbcType=VARCHAR},
    64       </if>
    65     </set>
    66     where category_id = #{categoryId,jdbcType=INTEGER}
    67   </update>
    68   <update id="updateByPrimaryKey" parameterType="com.yuan.model.Category" >
    69     update t_hibernate_category
    70     set category_name = #{categoryName,jdbcType=VARCHAR}
    71     where category_id = #{categoryId,jdbcType=INTEGER}
    72   </update>
    73 
    74   <!--通过一个类别的id,查询出类别信息的同时,还查询出各个类别对应的书籍-->
    75   <select id="selectByCid" resultMap="CategoryVoMap" parameterType="java.lang.Integer" >
    76    SELECT * FROM t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
    77   WHERE b.book_id = bc.bid AND bc.cid = c.category_id
    78   AND c.category_id = #{cid}
    79   </select>
    80 
    81 </mapper>

    CategoryMapper

    package com.yuan.mapper;
    
    import com.yuan.model.Category;
    import com.yuan.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);
    
    //    通过一个类别的id,查询出类别信息的同时,还查询出各个类别对应的书籍
        CategoryVo selectByCid(@Param("cid") Integer cid);
    
    }

    HBookMapper.xml

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
     3 <mapper namespace="com.yuan.mapper.HBookMapper" >
     4   <resultMap id="BaseResultMap" type="com.yuan.model.HBook" >
     5     <constructor >
     6       <idArg column="book_id" jdbcType="INTEGER" javaType="java.lang.Integer" />
     7       <arg column="book_name" jdbcType="VARCHAR" javaType="java.lang.String" />
     8       <arg column="price" jdbcType="REAL" javaType="java.lang.Float" />
     9     </constructor>
    10   </resultMap>
    11 
    12   <resultMap id="HbookVoMap" type="com.yuan.model.vo.HbookVo">
    13     <result property="bookId" column="book_id"></result>
    14     <result property="bookName" column="book_name"></result>
    15     <result property="price" column="price"></result>
    16     <!--<result property="orderItems" column=""></result>-->
    17     <collection property="categories" ofType="com.yuan.model.Category">
    18       <result property="categoryId" column="category_id"></result>
    19       <result property="categoryName" column="category_name"></result>
    20     </collection>
    21   </resultMap>
    22 
    23   <sql id="Base_Column_List" >
    24     book_id, book_name, price
    25   </sql>
    26   <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    27     select 
    28     <include refid="Base_Column_List" />
    29     from t_hibernate_book
    30     where book_id = #{bookId,jdbcType=INTEGER}
    31   </select>
    32   <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    33     delete from t_hibernate_book
    34     where book_id = #{bookId,jdbcType=INTEGER}
    35   </delete>
    36   <insert id="insert" parameterType="com.yuan.model.HBook" >
    37     insert into t_hibernate_book (book_id, book_name, price
    38       )
    39     values (#{bookId,jdbcType=INTEGER}, #{bookName,jdbcType=VARCHAR}, #{price,jdbcType=REAL}
    40       )
    41   </insert>
    42   <insert id="insertSelective" parameterType="com.yuan.model.HBook" >
    43     insert into t_hibernate_book
    44     <trim prefix="(" suffix=")" suffixOverrides="," >
    45       <if test="bookId != null" >
    46         book_id,
    47       </if>
    48       <if test="bookName != null" >
    49         book_name,
    50       </if>
    51       <if test="price != null" >
    52         price,
    53       </if>
    54     </trim>
    55     <trim prefix="values (" suffix=")" suffixOverrides="," >
    56       <if test="bookId != null" >
    57         #{bookId,jdbcType=INTEGER},
    58       </if>
    59       <if test="bookName != null" >
    60         #{bookName,jdbcType=VARCHAR},
    61       </if>
    62       <if test="price != null" >
    63         #{price,jdbcType=REAL},
    64       </if>
    65     </trim>
    66   </insert>
    67   <update id="updateByPrimaryKeySelective" parameterType="com.yuan.model.HBook" >
    68     update t_hibernate_book
    69     <set >
    70       <if test="bookName != null" >
    71         book_name = #{bookName,jdbcType=VARCHAR},
    72       </if>
    73       <if test="price != null" >
    74         price = #{price,jdbcType=REAL},
    75       </if>
    76     </set>
    77     where book_id = #{bookId,jdbcType=INTEGER}
    78   </update>
    79   <update id="updateByPrimaryKey" parameterType="com.yuan.model.HBook" >
    80     update t_hibernate_book
    81     set book_name = #{bookName,jdbcType=VARCHAR},
    82       price = #{price,jdbcType=REAL}
    83     where book_id = #{bookId,jdbcType=INTEGER}
    84   </update>
    85 
    86   <!--通过一个书籍id,查询出书籍的信息的同时,还查询出这本书对应的类别-->
    87   <select id="selectByBid" resultMap="HbookVoMap" parameterType="java.lang.Integer" >
    88    SELECT * FROM t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
    89   WHERE b.book_id = bc.bid AND bc.cid = c.category_id
    90   AND b.book_id=#{bid}
    91   </select>
    92 
    93 
    94 </mapper>

    HBookMapper

     1 package com.yuan.mapper;
     2 
     3 import com.yuan.model.HBook;
     4 import com.yuan.model.vo.HbookVo;
     5 import org.apache.ibatis.annotations.Param;
     6 import org.springframework.stereotype.Repository;
     7 
     8 @Repository
     9 public interface HBookMapper {
    10     int deleteByPrimaryKey(Integer bookId);
    11 
    12     int insert(HBook record);
    13 
    14     int insertSelective(HBook record);
    15 
    16     HBook selectByPrimaryKey(Integer bookId);
    17 
    18     int updateByPrimaryKeySelective(HBook record);
    19 
    20     int updateByPrimaryKey(HBook record);
    21 
    22 //    通过一个书籍id,查询出书籍的信息的同时,还查询出这本书对应的类别
    23     HbookVo selectByBid(@Param("bid") Integer bid);
    24 
    25 }

    Service层相关代码

    ManyToManyService

     1 package com.yuan.service;
     2 
     3 import com.yuan.model.vo.CategoryVo;
     4 import com.yuan.model.vo.HbookVo;
     5 import org.apache.ibatis.annotations.Param;
     6 
     7 public interface ManyToManyService {
     8 
     9 //   通过一个书籍id,查询出书籍的信息的同时,还查询出这本书对应的类别
    10     HbookVo selectByBid(Integer bid);
    11 
    12 //    通过一个类别的id,查询出类别信息的同时,还查询出各个类别对应的书籍
    13     CategoryVo selectByCid(Integer cid);
    14 }

    Service层实现类代码

    ManyToManyServiceImpl

     1 package com.yuan.service.impl;
     2 
     3 import com.yuan.mapper.CategoryMapper;
     4 import com.yuan.mapper.HBookMapper;
     5 import com.yuan.model.vo.CategoryVo;
     6 import com.yuan.model.vo.HbookVo;
     7 import com.yuan.service.ManyToManyService;
     8 import org.springframework.beans.factory.annotation.Autowired;
     9 import org.springframework.stereotype.Service;
    10 
    11 @Service
    12 public class ManyToManyServiceImpl implements ManyToManyService {
    13     @Autowired
    14     private CategoryMapper categoryMapper;
    15     @Autowired
    16     private HBookMapper hBookMapper;
    17 
    18     @Override
    19     public HbookVo selectByBid(Integer bid) {
    20         return hBookMapper.selectByBid(bid);
    21     }
    22 
    23     @Override
    24     public CategoryVo selectByCid(Integer cid) {
    25         return categoryMapper.selectByCid(cid);
    26     }
    27 }

    Test测试类

    ManyToManyServiceImplTest

     1 package com.yuan.service.impl;
     2 
     3 import com.yuan.SpringBaseTest;
     4 import com.yuan.model.Category;
     5 import com.yuan.model.HBook;
     6 import com.yuan.model.vo.CategoryVo;
     7 import com.yuan.model.vo.HbookVo;
     8 import org.junit.Test;
     9 import org.junit.runner.RunWith;
    10 import org.springframework.beans.factory.annotation.Autowired;
    11 
    12 import static org.junit.Assert.*;
    13 
    14 public class ManyToManyServiceImplTest extends SpringBaseTest {
    15 
    16     @Autowired
    17     private ManyToManyServiceImpl manyToManyService;
    18 
    19 //    通过一个书籍id,查询出书籍的信息的同时,还查询出这本书对应的类别
    20     @Test
    21     public void selectByBid() {
    22         HbookVo hbookVo = manyToManyService.selectByBid(8);
    23         System.out.println(hbookVo);
    24         for (Category category : hbookVo.getCategories()) {
    25             System.out.println(category);
    26         }
    27 
    28     }
    29 
    30 //    通过一个类别的id,查询出类别信息的同时,还查询出各个类别对应的书籍
    31     @Test
    32     public void selectByCid() {
    33         CategoryVo categoryVo = manyToManyService.selectByCid(8);
    34         System.out.println(categoryVo);
    35         for (HBook gethBook : categoryVo.gethBooks()) {
    36             System.out.println(gethBook);
    37         }
    38 
    39     }
    40 }

    测试方法一:selectByBid

    测试方法二:selectByCid

     

    谢谢观看!!!

  • 相关阅读:
    Docker(35)- docker inspect 命令详解
    Docker(34)- 如何修改 docker 容器的目录映射
    Docker(33)- 如何修改 docker 容器的端口映射
    Docker(32)- 如何修改 docker 容器的启动参数
    Docker(31)- docker import 命令详解
    Docker(30)- docker export 命令详解
    Docker(29)- docker top 命令详解
    Docker(28)- docker port 命令详解
    Docker
    Docker(27)- docker push 命令详解
  • 原文地址:https://www.cnblogs.com/ly-0919/p/11745530.html
Copyright © 2020-2023  润新知