一对一

 <!-- 关系映射 -->
<!-- 1-1:自动映射 -->
<select id="oneToOne" resultType="UserView">
select u.*,c.num from user u,card c where u.id=c.per_fk
</select> <!-- 1-1:手动映射之级联查询 -->
<resultMap type="card" id="cardrs">
<result property="num" column="num"/>
</resultMap>
<resultMap type="user" id="commrs">
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
</resultMap>
<resultMap type="user" id="oTors" extends="commrs">
<!-- <id column="id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/> -->
<!--association:完成自定义类型的映射
property:User类中自定义类型(Card)的属性名
javaType:指定对应属性的类型-->
<association property="card" javaType="card" resultMap="cardrs">
<!-- <result property="num" column="num"/> -->
</association>
</resultMap>
<select id="oneToOne1" resultMap="oTors">
select u.*,c.num from user u,card c where u.id=c.per_fk
</select>

一对一(嵌套查询例)

<!-- 1-1:手动映射之嵌套查询(分步)  -->
<resultMap type="User" id="oTors2" >
<!-- <id column="id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/> -->
<!-- select:调用另外一条sql语句(命名空间.sql语句id)
column:在第一个结果集中用来关联查询的列名 -->
<association property="card" javaType="card"
select="com.offcn.dao.UserDao.getCardByUid" column="id"></association>
</resultMap>
<select id="oneToOne2" resultMap="oTors2">
select * from user
</select> <select id="getCardByUid" parameterType="int" resultType="card">
select * from card where per_fk=#{uid}
</select>

一对多

<!-- 1-n:关联查询 -->
<resultMap type="Orders" id="orderrs">
<id column="oid" property="id"></id>
<result column="number" property="number"/>
</resultMap>
<resultMap type="user" id="oTmrs" extends="commrs">
<collection property="olist" ofType="Orders" resultMap="orderrs">
<id column="oid" property="id"></id>
<result column="number" property="number"/>
</collection>
</resultMap>
<select id="oneToMany" resultMap="oTmrs">
select u.*,o.id oid,o.number from user u,orders o where u.id=o.userId
</select> <!--1-n:嵌套查询-->
<resultMap type="user" id="oTmrs2">
<collection property="olist" ofType="Orders"
select="com.offcn.dao.UserDao.getOrdersByUid" column="id"></collection>
</resultMap>
<select id="oneToMany2" resultMap="oTmrs2">
select * from user
</select> <select id="getOrdersByUid" parameterType="int" resultType="Orders">
select * from orders where userId=#{uid}
</select>

多对多

<resultMap type="user" id="mTmrs">
<result column="username" property="username"/>
<collection property="olist" ofType="Orders">
<result property="number" column="number"/> <collection property="dlist" ofType="Orderdetail">
<result column="itemsNum" property="itemsNum"/> <association property="items" javaType="Items">
<result column="name" property="name"/>
<result column="price" property="price"/>
</association>
</collection>
</collection>
</resultMap>
<select id="manyToMany" resultMap="mTmrs">
select u.username,o.number,i.name,i.price,d.itemsNum
from user u,orders o,orderdetail d,items i
where u.id=o.userId and o.id=d.ordersId and d.itemsId=i.id
</select>

嵌套查询和关联查询的区别:

1、关联查询是使用一条sql语句对数据库进行查询,在查询后根据查询结果将数据和自定义的resultMap集合返回结果

2、嵌套查询根据查询的内容分表查询,每个表至少查询一次,查询的结果嵌套使用。

3、关联查询的结果集必须将所有的属性都配置,没有配置的不会映射,嵌套查询如果有相同的属性会自动映射数据到对象

Mybatis一对一,一对多,多对多代码的更多相关文章

  1. mybatis 一对一 一对多 多对多

    一对一 一对多 多对多

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

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

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

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

  4. 使用NHibernate(7)-- 一对一 && 一对多 && 多对多

    1, 一对一. 对于数据量比较大的时候,考虑查询的性能,肯能会把一个对象的属性分到两个表中存放:比如用户和用户资料,经常使用的一般是Id和用户名,用户资料(学校,籍贯等)是不经常被查询的,所以就会分成 ...

  5. day 69-70 一对一 一对多 多对一联表查询

    day 69 orm操作之表关系,多对多,多对一 多对一/一对多, 多对多{类中的定义方法} day69 1. 昨日内容回顾 1. 单表增删改查 2. 单表查询API 返回QuerySet对象的: 1 ...

  6. JPA 一对一 一对多 多对一 多对多配置

    1 JPA概述 1.1 JPA是什么 JPA (Java Persistence API) Java持久化API.是一套Sun公司 Java官方制定的ORM 方案,是规范,是标准 ,sun公司自己并没 ...

  7. MyBatis的关联关系 一对一 一对多 多对多

    一对一示例 一个妻子对应一个丈夫 数据库表设计时 在妻子表中添加一个丈夫主键的作为外键 1 对应的JavaBean代码虽然在数据库里只有一方配置的外键,但是这个一对一是双向的关系. Husband实体 ...

  8. 初学者易上手的SSH-hibernate04 一对一 一对多 多对多

    这章我们就来学习下hibernate的关系关联,即一对一(one-to-one),一对多(one-to-many),多对多(many-to-many).这章也将是hibernate的最后一章了,用于初 ...

  9. EntityFrameworkCore 一对一 && 一对多 && 多对多配置

    基本数据结构 表设计如下: 入学记录 public class AdmissionRecord { [Key] public long Id { get; set; } public DateTime ...

  10. JAVA日记之mybatis-3一对一,一对多,多对多xml与注解配置

    1.Mybatis多表查询1.1 一对一查询1.1.1 一对一查询的模型用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户 一对一查询的需求:查询一个订单,与此同时查询出该订单所属的 ...

随机推荐

  1. Google开源PDF软件库

    Google开启了一个叫做PDFium的PDF软件库开源项目,开发人员能够将其纳入各种平台应用中. 据Google的Chromium项目的布道师François Beaufort称,PDFium将被包 ...

  2. 老男孩LINUX--打包压缩、查看、解压

    tar就是打包的意思,打包就是将多个文件或者目录放置到一起,整体的大小没有变化,tar可以调用一些压缩的软件,比如zip,在打包的同时进行压缩.先来上一个例子: tar zcvf /tmp/etc.t ...

  3. Linux for Matlab中文注释乱码(亲测有效)

    中文注释乱码的原因是windows下的m文件采用的是gb2312编码,只要将所有的m文件转成 utf8文件,显示就正常了. 1.首先安装enca:sudo apt-get install enca 2 ...

  4. LG5357 「模板」AC自动机(二次加强版) AC自动机+fail树

    问题描述 LG5357 题解 不是fail树的AC自动机复杂度是假的. 把AC自动机搞出来,建立Trie树,树上爆搜一遍就好了. \(\mathrm{Code}\) #include<bits/ ...

  5. 生产者消费者模型-Java代码实现

    什么是生产者-消费者模式 比如有两个进程A和B,它们共享一个固定大小的缓冲区,A进程产生数据放入缓冲区,B进程从缓冲区中取出数据进行计算,那么这里其实就是一个生产者和消费者的模式,A相当于生产者,B相 ...

  6. GDB 调试C++

    原来比较熟悉用gdb调试C程序,没有用过gdb调试C++程序,原理上没有什么区别.在形式上有一些区别,因为C++支持名字空间和class等机制,把函数的可见域做了隔离. 拿envoy的代码作个例子: ...

  7. [LeetCode] 381. Insert Delete GetRandom O(1) - Duplicates allowed 常数时间内插入删除和获得随机数 - 允许重复

    Design a data structure that supports all following operations in average O(1) time. Note: Duplicate ...

  8. Linux网络编程综合运用之MiniFtp实现(一)

    春节过后,万物复苏,在这元宵佳节的前一天,决定继续开启新年的学习计划,生命在于运动,提高源于学习,在经过漫长的Linux网络编程学习后,接下来会以一个综合的小项目来将所学的知识点综合运用,首先是对项目 ...

  9. JavaScript (JS)常用方法

    被按的按键的 unicode 是? <html> <head> <script type="text/javascript"> function ...

  10. 各版本linux推荐的软件源

    x64 Ubuntu 18.4 deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ bionic main restricted universe mul ...