一、
1、一对多
使用逆向生成工具生成t_hibernate_order、t_hibernate_order_item
这两张表对应的model与mapper
1.1 OrderVo
1 package com.jy.model.vo;
2
3 import com.jy.model.Order;
4 import com.jy.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 }
1.2 OrderItemVo
1 package com.jy.model.vo;
2
3 import com.jy.model.Order;
4 import com.jy.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 }
1.3 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.jy.mapper.OrderMapper">
4 <resultMap id="BaseResultMap" type="com.jy.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.jy.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.jy.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.jy.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.jy.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.jy.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.jy.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>
1.4 OrderMapper
1 package com.jy.mapper;
2
3 import com.jy.model.Order;
4 import com.jy.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 }
1.5 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.jy.mapper.OrderItemMapper" >
4 <resultMap id="BaseResultMap" type="com.jy.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.jy.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.jy.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.jy.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.jy.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.jy.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.jy.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>
1.6 OrderItemMapper
1 package com.jy.mapper;
2
3 import com.jy.model.OrderItem;
4 import com.jy.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层
1.7 OneToManyService
1 package com.jy.service;
2
3 import com.jy.model.vo.OrderItemVo;
4 import com.jy.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实现类
1.8 OneToManyServiceImpl
1 package com.jy.service.impl;
2
3 import com.jy.mapper.OrderItemMapper;
4 import com.jy.mapper.OrderMapper;
5 import com.jy.model.vo.OrderItemVo;
6 import com.jy.model.vo.OrderVo;
7 import com.jy.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测试层代码
1.9 OneToManyServiceImplTest
1 package com.jy.service.impl;
2
3 import com.jy.SpringBaseTest;
4 import com.jy.model.OrderItem;
5 import com.jy.model.vo.OrderItemVo;
6 import com.jy.model.vo.OrderVo;
7 import com.jy.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 }
二、多对多
使用逆向生成工具生成t_hibernate_book、t_hibernate_book_category、t_hibernate_category 这几张表对应的model与mapper
2.1 HbookVo
1 package com.jy.model.vo;
2
3 import com.jy.model.Category;
4 import com.jy.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 }
2.2 CategoryVo
package com.jy.model.vo;
import com.jy.model.Category;
import com.jy.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;
}
}
2.3 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.jy.mapper.CategoryMapper" >
4 <resultMap id="BaseResultMap" type="com.jy.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.jy.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.jy.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.jy.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.jy.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.jy.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.jy.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>
2.4 CategoryMapper
package com.jy.mapper;
import com.jy.model.Category;
import com.jy.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);
}
2.5 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.jy.mapper.HBookMapper" >
4 <resultMap id="BaseResultMap" type="com.jy.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.jy.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.jy.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.jy.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.jy.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.jy.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.jy.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>
2.6 HBookMapper
1 package com.jy.mapper;
2
3 import com.jy.model.HBook;
4 import com.jy.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层相关代码
2.7 ManyToManyService
1 package com.jy.service;
2
3 import com.jy.model.vo.CategoryVo;
4 import com.jy.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层实现类代码
2.8 ManyToManyServiceImpl
1 package com.jy.service.impl;
2
3 import com.jy.mapper.CategoryMapper;
4 import com.jy.mapper.HBookMapper;
5 import com.jy.model.vo.CategoryVo;
6 import com.jy.model.vo.HbookVo;
7 import com.jy.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测试类
2.9 ManyToManyServiceImplTest
1 package com.jy.service.impl;
2
3 import com.jy.SpringBaseTest;
4 import com.jy.model.Category;
5 import com.jy.model.HBook;
6 import com.jy.model.vo.CategoryVo;
7 import com.jy.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