原文: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. vscode环境配置(三)——解决控制台终端中文输出乱码

    由于系统终端默认编码为GBK,所以需要修改为UTF-8 方法一 打开cmd输入chcp查看编码格式,查看以及修改如下图所示: 方法二

  2. Hibernate实现limit查询报错 :unexpected token: limit near line 1, column 33 [from pojo.Product p order by id limit ?,? ]

    原因: hibernate无法识别limit,  hql语句更不能这样写String hql="from Product p order by id limit ?,? "; 解决 ...

  3. docker常用命令,及进入Tomcat的WebApps发布目录(就是进入docker容器后台目录)

    docker常用命令,及进入Tomcat的WebApps发布目录(就是进入docker容器后台目录)   一.常用命令 1.显示所有的容器,包括未运行的 docker ps -a   2.启动容器.注 ...

  4. JavaScript的基础语法及DOM元素和事件

    一,JavaScript是什么? 1,JavaScript简称:js,是一种浏览器解释型语言,嵌套在HTML文件中交给浏览器解释执行.主要用来实现网页的动态效果,用户交互及前后端的数据传输等. 2,J ...

  5. 织梦系统dedecms实现列表页双样式,列表样式循环交替变化

    有时候做列表页需要交替变换样式,那如何实现列表页双样式呢? 在DeDeCMS里面有这样一个函数,可以循环赋予html代码不同的样式,如下: [field:global function=MagicVa ...

  6. Cypress系列(3)- Cypress 的初次体验

    如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 前言 这里的栗子项目时 Cypress ...

  7. Chisel3 - 复合数据类型

    https://mp.weixin.qq.com/s/rXYqiZKuBpAYL8R94zxgRA   Chisel允许用户根据需要,把基本数据类型组合成为复合数据类型使用.如C语言里面的结构体,这样 ...

  8. MethodHandle(方法句柄)系列之一:MethodHandle和MethodType

        阅读此文章的作者建议先了解java反射和动态代理.       java7中为间接调用方法引入了新的api,其中最关键的是java.lang.invoke包,即方法句柄.我们可以看成是java ...

  9. DDD之3实体和值对象

    图中是一个别墅的模型,代表实体,可以真实的看得到.那么在DDD设计方法论中,实体和值对象是什么呢? 背景 实体和值对象是领域模型中的领域对象,是组成领域模型的基础单元,一起实现实体最基本的核心领域逻辑 ...

  10. Java实现蓝桥杯模拟约数的个数

    问题描述 1200000有多少个约数(只计算正约数). 答案提交 这是一道结果填空的题,你只需要算出结果后提交即可.本题的结果为一个整数,在提交答案时只填写这个整数,填写多余的内容将无法得分. pac ...