原文:https://blog.csdn.net/m0_37787069/article/details/79247321

1、一对一
关键字:association
作用:针对pojo对象属性的映射
      property:pojo的属性名
      javaType:pojo类名
(1) 嵌套结果: 使用嵌套结果映射来处理重复的联合结果的子集

<resultMap type="com.gec.domain.Person" id="basePersonResultMap">
<id column="id" property="personId"/>
<result column="name" property="name"/>
<result column="sex" property="sex"/>
<result column="age" property="age"/> <association property="card" javaType="com.gec.domain.Card">
<!-- 、构造器注入
<constructor>
<idArg column="id" javaType="int"/>
<arg column="code" javaType="string"/>
</constructor>
-->
<!-- 、setter注入 -->
<result column="id" property="cardId"/>
<result column="code" property="code"/>
</association>
</resultMap>
<select id="queryUserList" resultMap="basePersonResultMap">
select p.id as personId,p.name,p.sex,p.age,c.*
from tbl_person p,tbl_card c where p.card_id=c.id;
</select>

(2) 嵌套查询:通过执行另外一个SQL映射语句来返回预期的复杂类型
    column="引入执行另外定制sql方法的参数值(外键)"
    select="执行定制sql方法名"
PersonMapper2.xml

<resultMap type="com.gec.domain.Person" id="basePersonResultMap">
<id column="id" property="personId"/>
<result column="name" property="name"/>
<result column="sex" property="sex"/>
<result column="age" property="age"/>
<association property="card"
javaType="com.gec.domain.Card"
column="card_id"
select="com.gec.mapper.CardMapper.queryCardById">
</association>
</resultMap>
<select id="queryUserList" resultMap="basePersonResultMap">
select * from tbl_person;
</select>

CardMapper.xml

<resultMap type="com.gec.domain.Card" id="baseCardResultMap">
<id column="card_id" property="cardId"/>
<result column="code" property="code"/>
</resultMap>
<select id="queryCardById" resultMap="baseCardResultMap">
select c.id as card_id,c.code from tbl_card c
where c.id=#{id};
</select>
<resultMap type="com.gec.domain.Card" id="queryCardResultMap" extends="baseCardResultMap">
<association property="person" javaType="com.gec.domain.Person">
<id column="card_id" property="personId"/>
<result column="name" property="name"/>
<result column="sex" property="sex"/>
<result column="age" property="age"/>
</association>
</resultMap>
<select id="queryCardList" resultMap="queryCardResultMap">
SELECT c.id AS card_id, c.code, p.*
FROM tbl_card c,tbl_person p WHERE c.id=p.card_id;
</select>

2 、一对多
mybatis如何实现一对多的实现?(学生与班级)
(1) 嵌套结果:
ClazzMapper.xml

<resultMap type="com.gec.domain.Clazz" id="baseClazzResultMap">
<id column="id" property="clazzId"/>
<result column="clazz_name" property="clazzName"/>
<result column="code" property="code"/>
</resultMap>
<resultMap type="com.gec.domain.Clazz" id="queryClazzList2ResultMap" extends="baseClazzResultMap">
<collection property="studentList" javaType="ArrayList" ofType="com.gec.domain.Student">
<id column="stu_id" property="studentId"/>
<result column="name" property="name"/>
<result column="sex" property="sex"/>
<result column="age" property="age"/>
</collection>
</resultMap>
<select id="queryClazzList2" resultMap="queryClazzList2ResultMap">
SELECT c.*, s.id AS stu_id,s.name,s.sex,s.age
FROM tbl_clazz c LEFT JOIN tbl_student s
ON c.id=s.clazz_id;
</select>

StudentMapper.xml

<resultMap type="com.gec.domain.Student" id="baseStudentResultMap">
<id column="id" property="studentId"/>
<result column="name" property="name"/>
<result column="sex" property="sex"/>
<result column="age" property="age"/>
</resultMap>
<resultMap type="com.gec.domain.Student" id="queryStudentListResultMap" extends="baseStudentResultMap">
<association property="clazz" javaType="com.gec.domain.Clazz">
<id column="cls_id" property="clazzId"/>
<result column="clazz_name" property="clazzName"/>
<result column="code" property="code"/>
</association>
</resultMap>
<select id="queryStudentList" resultMap="queryStudentListResultMap">
SELECT s.*,c.id AS cls_id,c.clazz_name,c.code
FROM tbl_student s,tbl_clazz c WHERE s.clazz_id=c.id;
</select>

(2) 嵌套查询:
ClazzMapper.xml

<resultMap type="com.gec.domain.Clazz" id="baseClazzResultMap">
<id column="id" property="clazzId"/>
<result column="clazz_name" property="clazzName"/>
<result column="code" property="code"/>
</resultMap>
<resultMap type="com.gec.domain.Clazz" id="queryClazzListResultMap" extends="baseClazzResultMap">
<collection property="studentList" javaType="ArrayList"
column="id" ofType="com.gec.domain.Student"
select="com.gec.mapper.StudentMapper.queryStudentByClazzId"
>
</collection>
</resultMap>
<select id="queryClazzList" resultMap="queryClazzListResultMap">
select * from tbl_clazz;
</select>

StudentMapper.xml

<resultMap type="com.gec.domain.Student" id="baseStudentResultMap">
<id column="id" property="studentId"/>
<result column="name" property="name"/>
<result column="sex" property="sex"/>
<result column="age" property="age"/>
</resultMap>
<resultMap type="com.gec.domain.Student" id="queryStudentListResultMap" extends="baseStudentResultMap">
<association property="clazz" javaType="com.gec.domain.Clazz">
<id column="cls_id" property="clazzId"/>
<result column="clazz_name" property="clazzName"/>
<result column="code" property="code"/>
</association>
</resultMap>
<select id="queryStudentList" resultMap="queryStudentListResultMap">
SELECT s.*,c.id AS cls_id,c.clazz_name,c.code
FROM tbl_student s,tbl_clazz c WHERE s.clazz_id=c.id;
</select>
<!-- 根据班级的id,获取学生列表 -->
<select id="queryStudentByClazzId" resultMap="baseStudentResultMap">
select * from tbl_student where clazz_id=#{id};
</select>

3、多对多
商品表、订单表之间就是以多对多关联
商品与订单的关系表
描述多对多的数据表实现
(1)商品pojo:
Article.java

public class Article implements Serializable {
private Integer articleId;
private String name;
private Double price;
private String remark;
private List<Order> orders;
省略setter/gettera方法
}

(2)商品表映射:
ArticleMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.gec.mapper.ArticleMapper">
<resultMap type="article" id="baseArticleResultMap">
<id column="id" property="articleId"/>
<result column="NAME" property="name"/>
<result column="price" property="price"/>
<result column="remark" property="remark"/>
</resultMap>
<resultMap type="article" id="findArtcleByIdResultMap" extends="baseArticleResultMap">
<collection property="orders" javaType="ArrayList"
ofType="com.gec.domain.Article" column="id"
select="com.gec.mapper.OrderMapper.findOrderByArticleId"
>
</collection>
</resultMap>
<!-- 根据订单id查询商品 -->
<select id="findArtcleByOrderId" resultMap="baseArticleResultMap">
select * from tb_article where id
in (select article_id from tb_item where order_id=#{id})
</select>
<select id="findArtcleById" resultMap="findArtcleByIdResultMap">
select * from tb_article where id=#{id}
</select>
</mapper>

(3)订单pojo:
Order.java

public class Order {
private Integer orderid;
private String code;
private Double total;
private List<Article> articles;
省略setter/getter方法
}

(4)订单表映射:
OrderMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.gec.mapper.OrderMapper">
<resultMap type="order" id="baseOrderResultMap">
<id column="orderId" property="orderid"/>
<result column="CODE" property="code"/>
<result column="total" property="total"/>
</resultMap>
<resultMap type="order" id="queryOrderByUserIdRsultMap" extends="baseOrderResultMap">
<collection property="articles" javaType="ArrayList"
ofType="article" column="orderId"
select="com.gec.mapper.ArticleMapper.findArtcleByOrderId">
</collection>
</resultMap>
<!-- 根据商品id查询订单 -->
<select id="findOrderByArticleId" resultMap="baseOrderResultMap">
select * from tb_order where id
in (select order_id from tb_item where article_id=#{id})
</select>
</mapper>

mybatis实现多表一对一,一对多,多对多关联查询的更多相关文章

  1. MyBatis 一对多,多对一关联查询的时候Mapper的顺序

    要先写association,然后写collection:这是由DTD决定的: <resultMap ...> <association ...> </associati ...

  2. Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作

    Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作,单表查询,多表查询 一丶表与表之间的关系 背景: ​ ​ ​  ​ ​ 由于如果只使用一张表存储所有的数据,就会操作数 ...

  3. mybatis 一对一 一对多 多对多

    一对一 一对多 多对多

  4. mybatis实战教程二:多对一关联查询(一对多)

    多对一关联查询 一.数据库关系.article表和user表示多对一的关系 CREATE TABLE `article` ( `id` ) NOT NULL AUTO_INCREMENT, `user ...

  5. JPA级联(一对一 一对多 多对多)注解【实际项目中摘取的】并非自己实际应用

    下面把项目中的用户类中有个:一对一  一对多  多对多的注解对应关系列取出来用于学习      说明:项目运行正常 问题类:一对多.一对一.多对多 ============一对多 一方的设置 @One ...

  6. mybatis 14: 多对一关联查询

    业务背景 根据订单id查询订单的信息,以及该订单所属的客户的基本信息(不包括该客户自己的订单信息) 两张数据表 客户表 订单表 实体类 客户实体类:Customer private Integer i ...

  7. NHibernate教程(11)--多对多关联查询

    本节内容 多对多关系引入 多对多映射关系 多对多关联查询 1.原生SQL关联查询 2.HQL关联查询 3.Criteria API关联查询 结语 多对多关系引入 让我们再次回顾在第二篇中建立的数据模型 ...

  8. [NHibernate]一对多关系(关联查询)

    目录 写在前面 文档与系列文章 一对多查询 总结 写在前面 上篇文章介绍了nhibernate的一对多关系如何配置,以及级联删除,级联添加数据的内容.这篇文章我们将学习nhibernate中的一对多关 ...

  9. Python--day64--找到作者关联的所有书籍对象、ORM多对多关联查询的原理

    找到当前作者关联的所有书籍对象: ORM多对多关联查询的原理: 编辑作者:

随机推荐

  1. queue.Queue()

    一.构造方法 Queue是构造方法,函数签名是Queue(maxsize=0) ,其中maxsize设置队列的大小. 二.实例方法 Queue.qsize(): 返回queue的近似值.注意:qsiz ...

  2. scipy.sparse的一些整理

    一.scipy.sparse中七种稀疏矩阵类型 1.bsr_matrix:分块压缩稀疏行格式 介绍 BSR矩阵中的inptr列表的第i个元素与i+1个元素是储存第i行的数据的列索引以及数据的区间索引, ...

  3. COLA的扩展性使用和源码研究

    cola扩展点使用和设计初探 封装变化,可灵活应对程序的需求变化. 扩展点使用 步骤: 定义扩展点接口,类型可以是校验器,转换器,实体: 必须以ExtPt结尾,表示一个扩展点. 比如,我定义一个云枢的 ...

  4. DQN(Deep Q-learning)入门教程(五)之DQN介绍

    简介 DQN--Deep Q-learning.在上一篇博客DQN(Deep Q-learning)入门教程(四)之Q-learning Play Flappy Bird 中,我们使用Q-Table来 ...

  5. 安全性只是辅助效果?解读DevSecOps的核心动机

    DevSecOps背后的思想仅是对DevOps的扩展.就像开发人员以瀑布式开发风格将项目扔给运营团队以使其在生产中工作一样,即使使用“ DevOps”,安全性也与应用程序开发或运营完全分开. DevS ...

  6. vc程序设计--图形绘制2

    // 实验2.cpp : 定义应用程序的入口点. // #include "framework.h" #include "实验2.h" #define MAX_ ...

  7. AES实现财务数据的加密解密存储

    需求背景 众所周知,金融行业有各种各样的财务报表,有些报表涉及到公司财务或经营相关的敏感数据,需要进行加密存储,只有掌握密钥的用户才能看到解密后的数据.注意,这里所说的加密并不是针对整个数据库或者表全 ...

  8. ES6-for...of与for...in

    1.includes 数组是否包含某个东西 <!DOCTYPE html> <html lang="en"> <head> <meta c ...

  9. Java实现 LeetCode 327 区间和的个数

    327. 区间和的个数 给定一个整数数组 nums,返回区间和在 [lower, upper] 之间的个数,包含 lower 和 upper. 区间和 S(i, j) 表示在 nums 中,位置从 i ...

  10. Java实现 蓝桥杯VIP 算法训练求先序排列

    问题描述 给出一棵二叉树的中序与后序排列.求出它的先序排列.(约定树结点用不同的大写字母表示,长度<=8). 输入格式 两行,每行一个字符串,分别表示中序和后序排列 输出格式 一个字符串,表示所 ...