Mybatis一对一,一对多,多对多代码
一对一
<!-- 关系映射 -->
<!-- 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一对一,一对多,多对多代码的更多相关文章
- mybatis 一对一 一对多 多对多
一对一 一对多 多对多
- JPA级联(一对一 一对多 多对多)注解【实际项目中摘取的】并非自己实际应用
下面把项目中的用户类中有个:一对一 一对多 多对多的注解对应关系列取出来用于学习 说明:项目运行正常 问题类:一对多.一对一.多对多 ============一对多 一方的设置 @One ...
- Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作
Python进阶----表与表之间的关系(一对一,一对多,多对多),增删改查操作,单表查询,多表查询 一丶表与表之间的关系 背景: 由于如果只使用一张表存储所有的数据,就会操作数 ...
- 使用NHibernate(7)-- 一对一 && 一对多 && 多对多
1, 一对一. 对于数据量比较大的时候,考虑查询的性能,肯能会把一个对象的属性分到两个表中存放:比如用户和用户资料,经常使用的一般是Id和用户名,用户资料(学校,籍贯等)是不经常被查询的,所以就会分成 ...
- day 69-70 一对一 一对多 多对一联表查询
day 69 orm操作之表关系,多对多,多对一 多对一/一对多, 多对多{类中的定义方法} day69 1. 昨日内容回顾 1. 单表增删改查 2. 单表查询API 返回QuerySet对象的: 1 ...
- JPA 一对一 一对多 多对一 多对多配置
1 JPA概述 1.1 JPA是什么 JPA (Java Persistence API) Java持久化API.是一套Sun公司 Java官方制定的ORM 方案,是规范,是标准 ,sun公司自己并没 ...
- MyBatis的关联关系 一对一 一对多 多对多
一对一示例 一个妻子对应一个丈夫 数据库表设计时 在妻子表中添加一个丈夫主键的作为外键 1 对应的JavaBean代码虽然在数据库里只有一方配置的外键,但是这个一对一是双向的关系. Husband实体 ...
- 初学者易上手的SSH-hibernate04 一对一 一对多 多对多
这章我们就来学习下hibernate的关系关联,即一对一(one-to-one),一对多(one-to-many),多对多(many-to-many).这章也将是hibernate的最后一章了,用于初 ...
- EntityFrameworkCore 一对一 && 一对多 && 多对多配置
基本数据结构 表设计如下: 入学记录 public class AdmissionRecord { [Key] public long Id { get; set; } public DateTime ...
- JAVA日记之mybatis-3一对一,一对多,多对多xml与注解配置
1.Mybatis多表查询1.1 一对一查询1.1.1 一对一查询的模型用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户 一对一查询的需求:查询一个订单,与此同时查询出该订单所属的 ...
随机推荐
- LoadRunner性能测试工具下载
LoadRunner性能测试工具 LoadRunner是前美科利(Mercury Interactive)公司著名的性能测试产品.Mercury公司曾经是全球业务优化科技领域的领导者.2006年由惠普 ...
- 每次都能让人头大的 Shader -- 从一次简单的功能说起
最近有个功能, 要渲染从主相机视角看到的另一个相机的可视范围和不可见范围, 大概如下图 : 简单来说就是主相机视野和观察者相机视野重合的地方, 能标记出观察者相机的可见和不可见, 实现原理就跟 Sha ...
- <人人都懂设计模式>-单例模式
这个模式,我还是了解的. 书上用了三种不同的方法. class Singleton1: # 单例实现方式1 __instance = None __is_first_init = False def ...
- 9-剑指offer: 二进制中1的个数
题目描述 输入一个整数,输出该数二进制表示中1的个数.其中负数用补码表示. 代码 class Solution { public: int NumberOf1(int n) { if(n==0) re ...
- Linux安装docker(ubuntu16.04和centos7.4)
ubuntu16.04版本 1.安装依赖 sudo apt-get install apt-transport-https ca-certificates software-properties-co ...
- 本地jar包在maven工程中pom引用
背景 在使用Maven的过程中,经常碰到有些jar包在中央仓库没有的情况.如果公司有私服,那么就把jar包安装到私服上.如果没有私服,那就把jar包安装到本地Maven仓库.下面是如何把jar包导 ...
- 生产者和消费者模型producer and consumer(单线程下实现高并发)
#1.生产者和消费者模型producer and consumer modelimport timedef producer(): ret = [] for i in range(2): time.s ...
- MyBatis Plus 将查询结果封装到指定实体
MyBatis Plus 将查询结果封装到指定实体 思路 自定义方法,使用Wrapper,自定义映射结果集 Mapper接口 package com.mozq.boot.mpsand01.dao; i ...
- Eclipse分享和检出(六)
一.创建资源库位置 1. 切换到透视图 SVN 资源库:Window--Show View--Other,搜索“SVN资源库” 2. 创建资源库位置,右键--新建--资源库位置 3. 输入可用的版 ...
- python中多线程相关
基础知识 进程:进程就是一个程序在一个数据集上的一次动态执行过程 数据集:程序执行过程中需要的资源 进程控制块:完成状态保存的单元 线程:线程是寄托在进程之上,为了提高系统的并发性 线程是进程的实体 ...