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 ...
随机推荐
- ES访问遇到sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
cmd命令cd到jre/bin目录下 输入命令keytool -import -alias 别名 -keystore cacerts -file C://certs//elasticsearch// ...
- Part 11 to 20 Basic in C# continue
Part 11-12 switch statement in C# switch statement break statement if break statement is used insi ...
- Python基础(列表生成式)
import os; list1 = list(range(1,11)) list2 = [x*x for x in list1 if x % 2 == 0]#列表生成式时,把要生成的元素x * x放 ...
- Dapr初体验之服务调用
初次理解服务调用 在微服务中,有一个难点就是:如果你想使用各个服务组件,你就得知道不同服务的地址和端口,也就是服务发现. 在传统应用我们是怎么做的?就是在web项目里配置上api地址,如下: 在一个w ...
- Java设计模式之(八)——适配器模式
1.什么是适配器模式? Convert the interface of a class into another interface clients expect.Adapter lets clas ...
- 01 eclipse搭建maven的web工程(3.1)
eclipse搭建maven的web工程(3.1) 一.下载并在eclipse安装JDK环境[查看] 二.下载并在eclipse安装maven环境[查看] 三.新建maven-webapp工程: 1. ...
- 联盛德 HLK-W806 (六): I2C驱动SSD1306 128x64 OLED液晶屏
目录 联盛德 HLK-W806 (一): Ubuntu20.04下的开发环境配置, 编译和烧录说明 联盛德 HLK-W806 (二): Win10下的开发环境配置, 编译和烧录说明 联盛德 HLK-W ...
- 学习java 7.27
学习内容: 创建树 Swing 使用JTree对象来代表一棵树,JTree树中结点可以使用TreePath来标识,该对象封装了当前结点及其所有的父结点. 当一个结点具有子结点时,该结点有两种状态: 展 ...
- A Child's History of England.31
The English in general were on King Henry's side, though many of the Normans were on Robert's. But t ...
- 基于MQTT协议实现远程控制的"智能"车
智能,但不完全智能 虽然我不觉得这玩意儿有啥智能的,但都这么叫就跟着叫喽. 时隔好几天才写的 其实在写这篇博文的时候我已经在做升级了,并且已经到了中后期阶段了. 主要是业余时间做着玩,看时间了. 规格 ...