一对一

 <!-- 关系映射 -->
<!-- 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. TensorFlow GPU版本的安装与调试

    笔者采用python3.6.7+TensorFlow1.12.0+CUDA10.0+CUDNN7.3.1构建环境 PC端配置为GTX 1050+Intel i7 7700HQ 4核心8线程@2.8GH ...

  2. 生产者和消费者模型producer and consumer(单线程下实现高并发)

    #1.生产者和消费者模型producer and consumer modelimport timedef producer(): ret = [] for i in range(2): time.s ...

  3. Codeforces Round #607 (Div. 2) 题解

    Suffix Three Azamon Web Services Cut and Paste Beingawesomeism Jeremy Bearimy Miss Punyverse Suffix ...

  4. Linux性能优化实战学习笔记:第二十七讲

    一.案例环境描述 1.环境准备 2CPU,4GB内存 预先安装docker sysstat工具 2.温馨提示 案例中 Python 应用的核心逻辑比较简单,你可能一眼就能看出问题,但实际生产环境中的源 ...

  5. java web开发入门七(mybatis)基于intellig idea

    mybatis 一. 入门开发步骤 1.导入相关jar包 mybatis3.4.2核心开发包 asm-5.1.jarcglib-3.2.4.jarcommons-logging-1.1.3.jarlo ...

  6. 第22课 weak_ptr弱引用智能指针

    一. weak_ptr的概况 (一)weak_ptr的创建 1. 直接初始化:weak_ptr<T> wp(sp); //其中sp为shared_ptr类型 2. 赋值: wp1 = sp ...

  7. 利用Jenkins打包并远程部署NodeJS应用

    本文Jenkins版本2.190.2,为19年11月最新 1.安装Jenkins.Git和NodeJS Jenkins安装参考:https://www.cnblogs.com/zhi-leaf/p/1 ...

  8. SDK-基于Windows环境搭建

    SDK安装配置 前言:相信很多小伙伴还不会搭SDK,近日一位前同事询问我SDK怎么搭建了?不妨看看吧,小编是基于appium. 1.下载SDK:http://tools.android-studio. ...

  9. Extra:Cg Math Functions

    常用Cg函数 数学函数 abs(x):绝对值 // float类型的实现 float abs(float x) { return max(-a, a); } sin(x):正弦,输入为弧度 // fl ...

  10. commitizen规范代码提交

    转载链接:https://www.jianshu.com/p/bd712e42f2e9 参考链接:https://segmentfault.com/a/1190000009048911 平时提交的变动 ...