当前位置:   article > 正文

Mybatis之关联关系映射_

目录

一、一对多

          1、Mybatis的关联关系和Hibernate的关联关系大同小异。

          2、关于一对多的关系:

          3、关于Mybatis一对多的关系配置步骤:(以Order和OrderItem为例) 

               3.1:先将对应的表建立好,并且用代码生成器生成对应的mapper和model(注意不要生成重复的mapper和model)

               3.2:在对应的orderMpper.xml和orderItemMpper.xml中配置resultMap节点以及对应的方法:

               3.3:在对应的OrderMapper.java和OrderItemsMapper.java中配置出对应的方法:

               3.4:之后到service层中接口和实现类中写好方法:

               3.5:之后配置两个表之间的关系,建立vo类:

               3.6:建立实体类:

二、多对多

           1、多对多关系就是和一对多的关系的大同小异,只是多对多的关系可以看成两个一对多关系。

           2、多对多关系的配置的步骤和一对多关系配置是一样的:(以书籍表、类别表以及书籍类别表为例)

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

                 2.2在对应的HbookCategoryMapper.xml配置resultMap节点以及对应的方法:(为什么只在HbookCategoryMapper.xml中进行配置,因为只是一个中间表,可以起到一个连接的动作):

               2.3:在对应的HbookCategoryMapper.java文件中写好方法:

               2.4之后到service层中接口和实现类中写好方法

               2.5:之后配置两个表之间的关系,建立vo类:

               2.6:编写测试类:

三、总结:


一、一对多

          1、Mybatis的关联关系和Hibernate的关联关系大同小异。

               mybatis的Hibernate的不同就在于Hibernate代码是自动生成的,而Mybatis的代码可以手动写,可以手动改变。

          2、关于一对多的关系:

先来回归一下Hibernate中关联关系:在Hibernate中,一对多和多对多的关系处理如下:

1、一对多:(以订单表和订单详情表为例)

           一方order类,

               定义一个引用对象list<orderItems>  orderItems

               order.hbm.xml配置:

                                 one-to-many id=“orderItems”

                                  配置多方的实体类路径

           多方orderItem类:

               定义一个引用对象list<order>  order

                orderItem.hbm.xml配置:

                       many-to-one id=“order”

                        配置一方的实体类路径

2、多对多:(以书籍表和类别表为例,其中书籍类别表为中间表) 

                       1方Book:

               定义一个引用对象list<category>  categorys

               book.hbm.xml配置:

                                 many -to-many id=“categorys”

                                  配置多方的实体类路径

                       1方category:

               定义一个引用对象list<book>  books

                category.hbm.xml配置:

                       many-to-manyid=“books”

                        配置一方的实体类路径

                                               

          3、关于Mybatis一对多的关系配置步骤:(以Order和OrderItem为例) 

               3.1:先将对应的表建立好,并且用代码生成器生成对应的mapper和model(注意不要生成重复的mapper和model)

  1. <table schema="" tableName="t_hibernate_order" domainObjectName="Order"
  2. enableCountByExample="false" enableDeleteByExample="false"
  3. enableSelectByExample="false" enableUpdateByExample="false">
  4. <!-- 忽略列,不生成bean 字段 -->
  5. <!-- <ignoreColumn column="FRED" /> -->
  6. <!-- 指定列的java数据类型 -->
  7. <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
  8. </table>
  9. <table schema="" tableName="t_hibernate_order_item" domainObjectName="OrderItem"
  10. enableCountByExample="false" enableDeleteByExample="false"
  11. enableSelectByExample="false" enableUpdateByExample="false">
  12. <!-- 忽略列,不生成bean 字段 -->
  13. <!-- <ignoreColumn column="FRED" /> -->
  14. <!-- 指定列的java数据类型 -->
  15. <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
  16. </table>

               3.2:在对应的orderMpper.xml和orderItemMpper.xml中配置resultMap节点以及对应的方法:

orderMpper.xml:

  1. <resultMap id="OrderVoMap" type="com.javaxl.ssm.model.vo.OrderVo" >
  2. <result property="orderId" column="order_id"></result>
  3. <result property="orderNo" column="order_no"></result>
  4. <collection property="orderItems" ofType="com.javaxl.ssm.model.OrderItem">
  5. <result property="orderItemId" column="order_item_id"></result>
  6. <result property="oid" column="oid"></result>
  7. <result property="productId" column="product_id"></result>
  8. <result property="quantity" column="quantity"></result>
  9. </collection>
  10. </resultMap>
  11. <select id="queryOrderVoByOrderId" resultMap="OrderVoMap" parameterType="java.lang.Integer">
  12. select * from t_hibernate_order o,t_hibernate_order_item oi
  13. where o.order_id = oi.oid
  14. and o.order_id = #{orderId}
  15. </select>

orderItemMpper.xml:

  1. <resultMap id="OrderVoMap" type="com.javaxl.ssm.model.vo.OrderItemVo" >
  2. <result property="orderItemId" column="order_item_id"></result>
  3. <result property="oid" column="oid"></result>
  4. <result property="productId" column="product_id"></result>
  5. <result property="quantity" column="quantity"></result>
  6. <association property="order" javaType="com.javaxl.ssm.model.Order">
  7. <result property="orderId" column="order_id"></result>
  8. <result property="orderNo" column="order_no"></result>
  9. </association>
  10. </resultMap>
  11. <select id="queryOrderItemVoByOrderItemId" resultMap="OrderVoMap" parameterType="java.lang.Integer">
  12. select * from t_hibernate_order o,t_hibernate_order_item oi
  13. where o.order_id = oi.oid
  14. and oi.order_item_id = #{orderItemId}
  15. </select>

               3.3:在对应的OrderMapper.java和OrderItemsMapper.java中配置出对应的方法:

OrderMapper.java:

    OrderVo queryOrderVoByOrderId(@Param("orderId") Integer orderId);

OrderItemsMapper.java:

    OrderItemVo queryOrderItemVoByOrderItemId(@Param("orderItemId") Integer orderItemId);

               3.4:之后到service层中接口和实现类中写好方法:

One2ManyService.java:

  1. package com.zj.www.service;
  2. import com.zj.www.vo.OrderItemVo;
  3. import com.zj.www.vo.OrderVo;
  4. public interface One2ManyService {
  5. OrderVo queryOrderVoByOrderId(Integer orderId);
  6. OrderItemVo queryOrderItemVoByOrderItemId(Integer OrderItemId);
  7. }
One2ManyServiceImpl:
  1. package com.zj.www.service.impl;
  2. import com.zj.www.mapper.OrderItemMapper;
  3. import com.zj.www.mapper.OrderMapper;
  4. import com.zj.www.service.One2ManyService;
  5. import com.zj.www.vo.OrderItemVo;
  6. import com.zj.www.vo.OrderVo;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.stereotype.Service;
  9. @Service
  10. public class One2ManyServiceImpl implements One2ManyService {
  11. @Autowired
  12. private OrderMapper orderMapper;
  13. @Autowired
  14. private OrderItemMapper orderItemMapper;
  15. @Override
  16. public OrderVo queryOrderVoByOrderId(Integer orderId) {
  17. return orderMapper.queryOrderVoByOrderId(orderId);
  18. }
  19. @Override
  20. public OrderItemVo queryOrderItemVoByOrderItemId(Integer OrderItemId) {
  21. return orderItemMapper.queryOrderItemVoByOrderItemId(OrderItemId);
  22. }
  23. }

               3.5:之后配置两个表之间的关系,建立vo类:

两个表之间的关系:

             1、一个订单有多个订单详情表

             2、一个订单详情只有一个订单

ordervo:

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

orderItemVo:

  1. package com.zj.www.vo;
  2. import com.zj.www.model.Order;
  3. import com.zj.www.model.OrderItem;
  4. public class OrderItemVo extends OrderItem {
  5. private Order order;
  6. public Order getOrder() {
  7. return order;
  8. }
  9. public void setOrder(Order order) {
  10. this.order = order;
  11. }
  12. @Override
  13. public String toString() {
  14. return "OrderItemVo{" +
  15. "order=" + order +
  16. '}';
  17. }
  18. }

               3.6:建立实体类:

One2ManyService:
  1. package com.zj.www.service.impl;
  2. import com.zj.www.model.OrderItem;
  3. import com.zj.www.service.One2ManyService;
  4. import com.zj.www.vo.OrderItemVo;
  5. import com.zj.www.vo.OrderVo;
  6. import junit.framework.TestCase;
  7. import org.junit.Test;
  8. import org.junit.runner.RunWith;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.test.context.ContextConfiguration;
  11. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  12. @RunWith(SpringJUnit4ClassRunner.class)
  13. @ContextConfiguration(locations={"classpath:applicationContext.xml"})
  14. public class One2ManyServiceImplTest extends TestCase {
  15. @Autowired
  16. private One2ManyService one2ManyService;
  17. @Test
  18. public void queryOrderVoByOrderId() {
  19. OrderVo orderVo = this.one2ManyService.queryOrderVoByOrderId(8);
  20. System.out.println(orderVo);
  21. for (OrderItem orderItem : orderVo.getOrderItems()) {
  22. System.out.println(orderItem);
  23. }
  24. }
  25. @Test
  26. public void queryOrderItemVoByOrderItemId() {
  27. OrderItemVo orderItemVo = this.one2ManyService.queryOrderItemVoByOrderItemId(49);
  28. System.out.println(orderItemVo);
  29. System.out.println(orderItemVo.getOrder());
  30. }
  31. }

运行结果:

通过订单id产查找订单详情:

通过订单详情id查找对应的订单:

表结构:

订单详情表: 

订单表:

二、多对多

           1、多对多关系就是和一对多的关系的大同小异,只是多对多的关系可以看成两个一对多关系。

           2、多对多关系的配置的步骤和一对多关系配置是一样的:(以书籍表、类别表以及书籍类别表为例)

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

  1. <table schema="" tableName="t_hibernate_book" domainObjectName="HBook"
  2. enableCountByExample="false" enableDeleteByExample="false"
  3. enableSelectByExample="false" enableUpdateByExample="false">
  4. <!-- 忽略列,不生成bean 字段 -->
  5. <!-- <ignoreColumn column="FRED" /> -->
  6. <!-- 指定列的java数据类型 -->
  7. <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
  8. </table>
  9. <table schema="" tableName="t_hibernate_book_category" domainObjectName="HBookC"
  10. enableCountByExample="false" enableDeleteByExample="false"
  11. enableSelectByExample="false" enableUpdateByExample="false">
  12. <!-- 忽略列,不生成bean 字段 -->
  13. <!-- <ignoreColumn column="FRED" /> -->
  14. <!-- 指定列的java数据类型 -->
  15. <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
  16. </table>
  17. <table schema="" tableName="t_hibernate_category" domainObjectName="HCate"
  18. enableCountByExample="false" enableDeleteByExample="false"
  19. enableSelectByExample="false" enableUpdateByExample="false">
  20. <!-- 忽略列,不生成bean 字段 -->
  21. <!-- <ignoreColumn column="FRED" /> -->
  22. <!-- 指定列的java数据类型 -->
  23. <!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" /> -->
  24. </table>

                 2.2在对应的HbookCategoryMapper.xml配置resultMap节点以及对应的方法:(为什么只在HbookCategoryMapper.xml中进行配置,因为只是一个中间表,可以起到一个连接的动作):

  1. <resultMap id="HbookVoMap" type="com.zj.www.vo.HBookVo">
  2. <result property="bookId" column="book_id"></result>
  3. <result property="bookName" column="book_name"></result>
  4. <collection property="categories" ofType="com.zj.www.model.HCate">
  5. <result property="categoryId" column="category_id"></result>
  6. <result property="categoryName" column="category_name"></result>
  7. </collection>
  8. </resultMap>
  9. <resultMap id="CategoryVoMap" type="com.zj.www.vo.HCateVo">
  10. <result property="categoryId" column="category_id"></result>
  11. <result property="categoryName" column="category_name"></result>
  12. <collection property="hbooks" ofType="com.zj.www.model.HBook">
  13. <result property="bookId" column="book_id"></result>
  14. <result property="bookName" column="book_name"></result>
  15. </collection>
  16. </resultMap>
  17. <select id="queryByBookId" resultMap="HbookVoMap" parameterType="java.lang.Integer">
  18. select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
  19. where b.book_id = bc.bid and bc.cid = c.category_id and b.book_id = #{bookId}
  20. </select>
  21. <select id="queryByCid" resultMap="CategoryVoMap" parameterType="java.lang.Integer">
  22. select * from t_hibernate_book b,t_hibernate_book_category bc,t_hibernate_category c
  23. where b.book_id = bc.bid and bc.cid = c.category_id and c.category_id=#{cid}
  24. </select>

               2.3:在对应的HbookCategoryMapper.java文件中写好方法:

  1. package com.zj.www.mapper;
  2. import com.zj.www.model.HBookC;
  3. import com.zj.www.vo.HBookVo;
  4. import com.zj.www.vo.HCateVo;
  5. import org.apache.ibatis.annotations.Param;
  6. public interface HBookCMapper {
  7. int deleteByPrimaryKey(Integer bcid);
  8. int insert(HBookC record);
  9. int insertSelective(HBookC record);
  10. HBookC selectByPrimaryKey(Integer bcid);
  11. int updateByPrimaryKeySelective(HBookC record);
  12. int updateByPrimaryKey(HBookC record);
  13. HBookVo queryByBookId(@Param("bookId") Integer bookId);
  14. HCateVo queryByCid(@Param("cid") Integer cid);
  15. }

               2.4之后到service层中接口和实现类中写好方法

Many2ManyService:
  1. package com.zj.www.service;
  2. import com.zj.www.vo.HBookVo;
  3. import com.zj.www.vo.HCateVo;
  4. public interface Many2ManyService {
  5. HBookVo queryByBookId(Integer bookId);
  6. HCateVo queryByCid(Integer cid);
  7. }
Many2ManyServiceImpl:
  1. package com.zj.www.service.impl;
  2. import com.zj.www.mapper.HBookCMapper;
  3. import com.zj.www.service.Many2ManyService;
  4. import com.zj.www.vo.HBookVo;
  5. import com.zj.www.vo.HCateVo;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.stereotype.Service;
  8. @Service
  9. public class Many2ManyServiceImpl implements Many2ManyService {
  10. @Autowired
  11. private HBookCMapper hBookCMapper;
  12. @Override
  13. public HBookVo queryByBookId(Integer bookId) {
  14. return hBookCMapper.queryByBookId(bookId);
  15. }
  16. @Override
  17. public HCateVo queryByCid(Integer cid) {
  18. return hBookCMapper.queryByCid(cid);
  19. }
  20. }

               2.5:之后配置两个表之间的关系,建立vo类:

HBookVo:

  1. package com.zj.www.vo;
  2. import com.zj.www.model.HBook;
  3. import com.zj.www.model.HBookC;
  4. import com.zj.www.model.HCate;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. public class HBookVo extends HBook {
  8. private List<HCate> categories = new ArrayList<>();
  9. public List<HCate> getCategories() {
  10. return categories;
  11. }
  12. public void setCategories(List<HCate> categories) {
  13. this.categories = categories;
  14. }
  15. }

HBookC:

  1. package com.zj.www.vo;
  2. import com.zj.www.model.HBook;
  3. import com.zj.www.model.HCate;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. public class HCateVo extends HCate {
  7. private List<HBook> hbooks = new ArrayList<>();
  8. public List<HBook> getHbooks() {
  9. return hbooks;
  10. }
  11. public void setHbooks(List<HBook> hbooks) {
  12. this.hbooks = hbooks;
  13. }
  14. @Override
  15. public String toString() {
  16. return "HCateVo{" +
  17. "hbooks=" + hbooks +
  18. '}';
  19. }
  20. }

               2.6:编写测试类:

  1. package com.zj.www.service.impl;
  2. import com.zj.www.model.HBook;
  3. import com.zj.www.model.HCate;
  4. import com.zj.www.service.Many2ManyService;
  5. import com.zj.www.vo.HBookVo;
  6. import com.zj.www.vo.HCateVo;
  7. import junit.framework.TestCase;
  8. import org.junit.Test;
  9. import org.junit.runner.RunWith;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.test.context.ContextConfiguration;
  12. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  13. @RunWith(SpringJUnit4ClassRunner.class)
  14. @ContextConfiguration(locations={"classpath:applicationContext.xml"})
  15. public class Many2ManyServiceImplTest extends TestCase {
  16. @Autowired
  17. private Many2ManyService many2ManyService;
  18. @Test
  19. public void queryByBookId() {
  20. HBookVo hbookVo = many2ManyService.queryByBookId(8);
  21. System.out.println(hbookVo);
  22. for (HCate category : hbookVo.getCategories()) {
  23. System.out.println(category);
  24. }
  25. }
  26. @Test
  27. public void queryByCid() {
  28. HCateVo categoryVo = many2ManyService.queryByCid(8);
  29. System.out.println(categoryVo);
  30. for (HBook hbook : categoryVo.getHbooks()) {
  31. System.out.println(hbook);
  32. }
  33. }
  34. }

运行结果:

1、通过书籍id找出对应的类别名称:

 sql语句:

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 = ? 

书籍表: 

书籍类别表:

类别表:

2、通过类别id查找出对应的书籍名称:

运行结果:

 sql语句: 

 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=? 

逻辑推理:

类别表: 

 书籍类别表:

书籍表:

 

三、总结:

mybatis的关联关系和Hibernate的关联关系大同小异

其中要注意的是:配置关系Vo类和在xxxMap.xml中的配置:

里面用的是collection--->oftype

<collection property="categories" ofType="com.zj.www.model.HCate">

                   association--->javaType

<association property="order" javaType="com.zj.www.model.Order">

今天的分享就到这里了,希望能够帮助到你!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/625670
推荐阅读
相关标签