Mybatis关联映射举例详解
作者:·~简单就好 发布时间:2023-08-05 18:18:40
一、关联映射
举例关系说明
数据库创建表,student,teacher
关系说明:
一个老师可以有多个学生
一个学生只有一个老师
一个老师对学生:一对多的关系
一个学生老师:一对一的关系
二、一对一多对一的关系
查询学生信息及其对应的教师信息
学生实体:用对象来存储教师信息,因为一个学生对应一个教师对象
public class Student {
private Integer id;
private String Sname;
private String sex;
private Integer age;
private Integer t_id;
//这个是重点
private Teacher teacher;
}
教师实体:
public class Teacher {
private Integer id;
private String Tname;
}
1.第一种形式-连表查询
数据库查询sql:
SELECT student.id,student.name,teacher.name FROM student LEFT JOIN teacher on student.t_id = teacher.id
mybatis多表联查查询语句:(嵌套其他实体对象)
对于特殊数据:
如果是对象:用association :< association property=“teacher” javaType=“com.qcby.entity.Teacher”>,特殊数据特殊处理
< result property=“id” column=“id”/> :所要查询的字段,property代表java中实体的属性名称,column:表示数据库的字段
<!-- 按照结果嵌套处理-->
<select id="getStudent1" resultMap="StudentTeacher1">
SELECT student.id,student.Sname,teacher.Tname FROM student LEFT JOIN teacher on student.t_id = teacher.id
</select>
<resultMap id="StudentTeacher1" type="com.qcby.entity.Student">
<result property="id" column="id"/>
<result property="Sname" column="Sname"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<result property="t_id" column="t_id"/>
<!-- 复杂的属性我们需要单独去处理 对象:association 集合:collection -->
<!-- property="teacher" student类当中的关联字段 -->
<!-- javaType="com.javen.model.Teacher" 为复杂属性设置类类型-->
<association property="teacher" javaType="com.qcby.entity.Teacher">
<result property="id" column="id"/>
<result property="Tname" column="Tname"/>
</association>
</resultMap>
2.第二种形式-分步查询
数据库查询sql:
SELECT s.id,s.Sname,t.Tname FROM student s,teacher t where s.t_id = t.id
mybatis分布查询查询语句:
<select id = "getStudent" resultMap="StudentTeacher">
select * from student;
</select>
<!--结果映射集-->
<resultMap id="StudentTeacher" type="com.qcby.entity.Student">
<result property="id" column="id"/>
<result property="Sname" column="Sname"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<result property="t_id" column="t_id"/>
<!-- select="getTeacher" :调用下一个查询语句 -->
<!-- column="t_id" 两个表的关联字段-->
<association property="teacher" column="t_id" javaType="com.qcby.entity.Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="com.qcby.entity.Teacher">
select * from teacher where id = #{t_id}; <!-- #{id}; 可以写任何东西,因为会自动匹配 t_id -->
</select>
三、一对多
查询教师对应的学生信息
设立教师实体:用集合来存储对应的学生信息,因为一个教师对应多个学生
public class Teacher {
private Integer id;
private String Tname;
//这个一定要有
private List<Student> students;
}
第一种形式按照结果嵌套处理
mybatis查询语句:
<!--按照结果进行查询-->
<select id="getTeacher" resultMap="TeacherStudent">
SELECT teacher.id,teacher.Tname,student.Sname FROM teacher
LEFT JOIN student on student.t_id = teacher.id
</select>
<resultMap id="TeacherStudent" type="com.qcby.entity.Teacher">
<result property="id" column="id"/>
<result property="Tname" column="Tname"/>
<!-- 复杂的属性我么需要单独去处理 对象:association 集合:collection
在集合中的泛型信息,我们使用ofType获取
-->
<collection property="students" ofType="com.qcby.entity.Student">
<!-- 查询什么写什么 -->
<result property="Sname" column="Sname"/>
</collection>
</resultMap>
第二种形式按照查询嵌套处理
mybatis查询语句: 对于特殊字段集合采用分布查询的方式,特殊字段特殊处理:< collection property=“students” column=“t_id”
ofType=“com.qcby.entity.Student” select=“getStudentByTeacherId” />,getStudentByTeacherId一个新的查询语句
<!--按照查询嵌套处理:分布查询-->
<select id="getTeacher" resultMap="TeacherStudent2">
select * from teacher
</select>
<resultMap id="TeacherStudent2" type="com.qcby.entity.Teacher">
<!--column="t_id" 传值-->
<collection property="students" column="t_id"
ofType="com.qcby.entity.Student" select="getStudentByTeacherId" /> <!--实现分布查询-->
</resultMap>
<select id="getStudentByTeacherId" resultType="com.qcby.entity.Student">
select * from student where id = #{t_id}
</select
来源:https://blog.csdn.net/qq_45830276/article/details/125320784
猜你喜欢
- 首先 下载 jedis.jar包然后再 工程设置里面找到Libraries,点击+。添加下载好的jedis.jar包。点击OK退出即可创建J
- 本文实例为大家分享了使用PageHelper插件实现Service层分页的具体代码,供大家参考,具体内容如下使用场景:平时分页我们可以直接使
- /** * 三角数字: * 比达哥斯拉领导下的古希腊数学家发现了一个有趣的数字序列1, 3, 6, 10, 15, 21,... *
- 我们深知在操作Java流对象后要将流关闭,但往往事情不尽人意,大致有以下几种不能一定将流关闭的写法:1.在try中关流,而没在finally
- 本文实例为大家分享了Android微信摇一摇功能的实现方法,供大家参考,具体内容如下import java.util.ArrayList;
- 众所周知,PDF文档通常是不能编辑和修改的。如果用户需要在PDF文档中签名或者填写其他内容时,就需要PDF文档中有可编辑的域。开发者也经常会
- 本文实例讲述了C#导出数据到Excel文件的方法。分享给大家供大家参考。具体实现方法如下:/// <summary>/// 导出
- Android对sdcard扩展卡文件的操作其实就是普通的文件操作,但是仍然有些地方需要注意。比如:1.加入sdcard操作权限;2.确认s
- 1. 项目背景最近参加了Compose挑战赛的终极挑战,使用Compose完成了一个天气app。之前几轮挑战也都有参与,每次都学到不少新东西
- 本文实例讲述了C#中线程同步对象的方法。分享给大家供大家参考。具体分析如下:在编写多线程程序时无可避免会遇到线程的同步问题。什么是线程的同步
- 这篇文章主要介绍了通过Java实现bash命令过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋
- request对象是什么?Request对象的作用是与客户端交互,收集客户端的Form、Cookies、超链接,或者收集服务器端的环境变量。
- Future和Promise执行回调Netty中的Future, 其实类似于jdk的Future, 用于异步获取执行结果Promise则相当
- 消费逻辑上文 流式图表框架搭建框架搭建好之后着手开发下kafka的核心消费逻辑,流式图表的核心消费逻辑就是实现一个消费链接池维护消
- 视频演示:springboot+vue音乐网站摘要网络技术以及计算机的发展,网友们对网络的要求也日益长高,平常在网上听话用一大堆下载软件下载
- 1.注解方式,yml文件配置上以下就可以直接使用mybatis-plus: mapper-locations: classpath:mapp
- 一、在GitHub上创建一个OAuth二、OAuth的原理Spring官方文档三、OkHttp的使用OkHttp官方网站1.Post代码示例
- 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的。比如说,你的代码少了一个分号,那么运行出来结果是提示是错误
- static修饰符是java里面非常常用的一个东西,用法也非常多。然而,在kotlin里竟然没有这个东西!那该如何替代呢?本文就总结了下ja
- 如果对共享的可变数据的访问不能同步,其后果非常可怕,即使这个变量是原子可读写的。下面考虑一个线程同步方面的问题。对于线程同步,Java类库提