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 一对一查询的模型用户表和订单表的关系为,一个用户有多个订单,一个订单只从属于一个用户 一对一查询的需求:查询一个订单,与此同时查询出该订单所属的 ...
随机推荐
- 20187101021-王方《面面相对象程序设计java》第十三周实验总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- 201871010128-杨丽霞《面向对象程序设计(Java)》第十一周学习总结
201871010128-杨丽霞<面向对象程序设计(Java)>第十一周学习总结 项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ ...
- pointnet++之classification/train.py
1.数据集加载 if FLAGS.normal: assert(NUM_POINT<=10000) DATA_PATH = os.path.join(ROOT_DIR, 'data/modeln ...
- 安卓Jsoup爬虫
第一步:导入Jsoup包:把你的jar包放在libs下面之后 接着就会显示在你的这个地方: 重要的还是源码(搞了很久 出错很多 终于成功):我做的是输入要查找的关键字百度百科爬取主要定义,按照标签爬取 ...
- 每天一道Rust-LeetCode(2019-06-06)
每天一道Rust-LeetCode(2019-06-02) Z 字形变换 坚持每天一道题,刷题学习Rust. 原题 题目描述 示例: 输入: 3 输出: 5 解释: 给定 n = 3, 一共有 5 种 ...
- ASP.NET Core 新建项目(Windows)
对于任何语言和框架,都是从 Hello World 开始的,这个非常简单,但却有十分重大的意义,ASP.NET Core 基础教程也会以 Hello World 开始 为什么呢? 因为能够运行 Hel ...
- WC 2008 观光计划(斯坦纳树)
题意 https://www.lydsy.com/JudgeOnline/problem.php?id=2595 思路 是一道比较裸的斯坦纳树呢- 题意等价于选出包含一些点的最小生成树,这就是斯坦纳树 ...
- 慕课网springboot博客系统开发(一)----spring initializr的使用 gradle构建项目
spring initializr工具的地址:https://start.spring.io/:通过它可以很方便的创建springboot项目 这里我们选择使用gradle作为项目的构建工具,此spr ...
- navicat 11.2.7破解
1,软件安装包目录 2,根据电脑系统安装x64或者x86,安装完成之后将PatchNavicat.exe放到navicat的安装目录下 3,右键以管理员身份运行PatchNavicat.exe,或者双 ...
- SVN全局文件过滤规则设置
*/packages */packages/* */.vs/* */.vs */.git/* */.git *.o *.lo *.la *.al .libs *.so *.so.[0-9]* *.a ...