目录:

1.一对多关联关系

2. 多对多关联关系

首先最经典的案例体现一对多就是一个订单对应多个订单项
所以我们先用逆向生成工具生成订单表和订单项两张表的mapper和model
涉及表:t_hibernate_book、t_hibernate_book_category、t_hibernate_category

OrderVo

package com.liuxia.ssm.model.vo;import com.liuxia.ssm.model.Order;
import com.liuxia.ssm.model.OrderItem;import java.util.ArrayList;
import java.util.List;public class OrderVo extends Order {private List<OrderItem> orderItems = new ArrayList();public List<OrderItem> getOrderItems() {return orderItems;}public void setOrderItems(List<OrderItem> orderItems) {this.orderItems = orderItems;}
}

OrderItemVo

package com.liuxia.ssm.model.vo;import com.liuxia.ssm.model.Order;
import com.liuxia.ssm.model.OrderItem;public class OrderItemVo extends OrderItem {private Order order;public Order getOrder() {return order;}public void setOrder(Order order) {this.order = order;}
}

然后再Mapper中写一个查询订单的方法

OrderMapper

package com.liuxia.ssm.mapper;import com.liuxia.ssm.model.Order;
import com.liuxia.ssm.model.vo.OrderVo;
import org.apache.ibatis.annotations.Param;import java.util.List;public interface OrderMapper {int deleteByPrimaryKey(Integer orderId);int insert(Order record);int insertSelective(Order record);Order selectByPrimaryKey(Integer orderId);int updateByPrimaryKeySelective(Order record);int updateByPrimaryKey(Order record);List<OrderVo> selectByOrderId(@Param("orderId") Integer orderId);
}

然后在OrderMapper.xml中配置好一对多关联关系的映射

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.liuxia.ssm.mapper.OrderMapper" ><resultMap id="BaseResultMap" type="com.liuxia.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.liuxia.ssm.model.vo.OrderVo"><result property="orderId" column="order_id"></result><result property="orderNo" column="order_no"></result><!--<result property="orderItems"></result>--><collection property="orderItems" ofType="com.liuxia.ssm.model.OrderItem"><result property="orderItemId" column="order_item_id"></result><result property="productId" column="product_id"></result><result property="quantity" column="quantity"></result><result property="oid" column="oid"></result></collection></resultMap><sql id="Base_Column_List" >order_id, order_no</sql><select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >select <include refid="Base_Column_List" />from t_hibernate_orderwhere order_id = #{orderId,jdbcType=INTEGER}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >delete from t_hibernate_orderwhere order_id = #{orderId,jdbcType=INTEGER}</delete><insert id="insert" parameterType="com.liuxia.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.liuxia.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.liuxia.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.liuxia.ssm.model.Order" >update t_hibernate_orderset order_no = #{orderNo,jdbcType=VARCHAR}where order_id = #{orderId,jdbcType=INTEGER}</update><select id="selectByOrderId" resultMap="OrderVoMap" parameterType="java.lang.Integer" >select * from t_hibernate_order o,t_hibernate_order_item oi where o.order_id=oi.oid and oi.oid = #{orderId}</select>
</mapper>

然后在OrderItemMapper.xml中配置好多个订单项对应一个订单的关系

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.liuxia.ssm.mapper.OrderItemMapper" ><resultMap id="BaseResultMap" type="com.liuxia.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.liuxia.ssm.model.vo.OrderItemVo"><result property="orderItemId" column="order_item_id"></result><result property="productId" column="product_id"></result><result property="quantity" column="quantity"></result><result property="oid" column="oid"></result><!--<result property="orderItems"></result>--><association property="order" javaType="com.liuxia.ssm.model.Order"><result property="orderId" column="order_id"></result><result property="orderNo" column="order_no"></result></association></resultMap><sql id="Base_Column_List" >order_item_id, product_id, quantity, oid</sql><select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >select <include refid="Base_Column_List" />from t_hibernate_order_itemwhere order_item_id = #{orderItemId,jdbcType=INTEGER}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >delete from t_hibernate_order_itemwhere order_item_id = #{orderItemId,jdbcType=INTEGER}</delete><insert id="insert" parameterType="com.liuxia.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.liuxia.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.liuxia.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.liuxia.ssm.model.OrderItem" >update t_hibernate_order_itemset product_id = #{productId,jdbcType=INTEGER},quantity = #{quantity,jdbcType=INTEGER},oid = #{oid,jdbcType=INTEGER}where order_item_id = #{orderItemId,jdbcType=INTEGER}</update><select id="selectByOrderItemId" resultMap="OrderItemVoMap" parameterType="java.lang.Integer" >select * from t_hibernate_order o,t_hibernate_order_item oi where o.order_id=oi.oidand oi.order_item_id = #{orderItemId}</select>
</mapper>

OneToManyService

package com.liuxia.ssm.service;import com.liuxia.ssm.model.vo.OrderItemVo;
import com.liuxia.ssm.model.vo.OrderVo;import java.util.List;public interface OneToManyService {List<OrderVo> selectByOrderId(Integer orderId);List<OrderItemVo> selectByOrderItemId(Integer orderItemId);}

OneToManyServiceImpl

package com.liuxia.ssm.service.impl;import com.liuxia.ssm.mapper.OrderItemMapper;
import com.liuxia.ssm.mapper.OrderMapper;
import com.liuxia.ssm.model.vo.OrderItemVo;
import com.liuxia.ssm.model.vo.OrderVo;
import com.liuxia.ssm.service.OneToManyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.util.List;@Service
public class OneToManyServiceImpl implements OneToManyService {@Autowiredprivate OrderMapper orderMapper;@Autowiredprivate OrderItemMapper orderItemMapper;@Overridepublic List<OrderVo> selectByOrderId(Integer orderId) {return orderMapper.selectByOrderId(orderId);}@Overridepublic List<OrderItemVo> selectByOrderItemId(Integer orderItemId) {return orderItemMapper.selectByOrderItemId(orderItemId);}
}

OneToManyServiceImplTest

package com.liuxia.ssm.service.impl;import com.liuxia.ssm.model.OrderItem;
import com.liuxia.ssm.model.vo.OrderItemVo;
import com.liuxia.ssm.model.vo.OrderVo;
import com.liuxia.ssm.service.OneToManyService;
import com.liuxia.ssm.test.SpringJunitBaseTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;import java.util.List;import static org.junit.Assert.*;public class OneToManyServiceImplTest extends SpringJunitBaseTest {@Autowiredprivate OneToManyService oneToManyService;@Beforepublic void setUp() throws Exception {}@Afterpublic void tearDown() throws Exception {}@Testpublic void selectByOrderId() {List<OrderVo> orderVos = oneToManyService.selectByOrderId(8);OrderVo orderVo = orderVos.get(0);System.out.println(orderVo);for (OrderItem orderItem : orderVo.getOrderItems()) {System.out.println(orderItem);}}@Testpublic void selectByOrderItemId() {List<OrderItemVo> orderItemVos = oneToManyService.selectByOrderItemId(36);OrderItemVo orderItemVo = orderItemVos.get(0);System.out.println(orderItemVo);System.out.println(orderItemVo.getOrder());}
}

然后可以看到查询出订单为8的订单并把它所有订单项也查出来了

多对多关联关系

我们多对多的配置和一对多是差不多的,多对多:多个一对多
这里是以一个书籍类别对应多本书,而一本书又对应多个书籍类别,通过一个中间表来实现多对多

涉及表:t_hibernate_book、t_hibernate_book_category、t_hibernate_category

HbookVo


package com.liuxia.ssm.model.vo;import com.liuxia.ssm.model.Category;
import com.liuxia.ssm.model.Hbook;import java.util.ArrayList;
import java.util.List;public class HbookVo extends Hbook {private List<Category> categories = new ArrayList();public List<Category> getCategories() {return categories;}public void setCategories(List<Category> categories) {this.categories = categories;}
}

CategoryVo

package com.liuxia.ssm.model.vo;import com.liuxia.ssm.model.Category;
import com.liuxia.ssm.model.Hbook;
import org.springframework.stereotype.Repository;import java.util.ArrayList;
import java.util.List;@Repository
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;}
}

中间表配置文件,HbookCategoryMapper.xml,基本配置都在里面了


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.liuxia.ssm.mapper.HbookCategoryMapper" ><resultMap id="BaseResultMap" type="com.liuxia.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><sql id="Base_Column_List" >bcid, bid, cid</sql><select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >select <include refid="Base_Column_List" />from t_hibernate_book_categorywhere bcid = #{bcid,jdbcType=INTEGER}</select><delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >delete from t_hibernate_book_categorywhere bcid = #{bcid,jdbcType=INTEGER}</delete><insert id="insert" parameterType="com.liuxia.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.liuxia.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.liuxia.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.liuxia.ssm.model.HbookCategory" >update t_hibernate_book_categoryset bid = #{bid,jdbcType=INTEGER},cid = #{cid,jdbcType=INTEGER}where bcid = #{bcid,jdbcType=INTEGER}</update>
</mapper>

HbookCategoryMapper


package com.liuxia.ssm.mapper;import com.liuxia.ssm.model.HbookCategory;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);
}

ManyToManyServiceImplTest

package com.liuxia.ssm.service.impl;import com.liuxia.ssm.model.Category;
import com.liuxia.ssm.model.Hbook;
import com.liuxia.ssm.model.vo.CategoryVo;
import com.liuxia.ssm.model.vo.HbookVo;
import com.liuxia.ssm.service.ManyToManyService;
import com.liuxia.ssm.test.SpringJunitBaseTest;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;import static org.junit.Assert.*;public class ManyToManyServiceImplTest extends SpringJunitBaseTest {@Autowiredprivate ManyToManyService manyToManyService;@Testpublic void selectByBid() {HbookVo hbookVo = manyToManyService.selectByBid(8);System.out.println(hbookVo);for (Category category : hbookVo.getCategories()) {System.out.println(category);}}@Testpublic void selectByCid() {CategoryVo categoryVo = this.manyToManyService.selectByCid(8);System.out.println(categoryVo);for (Hbook hbook : categoryVo.getHbooks()) {System.out.println(hbook);}}
}

Mybatis关联关系相关推荐

  1. mybatis关联关系映射

    课程目标 1.一对多关联关系 2.多对多关联关系 一对多 首先先用逆向生成工具生成t_hibernate_order.t_hibernate_order_item 这两张表对应的model与mappe ...

  2. 黑马程序员JAVAEE企业级开发应用教程笔记

    1 Spring 目标: ApplicationContext容器使用 属性setter方法注入的实现(掌握) Spring中的IoC和DI(熟悉) Spring的概念和优点(了解) 1.1 概述 S ...

  3. mybatis--关联关系

    mybatis  关联关系 我这里有   用户表(id    username) 家庭住址(id address userid)userid 是用户表的id 一.多对一关系: 1. entity实体类 ...

  4. CGB2110-DAY09-Mybatis

    文章目录 1.Mybatis学习 1.1 动态Sql-where条件 1.1.1 编辑测试类 1.1.2 编辑Mapper接口 1.1.3 编辑Mapper 映射文件 1.2 动态Sql-Set标签 ...

  5. Mybatis之关联关系映射

    目录 一.一对多 1.Mybatis的关联关系和Hibernate的关联关系大同小异. 2.关于一对多的关系: 3.关于Mybatis一对多的关系配置步骤:(以Order和OrderItem为例) 3 ...

  6. mybatis之关联关系映射(一对多和多对多关系)

    今日目标: 一对多关联关系 多对多关联关系 本篇博客是在前四篇的基础上在进行进一步开发的 (四) mybatis集成ehcache&&mybatis集成redis_m0_5852594 ...

  7. MyBatis学习笔记(三) 关联关系

    首先给大家推荐几个网页: http://blog.csdn.net/isea533/article/category/2092001 没事看看 - MyBatis工具:www.mybatis.tk h ...

  8. ef关联多实体查询_Mybatis基本知识十二:关联关系查询之延迟加载:侵入式延迟加载...

    上一篇文章:<Mybatis基本知识十一:关联关系查询之延迟加载策略:直接加载> 若文中有纰漏,请多多指正!!! 1.前言 延续上一章节,本章节主要讲解和演示在关联关系查询中侵入式延迟加载 ...

  9. 放弃MyBatis!我选择 JDBCTemplate!

    欢迎关注方志朋的博客,回复"666"获面试宝典 来源:segmentfault.com/a/1190000018472572 因为项目需要选择数据持久化框架,看了一下主要几个流行的 ...

  10. 另一种思考:为什么不选JPA、MyBatis,而选择JDBCTemplate?

    点击上方蓝色"方志朋",选择"设为星标"回复"666"获取独家整理的学习资料! 因为项目需要选择数据持久化框架,看了一下主要几个流行的和不流 ...

最新文章

  1. 数位DP 回文序列 POJ-3280 Cheapest Palindrome
  2. 云计算使关系数据库逐渐落伍(转载)
  3. ABAP的HTTP_GET和Linux的curl
  4. springapplication.run运行多个应用_编程的十二要素应用宣言
  5. [Swift]LeetCode793. 阶乘函数后K个零 | Preimage Size of Factorial Zeroes Function
  6. Centos 7安装与配置chef
  7. 【C语言简单说】五:常用运算符
  8. base32php,ThinkPHP6.0使用extends Base方式处理后台登录逻辑
  9. strace监视系统调用
  10. redis 高可用切换_Redis高可用架构演进
  11. PyCharm中的快捷键不能用怎么办?(复制粘贴)
  12. android访问链接,尝试使用Android访问本地Web服务时连接被拒...
  13. 俄罗斯方块c语言代码 vc 6.0,VC++6.0俄罗斯方块代码
  14. Linux命令大全详解
  15. 微生物生态排序分析——CCA分析
  16. 数据库备份的方式有哪些
  17. 10个物联网应用的案例
  18. Python调用百度API进行人像动漫化
  19. chrome浏览器强制采用https加密链接
  20. 0到3个月的宝宝护理重点和注意事项,家长要记住哦

热门文章

  1. Nacos一致性协议 CP/AP/JRaft/Distro协议
  2. Android、Python实现微信运动
  3. 一些游戏用到的渲染技术
  4. 关于three.js 抗锯齿的理解
  5. VsCode开发Flutter 连接夜神模拟器
  6. S32K的flash组件使用(操作FLASH)
  7. 网易公开课APP如何修改视频缓存地址
  8. 高效能人士的七个习惯读后感与总结概括-(第七章,第八章,第九章)
  9. java实现公式解析
  10. qpython3怎么运行代码_关于使用qpython和qpython3写程序