1.有学生实体

@Component
@Scope("prototype")
public class StudentInfo {
private Integer studentId;//学号
private String studentName;//学生姓名
private String studentAccount;//学生账号
private String studentPwd;//学生密码
private ClassInfo classInfo;//所在班级
private GradeInfo grade;//所在年级
}

班级对象和年级对象是学生实体的属性

2.有数据接口

public StudentInfo getStudentByAccountAndPwd(String studentAccount);

3.数据接口实现

3.1 定义resultmap

为什么要定义resultmap

告诉mybatis将从结果集中取出的数据转换成开发者需要的对象

resultMap="queryStudent"

表示引用定义好的resultmap进行数据库表和返回类型对象的映射

<resultMap type="com.taohan.online.exam.po.StudentInfo" id="queryStudent">
<id column="studentId" property="studentId"/>
<result column="studentName" property="studentName"/>
<result column="studentAccount" property="studentAccount"/>
<result column="studentPwd" property="studentPwd"/>
</resultmap>

id:resultMap的唯一标识符号

type:resultmap实际返回的类型

id:表示主键

column:表示数据库表的列名,property表示数据库列映射到返回类型的属性,这种情况可以解决数据库字段和实体类属性不匹配的问题

因为class和grade对象是学生对象的属性,所以使用resultmap去映射返回类型

<association property="classInfo" javaType="com.taohan.online.exam.po.ClassInfo">
<id column="classId" property="classId"/>
<result column="className" property="className"/>
</association>

column:数据库的列名

property:返回类型Student的属性名classId

javaType:该属性对应的类型名称,本次表示的是ClassInfo类型

<resultMap type="com.taohan.online.exam.po.StudentInfo" id="queryStudent">
<id column="studentId" property="studentId"/>
<result column="studentName" property="studentName"/>
<result column="studentAccount" property="studentAccount"/>
<result column="studentPwd" property="studentPwd"/>
<association property="classInfo" javaType="com.taohan.online.exam.po.ClassInfo">
<id column="classId" property="classId"/>
<result column="className" property="className"/>
</association>
<association property="grade" javaType="com.taohan.online.exam.po.GradeInfo">
<id column="gradeId" property="gradeId"/>
<result column="gradeName" property="gradeName"/>
</association>
</resultMap>

3.2 查询数据

<select id="getStudentByAccountAndPwd" parameterType="java.lang.String" resultMap="queryStudent">
SELECT a.*,b.className,c.gradeId,c.gradeName FROM StudentInfo a
INNER JOIN ClassInfo b ON a.classId=b.classId
INNER JOIN GradeInfo c ON b.gradeId=c.gradeId
WHERE studentAccount=#{studentAccount}
</select>

3.3 多表连接

SELECT a.*,b.className,c.gradeId,c.gradeName FROM StudentInfo a
INNER JOIN ClassInfo b ON a.classId=b.classId
INNER JOIN GradeInfo c ON b.gradeId=c.gradeId

查询学生表的所有信息,b表的班级名,c表的年级号,c表的年级名

内连接INNER JOIN:在表中存在至少一个匹配时,INNER JOIN 关键字返回行。

4.一般的一个学生只对应一个班级,但是一个班级往往对应很多学生,所以有

private List<Student> students;//setter,getter略

所以存在mapper

<resultMap type="com.taohan.online.exam.po.ClassInfo" id="queryStudent">
<id column="classId" property="classId"/>
<result column="className" property="className"/>
<collection
<!-- 返回的属性名students-->
property="students"
<!--返回的实例一个集合-->
javaType="ArrayList"
<!--表示使用参数id作为参数进行之后的select语句查询-->
column="id"
<!-- 表示集合中的类型-->
ofType="org.taohan.online.exam.po.StudentInfo>
<!--表示执行一条select语句-->
<!--select="selectStudentWithId"/>
</resultMap>
<select id="selectStudentWithId" resultType="org.taohan.online.exam.po.StudentInfo">
select * from tb_student where classId=#{id}
</select>
<select id=selectClazz" resultMap="clazzReultMap">
select * from tb_clazz
<select>

mybatis联合查询的更多相关文章

  1. MyBatis联合查询和使用association 进行分步式查询

    查询Emp的同时,查出emp对应的部门Department 方法1:联合查询,使用级联属性封装结果集 <!-- 联合查询,使用级联属性封装结果集 type:要自定义规则的javaBean类型 i ...

  2. Mybatis 联合查询XML与注解对比

    由于是练习,故只做了感兴趣的一部分测试. 测试类容XML配置转注解方式 实体类为了测试请忽略设计是否合理… User.java @Alias("User")public class ...

  3. Mybatis联合查询(一)

    Mybatis的简单联合查询操作: 实体类: Employee: package com.test.mybatis; public class Employee { private Integer i ...

  4. MyBatis联合查询association使用

    1.需求 两张表 channels(频道表)  member(会员表) 频道表里面有会员id,查询频道列表的时候需要关联查询出会员的名称,头像等信息 . 2.channels.xml定义,配置主要在这 ...

  5. Mybatis联合查询记录,左连接参数操作

    公司业务需求要做个列表的排序 而实际排序的字段不再本库中,需要跨库去拿到字段,因为是微服务体系架构,不可能Left join跨库的表,所以决定调用一次跨服务的API拿到排序相关的对象,里面包含需要排序 ...

  6. MyBatis 多表联合查询及优化 以及自定义返回结果集

    下面就来说一下 mybatis 是通过什么来实现多表联合查询的.首先看一下表关系,如图: 这 里,我已经搭好了开发的环境,用到的是 SpringMVC + Spring + MyBatis,当然,为了 ...

  7. MyBatis之三:多表联合查询

    在这篇文章里面主要讲解如何在mybatis里面使用一对一.一对多.多表联合查询(类似视图)操作的例子. 注:阅读本文前请先大概看一下之前两篇文章. 一.表结构 班级表class,学生表student, ...

  8. Mybatis.net与MVC入门配置及联合查询动态SQL拼接和简单事务

    第一次学习Mybatis.net,在博客园也找到好多资料,但是在配置成功之后也遇到了一些问题,尤其是在动态SQl拼接时候,这里把遇到的问题还有自己写的一个Demo贴出来,希望能帮到新手,有不适合的地方 ...

  9. MyBatis 多表联合查询,字段重复的解决方法

    MyBatis 多表联合查询,两张表中字段重复时,在配置文件中,sql语句联合查询时使用字段别名,resultMap中对应的column属性使用相应的别名: <resultMap type=&q ...

随机推荐

  1. Are we ready for learned cardinality estimation?

    Are we ready for learned Cardinality Estimation 摘要 文章包括三大部分: 对于一个静态的数据库,本文将五种基于学习的基数估计方法与九中传统的基数估计方法 ...

  2. 旧电脑做服务器--第一篇 sql server 服务器搭建

    背景:旧电脑为2015年的老电脑,联系G50系列,目前键盘鼠标操作都有问题,键盘按键和鼠标左键莫名奇妙变成右击,屏幕显示也是大颗粒.但是配置还可以,16GB内存+256GB三星固态硬盘.所以想搭建作为 ...

  3. 让textarea根据文本的长度自动调整它的高度

    ... <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR ...

  4. 基于linux与线程池实现文件管理

    项目要求 1.基本 用线程池实现一个大文件夹的拷贝,大文件夹嵌套很多小文件:实现复制到指定文件夹的全部文件夹. 2.扩充功能 显示进度条:拷贝耗时统计:类似linux的tree,不能直接用system ...

  5. [hdu6145]Arithmetic of Bomb II

    对于题中的"normal expression"(仅含加减乘和无前导0的非负整数,无括号)的计算,实际上并不需要通常的表达式求值,而可以用下述方式计算-- 维护三元组$(a,b,c ...

  6. 提升AI智能化水平,打造智慧新体验

    内容来源:华为开发者大会2021 HMS Core 6 AI技术论坛,主题演讲<提升AI智能化水平,打造智慧新体验>. 演讲嘉宾:沈波,华为消费者AI与智慧全场景ML Kit产品总监 今天 ...

  7. 监听器watch

     <label > 姓名: <input type="text" placeholder="请输入姓名" v-model="firt ...

  8. 论文翻译:2021_Decoupling magnitude and phase optimization with a two-stage deep network

    论文地址:两阶段深度网络的解耦幅度和相位优化 论文代码: 引用格式:Li A, Liu W, Luo X, et al. ICASSP 2021 deep noise suppression chal ...

  9. Redis总结笔记

    Redis总结笔记 应用场景 缓存--热数据 计算器 队列 位操作 分布式锁与单线程机制 最新列表 排行榜   Maxmemory-policy算法 volatile-lru:使用LRU算法移除key ...

  10. shell命令行——快捷键

    生活在 Bash shell 中,熟记以下快捷键,将极大的提高你的命令行操作效率. 编辑命令 Ctrl + a :移到命令行首 Ctrl + e :移到命令行尾 Ctrl + f :按字符前移(右向) ...