MyBatis—resultMap 的关联方式实现多表查询(多 对一)
mapper 层
a)在 StudentMapper.xml 中定义多表连接查询 SQL 语句, 一次性查到需要的所有数据, 包括对应班级的信息.
b)通过<resultMap>定义映射关系, 并通过<association>指定对象属性的映射关系. 可以把<association>看成一个<resultMap>使用. javaType 属性表示当前对象, 可以写全限定路径或别名.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bjsxt.mapper.StudentMapper">
<resultMap type="Student" id="smap">
<id property="stid" column="sid"/>
<result property="stname" column="name"/>
<result property="stage" column="age"/>
<result property="stsex" column="sex"/>
<result property="stcid" column="c_id"/>
<!-- 用于关联一个对象 -->
<association property="clazz" javaType="clazz">
<id property="id" column="cid"/>
<result property="name" column="cname"/>
<result property="room" column="croom"/>
</association>
</resultMap>
<select id="selAll" resultMap="smap">
select s.sid,s.name,s.age,s.sex,s.c_id,c.cid,c.cname,c.croom
from t_stu s
left join t_class c
on s.c_id=c.cid
</select>
</mapper>
Test测试类:
package com.bjsxt.test;
import java.util.List;
import com.bjsxt.pojo.Student;
import com.bjsxt.service.StudentService;
import com.bjsxt.service.Impl.StudentServiceImpl;
public class TestSel {
public static void main(String[] args) {
StudentService ss=new StudentServiceImpl();
List<Student> list = ss.selAll();
for (Student student : list) {
System.out.println(student);
}
}
}
其余的配置文件参考:Mybatis_多表关联查询_resultMap_集合对象_N+1方式实现(不需要ClazzMapper)
运行截图:

MyBatis—resultMap 的关联方式实现多表查询(多 对一)的更多相关文章
- Mybatis_resultMap的关联方式实现多表查询(一对多)
a)在 ClazzMapper.xml 中定义多表连接查询 SQL 语句, 一次性查到需要的所有数据, 包括对应学生的信息. b)通过<resultMap>定义映射关系, 并通过<c ...
- 在mybatis框架中,延迟加载与连表查询的差异
1.引子 mybatis的延迟加载,主要应用于一个实体类中有复杂数据类型的属性,包括一对一和一对多的关系(在xml中用collection.association标签标识).这个种属性往往还对应着另一 ...
- mybatis总结(三)之多表查询
上一节,已经把实体类和配置文件都写过了,这节课直接添加几个方法吧 在DeptMapper.xml文件中添加 <!-- 多表查询(1对多) ,通过部门编号,查询出部门所在的员工姓名,部门名,部门编 ...
- Django框架基础知识08-表关联对象及多表查询
1.自定义主键字段的创建 AutoFiled(pirmary_key=True) # 一般不会自定义,int类型,自增长 一般不自定义主键. 2.order_by asc desc from djan ...
- MyBatis学习存档(5)——联表查询
之前的数据库操作都是基于一张表进行操作的,若一次查询涉及到多张表,那该如何进行操作呢? 首先明确联表查询的几个关系,大体可以分为一对一和一对多这两种情况,接下来对这两种情况进行分析: 一.建立表.添加 ...
- myBatis框架_关于怎么获得多表查询的总记录数
<!-- 查找总记录数 --> <select id="billCount" resultType="int"> select coun ...
- mysql数据库的十种查询方式及多表查询
--mysql数据库的十种查询方式 -- (1)查询时起别名 SELECT id AS '编号',NAME AS '姓名',age AS '年龄' FROM student; -- (2)查询时添加常 ...
- mybatis学习 十六 auto_mapping实现连表查询
只能使用多表联合查询方式. 要求:查询出的列别和属性名相同. 点字符 "." 在 SQL 是关键字符,两侧添加反单引号(Tab键上的一个字符) <select id=&q ...
- mysql一张表多个字段关联另一张表查询
如下:一张订单表多个字段关联用户表: 1.链表查询 SELECT cu.id AS 'id',cu.version AS 'version',cu.cid AS 'cid',cu.uid AS 'ui ...
随机推荐
- tcpdump抓包工具
tcpdump抓包工具 一:TCPDump介绍 TcpDump可以将网络中传送的数据包的"头"完全截获下来提供分析.它支持针对网络层.协议.主机.网络或端口的过滤,并提供and ...
- IDEA升级,提示"Connection Error Failed to prepare an update"
问题来源: 之前修改了IDEA的默认配置文件路径,然后升级新版本时就无法升级,提示"Failed to prepare an update Temp directory inside ins ...
- 防火墙firewalld的基础操作
防火墙Firewalld.iptables 1.systemctl模式 systemctl status firewalld #查看状态 2 systemctl start firewalld #启动 ...
- Dart编程语言从基础到进阶1
Dart编程语言从基础到进阶Dart的语言的发展史以及Dart能做什么未来发展怎么样等等问题我们在这里是不讨论的.我相信既然选择了来学习它,那你内心基本已经认可了它,所以我们废话不多说直接进入主题. ...
- [LC]747题 Largest Number At Least Twice of Others (至少是其他数字两倍的最大数)
①中文题目 在一个给定的数组nums中,总是存在一个最大元素 . 查找数组中的最大元素是否至少是数组中每个其他数字的两倍. 如果是,则返回最大元素的索引,否则返回-1. 示例 1: 输入: nums ...
- pat 1065 A+B and C (64bit)(20 分)(大数, Java)
1065 A+B and C (64bit)(20 分) Given three integers A, B and C in [−263,263], you are supposed t ...
- pat 1124 Raffle for Weibo Followers(20 分)
1124 Raffle for Weibo Followers(20 分) John got a full mark on PAT. He was so happy that he decided t ...
- selenium介绍和环境搭建
一.简单介绍 1.selenium:Selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Fire ...
- spring奇怪异常记录(会逐渐记录)
1 严重: Context initialization failedorg.springframework.beans.factory.BeanCreationException: Error cr ...
- java path
static{ String path = new Object(){ public String getPath() { return this.getClass().getResource(&qu ...