Mybatis-07-多对一和一对多处理
多对一处理
如,
- 多个学生,对应一个老师
- 多个学生关联一个老师(多对一)
- 一个老师有很多学生(一对多)
SQL:
create table `teacher`(
`id` int(10) not null ,
`name` varchar(30) default null,
primary key(`id`)
)engine=innodb default charset =utf8;
insert into teacher(`id`,`name`)
values(1,"秦老师");
create table `student` (
`id` int(10) not null ,
`name` varchar(30) default null,
`tid` int(10) default null ,
primary key (`id`),
key `fktid` (`tid`),
constraint `fktid` foreign key (`tid`) references `teacher` (`id`)
)engine=innodb default charset =utf8;
insert into student(id,name,tid) values(1,'小明',1);
insert into student(id,name,tid) values(2,'小红',1);
insert into student(id,name,tid) values(3,'小张',1);
insert into student(id,name,tid) values(4,'小李',1);
insert into student(id,name,tid) values(5,'小王',1);
测试环境搭建
- 导入lombok
- 新建实体类Teacher,Student
- 建立Mapper接口
- 建立Mapper.xml文件
- 在核心配置文件中绑定注册Mapper接口或文件
- 测试查询是否成功
按照查询嵌套处理
<!--思路:
1.查询所有的学生信息
2.根据tid,寻找对应的老师
-->
<select id="getStudent" resultMap="StudentTeacher">
select * from student
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="id" column="id"/>
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="Teacher">
select * from teacher where id=#{id};
</select>
按照结果嵌套处理
<!--按照结果嵌套处理-->
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid,s.name sname,t.name tname
from student s,teacher t
where s.tid=t.id;
</select>
<resultMap id="StudentTeacher2" type="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
回顾对比Mysql多对一查询方式:
- 子查询
- 联表查询
一对多处理
步骤:
环境搭建
实体类
@Data
public class Student {
private int id;
private String name;
private int tid;
}
@Data
public class Teacher {
private int id;
private String name;
private List<Student> students;
}
- 按照结果嵌套处理
<select id="getTeacher" resultMap="TeacherStudent">
select s.id sid,s.name sname,t.name tname,t.id tid
from student s,teacher t
where s.tid=t.id and t.id=#{tid};
</select>
<resultMap id="TeacherStudent" type="Teacher">
<result property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
- 按照查询嵌套处理
<select id="getTeacher2" resultMap="TeacherStudent2">
select * from mybatis.teacher where id=#{tid}
</select>
<resultMap id="TeacherStudent2" type="Teacher">
<collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
</resultMap>
<select id="getStudentByTeacherId" resultType="Student">
select * from mybatis.student where tid=#{tid}
</select>
小结
- 关联 association多对一
- 集合 collection 一对多
- javaType 和 ofType
- JavaType 用来指定实体类中属性的类型
- ofType 用来指定映射到List中的pojo类型,泛型中的约束类型
注意点:
保证SQL的可读性,尽量通俗易懂
注意属性名和字段名区别
可以使用日志排除错误,推荐Log4j
面试高频
- Mysql引擎
- InnoDB底层原理
- 索引
- 索引优化
Mybatis-07-多对一和一对多处理的更多相关文章
- mybatis学习——多对一和一对多查询
首先先来说明一下数据库,数据库有两张表student表和teacher表: student表如下: teacher表如下: 两张表的关系:多个学生关联一位老师(多对一) *其中tid是外键 需要sql ...
- Mybatis中多对一与一对多
多对一的处理 在pojo中就有 Student private String name; private String id; private Teacher teacher; 比如说多个学生对应着一 ...
- 后端框架的学习----mybatis框架(9、多对一处理和一对多处理)
9.多对一处理和一对多处理 #多对一 <!--按照结果集嵌套查询--> <select id="getAllStudent1" resultMap="S ...
- Hibernate关联映射(单项多对一和一对多、双向一对多)
最近总是接触着新的知识点来扩展自己的知识面:不停的让自己在原地接触天空的感觉真的很美好!!!革命没有成功,程序员的我们怎么能不努力呢...... 一.用员工和部门来剖析关联映射的原理. 1)从这张截图 ...
- 一步步学习NHibernate(5)——多对一,一对多,懒加载(2)
请注明转载地址:http://www.cnblogs.com/arhat 通过上一章的学习,我们建立了Student和Clazz之间的关联属性,并从Student(many)的一方查看了Clazz的信 ...
- Mybatis的多对多映射
一.Mybatis的多对多映射 本例讲述使用mybatis开发过程中常见的多对多映射查询案例.只抽取关键代码和mapper文件中的关键sql和配置,详细的工程搭建和Mybatis详细的流程代码可参见& ...
- mybatis(一、原理,一对多,多对一查询)
MyBatis框架及原理分析 MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架,其主要就完成2件事情: 封装JDBC操作 利用反射打通Java类与SQL语句之间的相互转换 ...
- mybatis多对一与一对多
步骤: 1.创建maven项目 2.编写工具类 3.编写实体类 4.编写mapper接口 5.配置xml 6.测试 多对一:多个学生关联一个老师 工具类: //sqlSessionFactory -- ...
- MyBatis学习05(多对一和一对多)
8.多对一的处理 多对一的理解: 多个学生对应一个老师 如果对于学生这边,就是一个多对一的现象,即从学生这边关联一个老师! 数据库设计 CREATE TABLE `teacher` ( `id` IN ...
- Mybatis 多对一和一对多 学习总结04
1.Mybatis 组件的声明周期 声明周期是组件的重要问题,Mybatis也常用语多线程环境,错误使用会造成多线程并发问题,为正确编写Mybatis应用程序,我们要掌握Mybatis组件的声明周 ...
随机推荐
- 微信小程序实战:app主页面保存page页面实例
先上代码. app.js //app.js App({ onLaunch: function () { // 登录 wx.login({ success: res => { if (this.g ...
- linq介绍及工作中应用两例——左联与内联,linq循环方法
目录 1 linq介绍 1.1 linq产生背景 1.2 linq使用范围 1.3 linq核心程序集 1.4 linq架构图 1.5 linq使用形式对比 1.5.1 linq To Objects ...
- 不是吧,阿sir,2020年程序员要不好过?
自从网传程序员到了35岁之后必须要转行,现在又有人传言:“疫情之下,程序员今年要过苦日子了,降薪裁员是大趋势.” 不是,我就不明白了,你们怎么就看不得程序员好呢?天天巴望着程序员降薪.转行.裁员… ...
- linux日志朔源分析记录
lastlog 记录用户最后一次登录情况 只有root最近登录过 lastlog -u 用户名或者uid uid 直接在passwd文件中的低三位可以看到 lastb 记录用户用户登录失败的用户记录, ...
- django-模板之标签
目录 模板 模版是纯文本文件,可以生成任何基于文本的文件格式,比如HTML,XML,CSV等.Django模版语言致力于在性能和简单性上取得平衡.Django的模版系统并不是简单的将Python嵌入到 ...
- fiddler替换修改后的js文件绕过无限debugger
转自:https://www.jianshu.com/p/38c4afae636c 1.在js文件右击, 然后点击save as ..., 把js文件保存到本地.(网站:https://taodaxi ...
- 两种 HTTP 方法:GET 和 POST
区别 GET POST 可见性 数据在 URL 中对所有人都是可见的. post 方式通过body体进行传输,数据不会显示在 URL 中. 安全性 与 POST 相比,GET 的安全性较差,因为所发送 ...
- Spring+hibernate+JSP实现Piano的数据库操作---3.Piano实体类
package com.entity; import org.springframework.stereotype.Component; import javax.persistence.*; @Co ...
- Ionic 警告框
<html ng-app="mySuperApp"> <head> <meta charset="utf-8"> <m ...
- map,reduce,filter基础实现
#coding=gbk from operator import add # 导入加法 # map 函数名 , 序列对象 print(list(map(str,range(5)))) print(li ...