在关系型数据库中,多表之间存在着三种关联关系,分别为一对一、一对多和多对多,如下图所示:
1.1.3 定义Dao层接口 public interface ICountryDao { Country selectCountryById(int cid); } 1.1.4 定义测试类 public class Mytest { private SqlSession session; private ICountryDao dao; @Before public void setUp() { session = MyBatisUtils.getSqlSession(); dao = session.getMapper(ICountryDao.class); } @After public void tearDown() { if(session != null) { session.close(); } } @Test public void test01() { Country country = dao.selectCountryById(1); System.out.println(country); } } 1.1.5 定义映射文件 1、多表连接查询方式 <mapper namespace="com.eason.mybatis.dao.ICountryDao"> <resultMap type="Country" id="countryMapper"> <id column="cid" property="cid"/> <result column="cname" property="cname"/> <collection property="ministers" ofType="Minister"> <id column="mid" property="mid"/> <result column="mname" property="mname"/> </collection> </resultMap> <select id="selectCountryById" resultMap="countryMapper"> select cid, cname, mid, mname from t_country, t_minister where cid=#{xxx} and cid=countryId </select> </mapper>
resultMap标签为处理关联关系的父标签,后续关联关系定义都是在该标签下进行的
1、为什么说 Mybatis 是半自动 ORM 映射工具?它与全自动的区别在哪里?
---- 一、Mybatis一对一关联查询 查询学生时,将关联的一个班级对象查询出来,就是一对一关联查询。这里还新增了几个实体类 新增Classes实体类 package com.example.pojo; import java.util.List; public class Classes { private int cid; private String className; private List<Student> studentList;
可以通过关联查询实现。关联查询是几个表联合查询,只查询一次,通过在resultMap里面的association,collection节点配置一对一,一对多的类就可以完成
又要提到前面那个说了好几遍的起别名问题了,使用resultMap标签也能解决这个问题。
接《MyBatis+Spring MVC开发指南(一)》,本篇博客将涵盖MyBatis高级映射(一对一,一对多、多对多)、延迟加载、缓存原理分析(一级缓存、二级缓存)、MyBatis和Spring的整合、逆向工程等主题。
MySQL一对多的数据分页是非常常见的需求,比如我们要查询商品和商品的图片信息。但是很多人会在这里遇到分页的误区,得到不正确的结果。今天就来分析并解决这个问题。
2.在MyBatis中针对不同的数据库软件,<insert>元素如何将主键回填?
(3)用户实体对象,com.xuliugen.mybatis.demo.bean.User
不同的Xml映射文件,如果配置了namespace,那么id可以重复;如果没有配置namespace,那么id不能重复;毕竟namespace不是必须的,只是最佳实践而已。
通过前面的学习,我们使用 Mybatis 实现一对多关系的维护。多对多关系其实我们看成是双向的一对多关系。
resultMap 可以将查询到的复杂数据,比如多张表的数据、一对一映射、一对多映射等复杂关系聚合到一个结果集当中。日常的业务开发通常都会和它打交道,今天就对 resultMap 进行一个详细讲解。文末有 DEMO
通过前面的学习,我们已经掌握了 Mybatis 中一对一,一对多,多对多关系的配置及实现,可以实现对象的关联查询。实际开发过程中很多时候我们并不需要总是在加载用户信息时就一定要加载他的账户信息。此时就是我们所说的延迟加载。
注:代码已托管在GitHub上,地址是:https://github.com/Damaer/Mybatis-Learning ,项目是mybatis-10-one2many,需要自取,需要配置maven环境以及mysql环境(sql语句在resource下的test.sql中),觉得有用可以点个小星。
最近有一个小项目需求,需要用spring mvc + mybatis实现一个复杂的配置系统。其中遇到了很多不太常见的问题,在这里特意记录下: 主要涉及的内容有 事务 多表删除 插入并返回主键 1 spring mvc + mybatis的事务 背景 大概就是有ABC三张表,A表跟B表是一对多关系,B表跟C表是一对多关系。在创建的时候提交了一个大的json,需要先暴力删除A中某行关联的所有B和所有C,然后分别创建B,再创建C。这些操作要在一个事务中进行,不能删完,插入失败。 结构长得如下: { "i
以学生成绩表和课程表的关系为例,在学生成绩表中,一个学生可以有多条成绩记录,每一条成绩记录都与某一门课程相关联。而在课程表中,每一门课程也会有多条成绩记录,因此它们之间就是一个典型的一对多关系。
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://louluan.blog.csdn.net/article/details/26699735
实际的开发中,对数据库的操作常常会涉及到多张表,这在面向对象中就涉及到了对象与对象之间的关联关系。
二、Hibernate映射(由于之前只看过MyBatis没看过Hibernate,所以写的比较啰嗦)
Mybatis使用ognl表达式解析对象字段的值,#{}或者${}括号中的值为pojo属性名称。
注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询。如果从用户信息出发查询用户下的订单信息则为一对多查询,因为一个用户可以下多个订单。
欢迎访问我的GitHub 这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos 本篇概览 本文是《MyBatis初级实战》系列的第六篇,继续实践从多表获取数据; 回顾上一篇,咱们实战了多表关联的一对一关系,如下图所示,查找日志记录时,把对应的用户信息查出: 📷 本篇要实践的是一对多关系:查询用户记录时,把该用户的所有日志记录都查出来,逻辑关系如下图: 📷 在具体编码实现一对多查询时,分别使用联表和嵌套两种方式实现,每
2.2一对多:一本书对应多种书本类型,例如:西游记 -> 神话、古典、名著
#{}和${}的区别 #{}是预编译处理,${}是字符串替换。 Mybatis在处理#{}时,会将sql中的#{}替换为?号,调用PreparedStatement的set方法来赋值; Mybatis
Mybatis测试 1, Mybatis的核心是( sqlsessionfactory ) 2, 使用Mybatis持久化框架进行数据查询需要返回的一个实体类的集合, 在<select
如果您不想自己搭建kubernetes环境,推荐使用腾讯云容器服务TKE:无需自建,即可在腾讯云上使用稳定, 安全,高效,灵活扩展的 Kubernetes 容器平台;
Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心。
mybatis-config.xml <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-
(1)创建扩展PO类 一般User.java类要和数据表表字段一致,最好不要在这里面添加其他字段,今天学习mybatis的逆向工程时,会根据表结构,生成po类,如果在po类中扩展字段,此时会被覆盖掉。 所以针对要扩展的po类,我们需要创建一个扩展类,来继承它。
上次我们讲到了MyBatis的一对一关系的表示,简单回顾一下一对一关系就是一个学生只有一个学生证。那么什么是一对多关系呢?一个学生有多个课程这就是一对多的关系。我们结合上一章中的学生和学生证,在此基础
在开发过程中单表查询往往不能满足需求分析的很多功能,对于比较复杂业务来说,关联的表有几个,甚至是几十个并且表与表之间的关联相当复杂。为了能够实现复杂的功能业务,就必须进行多表查询,Mybatis作为流行的Java持久化框架,提供了灵活而强大的多表查询映射功能,可以实现表之间的一对一、一对多、多对多关系的映射。
班级信息和他的学生信息为一对多关系,并且在查询班级的信息过程中查询出学生信息。我们想到了左外连接查询比较合适。
注意:因为一个订单信息只会是一个人下的订单,所以从查询订单信息出发关联查询用户信息为一对一查询。
mybatis 高级映射和spring整合之高级映射 ————————————————学习结构———————————————————— 0.0 对订单商品数据模型进行分析 1.0 高级映射 1.1 一对一查询 1.2 一对多查询 1.3 多对多查询 1.4 resultMap总结 1.5 延迟加载 2.0 查询缓存 2.1 一级缓存 2.2 二级缓存(了解mybatis
在 Laravel 中定义模型关联是每个 Laravel 开发者可能已经做过不止一次的事情。但是在试图实现关联时可能会遇到各种问题。因为 Laravel 有各种各样的关联,你应该选择哪一个?当涉及到查询模型时,我们如何充分利用模型关联的功能?
<association>中嵌套一个id为selectAddressByUserId的查询,查询这个用户的地址。
Mybatis框架是一个半自动的ORM持久层框架,也可以在Java中实现类似 insert(User)的操作最终操作数据库,但是需要我们自己写Sql语句。Mybatis是目前比较流行的Dao层框架。
在绝大多数在线应用场景中,数据是存储在关系型数据库中的,当然,有特殊要求的场景中,我们也会将其他持久化存储(如 ElasticSearch、HBase、MongoDB 等)作为辅助存储。但不可否认的是,关系型数据库凭借几十年的发展、生态积累、众多成功的案例,依然是互联网企业的核心存储。
上一章中实现的MyBatis对象映射较为简单,对象中的属性和数据库中的表字段是一一对应的(无论数量和名称都完全一样),如果对象中的属性名和表中的字段名不一致怎么办?又或者Java对象中存在复杂类型属性(即类似Hibernate中多对一、一对多关系对象时)怎么完成数据库表和对象的映射?本章来解决这样的问题。
在关系型数据库中,表与表之间很少是独立与其他表没关系的。所以在实际开发过程中我们会碰到很多复杂的关联关系。在此我们来分析下载mybatis中怎么处理这些关系
注:代码已托管在GitHub上,地址是:https://github.com/Damaer/Mybatis-Learning ,项目是mybatis-12-many2one,需要自取,需要配置maven环境以及mysql环境(sql语句在resource下的test.sql中),觉得有用可以点个小星星。
单标的CRUD操作,主要根据业务如何编写高质量的SQL语句.单标操作的时候如果表中的列和实体的属性不对应的时候,建议使用别名方式进行影射处理.
在上面我们已经准备好了 sql 表结构,在 sql 中具有 一对一、一对多、多对多 三种关系。而如果在 mybatis 的角度来看,却只有 一对一、一对多 两种关系,这是为什么呢?
MyBatis是一个优秀的持久层ORM框架,它对jdbc的操作数据库的过程进行封装,使开发者只需要关注SQL 本身,而不需要花费精力去处理例如注册驱动、创建connection、创建statement、手动设置参数、结果集检索等jdbc繁杂的过程代码。
领取专属 10元无门槛券
手把手带您无忧上云