mybatis联合查询
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联合查询的更多相关文章
- MyBatis联合查询和使用association 进行分步式查询
查询Emp的同时,查出emp对应的部门Department 方法1:联合查询,使用级联属性封装结果集 <!-- 联合查询,使用级联属性封装结果集 type:要自定义规则的javaBean类型 i ...
- Mybatis 联合查询XML与注解对比
由于是练习,故只做了感兴趣的一部分测试. 测试类容XML配置转注解方式 实体类为了测试请忽略设计是否合理… User.java @Alias("User")public class ...
- Mybatis联合查询(一)
Mybatis的简单联合查询操作: 实体类: Employee: package com.test.mybatis; public class Employee { private Integer i ...
- MyBatis联合查询association使用
1.需求 两张表 channels(频道表) member(会员表) 频道表里面有会员id,查询频道列表的时候需要关联查询出会员的名称,头像等信息 . 2.channels.xml定义,配置主要在这 ...
- Mybatis联合查询记录,左连接参数操作
公司业务需求要做个列表的排序 而实际排序的字段不再本库中,需要跨库去拿到字段,因为是微服务体系架构,不可能Left join跨库的表,所以决定调用一次跨服务的API拿到排序相关的对象,里面包含需要排序 ...
- MyBatis 多表联合查询及优化 以及自定义返回结果集
下面就来说一下 mybatis 是通过什么来实现多表联合查询的.首先看一下表关系,如图: 这 里,我已经搭好了开发的环境,用到的是 SpringMVC + Spring + MyBatis,当然,为了 ...
- MyBatis之三:多表联合查询
在这篇文章里面主要讲解如何在mybatis里面使用一对一.一对多.多表联合查询(类似视图)操作的例子. 注:阅读本文前请先大概看一下之前两篇文章. 一.表结构 班级表class,学生表student, ...
- Mybatis.net与MVC入门配置及联合查询动态SQL拼接和简单事务
第一次学习Mybatis.net,在博客园也找到好多资料,但是在配置成功之后也遇到了一些问题,尤其是在动态SQl拼接时候,这里把遇到的问题还有自己写的一个Demo贴出来,希望能帮到新手,有不适合的地方 ...
- MyBatis 多表联合查询,字段重复的解决方法
MyBatis 多表联合查询,两张表中字段重复时,在配置文件中,sql语句联合查询时使用字段别名,resultMap中对应的column属性使用相应的别名: <resultMap type=&q ...
随机推荐
- maven添加代理,默认的.m2路径
Maven设置http代理 编辑~/.m2/settings.xml文件,添加如下配置 找到 <proxies> 节点.去掉相应的注释,设置代理信息如下: 1 <proxy> ...
- Spark整合Hive
spark-sql 写代码方式 1.idea里面将代码编写好打包上传到集群中运行,上线使用 spark-submit提交 2.spark shell (repl) 里面使用sqlContext 测试使 ...
- 7.深入TiDB:range 范围计算优化
本文基于 TiDB release-5.1进行分析,需要用到 Go 1.16以后的版本 我的博客地址:https://www.luozhiyun.com/archives/605 这篇文章首先会回顾一 ...
- etcd install & configuration
目录 概述 先决条件 相关术语 ETCD 部署 源码安装 其他方式 安装报错 配置文件详解 etcdctl 使用 日志独立 概述 etcd 是兼具一致性和高可用性的键值数据库,为云原生架构中重要的基础 ...
- java-TCP协议发送和接收数据
TCP协议接收数据的步骤: A:创建接收数据的Socket对象 创建对象的时候要指定端口 B:监听客户端连接 等待客户端连接 C:获取Socket对象的输入流(字节流) D:读数据,并显示在控制台 E ...
- OAuth 2.1 的进化之路
背景 2010年, OAuth 授权规范 1.0 (rfc 5849) 版本发布, 2年后, 更简单易用的 OAuth 2.0 规范发布(rfc 6749), 这也是大家最熟悉并且在互联网上使用最广泛 ...
- 【Tool】IntelliJ 搭建Node.js环境
IntelliJ IDEA 开发 Node.js 2019-07-29 14:12:34 by冲冲 1. 配置插件 在IDEA的 file -> setting -> Plugins, ...
- Python list的深拷贝和浅拷贝
深拷贝和浅拷贝 列表存储数据,列表拷贝就是数据备份 浅拷贝 优点:占用内存较少 缺点:修改深层数据,会影响原数据 深拷贝 优点:修改数据,互不影响 缺点:占用内存较大 ""&quo ...
- Parallel NetCDF 简介
Parallel NetCDF API 所有C接口前加ncmpi前缀,Fortran接口前加nfmpi前缀 函数返回整数 NetCDF 状态变量 1. Variable and Parameter T ...
- 硬盘SSD、HDD和SSHD都是什么意思?哪种类型硬盘最好?
硬盘分类:(1)HHD 机械硬盘(Mechanical hard disk)(2)SSD 固态硬盘(solid state drive/disk)(3)SSHD 混合硬盘,说白了就是HDD+SSD=S ...