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 ...
随机推荐
- C#简单配置类及数据绑定
目录 简介 配置基类 派生配置类 数据绑定 Winform中的数据绑定 WPF下的数据绑定 附件 简介 本文实现一个简单的配置类,原理比较简单,适用于一些小型项目.主要实现以下功能: 保存配置到jso ...
- PHP 数组函数分类整理
1.处理数组键名相关的函数: array_change_key_case - 返回字符串键名全为小写或大写的数组. array_key_exists - 检查给定的键名或索引是否存在于数组中 arra ...
- 关于JAVA中顺序IO的基本操作
关于JAVA中顺序IO的基本操作 写在前面 最近研究一下JAVA中的顺序IO,在网络上找了一会儿,发现少有详细的介绍,顾此在此处说说顺序IO,才学疏浅,如有不对,望赐教. 什么是顺序IO 事实上JAV ...
- Python系列教程-详细版 | 图文+代码,快速搞定Python编程(附全套速查表)
作者:韩信子@ShowMeAI 教程地址:http://showmeai.tech/article-detail/python-tutorial 声明:版权所有,转载请联系平台与作者并注明出处 引言 ...
- Python实战:截图识别文字,过万使用量版本!(附源码!!)
前人栽树后人乘凉,以不造轮子为由 使用百度的图片识字功能,实现了一个上万次使用量的脚本. 系统:win10 Python版本:python3.8.6 pycharm版本:pycharm 2021.1. ...
- WebGoat8.2.2-A8不安全的反序列化
1.概念 使用反序列化在各编程语言中略有不同,如Java.PHP.Python.Ruby.C/C++,但在关键概念上是一样的 序列化:将(内存中的)对象转化成数据格式,以便存储或传输 ...
- Redis键空间通知(keyspace notification),事件订阅
Redis键空间通知(keyspace notification),事件订阅 应用场景:有效期优惠券.24小时内支付.下单有效事件等等. 功能概览 键空间通知使得客户端可以通过订阅频道或模式, ...
- 毕业设计之dns搭建:
[apps@dns_sever ~]$ sudo yum install -y bind [apps@dns_sever ~]$ sudo vim /etc/named.conf // // name ...
- fatal error: runtime: out of memory
[root@VM_0_10_centos frp_0.27.0_linux_amd64]# top top - 21:09:19 up 2 days, 4 min, 2 users, load ave ...
- python—模拟生成双色球号和大乐透号
下边这个脚本,比较适合初级学习基本python语法用.但是,不精炼建议可参考https://www.cnblogs.com/Formulate0303/p/14031748.html的写法. 大乐透玩 ...