SpringBoot进行多表查询
实体类:
Emp 类:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {private int id;private String lastname;private String email;private int gender;private int did;private Dept dept;private Date birth = new Date();
}
Dept类:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {private int id;private String dname;
}
Mapper接口
EmpMapper:
//这个注解表示这是一个mybatis的mapper类
@Mapper
@Repository
public interface EmpMapper {void addEmp(Emp emp);void deleteEmp(int id);void updateEmp(Emp emp);Emp queryEmpById(int id);List<Emp> queryEmpList();
}
DeptMapper:
@Mapper
@Repository
public interface DeptMapper {List<Dept> queryDeptList(@Param("cid") int cid);
}
EmpMapper.xml 配置文件
<mapper namespace="com.acoffee.mapper.EmpMapper"><resultMap id="EmpMap" type="Emp"><id column="id" property="id"></id><id column="lastname" property="lastname"></id><id column="email" property="email"></id><id column="gender" property="gender"></id><association property="dept" select="com.acoffee.mapper.DeptMapper.queryDeptList" column="did"></association></resultMap><select id="queryEmpList" resultMap="EmpMap">select * from emp_dept.employees</select></mapper>
DeptMapper.xml配置文件
<mapper namespace="com.acoffee.mapper.DeptMapper"><select id="queryDeptList" resultType="Dept">select * from emp_dept.department where id = #{id};</select></mapper>
前端页面(部分)
<tr th:each="emp:${emps}"><td th:text="${emp.getId()}"></td><td th:text="${emp.getLastname()}"></td><td th:text="${emp.getEmail()}"></td><td th:text="${emp.getGender()==0?'女':'男'}"></td><td th:text="${emp.dept.getDname()}"></td><td th:text="${#dates.format(emp.getBirth(),'yyyy-MM-dd HH:mm:ss')}"></td><td><a class="btn btn-sm btn-primary" th:href="@{/emp/{id}/(id=${emp.getId()})}">编辑</a><a class="btn btn-sm btn-danger" th:href="@{/delemp/{id}/(id=${emp.getId()})}">删除</a></td>
</tr>
查询结果:
上述我们采用的是分步查询。
我们下面使用association嵌套映射
其实这里查询会出现一个奇怪的事情,
因为我们现在两个表中都有id这个字段,所以我们在映射时使用<result property="id" column="id"></result>
去查找部门的id时就发现查出来的是员工的id,就是因为员工的id与部门的id字段名重名了?
EmpMapper:
<mapper namespace="com.acoffee.mapper.EmpMapper"><resultMap id="EmpMap" type="Emp"><id column="id" property="id"></id><id column="lastname" property="lastname"></id><id column="email" property="email"></id><id column="gender" property="gender"></id><association property="dept" javaType="com.acoffee.pojo.Dept"><result property="id" column="id"></result><result property="dname" column="dname"></result></association></resultMap><select id="queryEmpList" resultMap="EmpMap">select emp.*,dept.* from emp_dept.employees emp,emp_dept.department dept where emp.did = dept.id</select>
</mapper>
上述执行结果如下
我们发现是员工id,我们现在将数据库中部门表的id改为pid
修改配置文件以及实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Dept {private int pid;private String dname;
}
<mapper namespace="com.acoffee.mapper.EmpMapper"><resultMap id="EmpMap" type="Emp"><id column="id" property="id"></id><id column="lastname" property="lastname"></id><id column="email" property="email"></id><id column="gender" property="gender"></id><association property="dept" javaType="com.acoffee.pojo.Dept"><result property="pid" column="pid"></result><result property="dname" column="dname"></result></association></resultMap><select id="queryEmpList" resultMap="EmpMap">select emp.*,dept.* from emp_dept.employees emp,emp_dept.department dept where emp.did = dept.pid</select></mapper>
执行结果:
我们就发现查询的就是部门的id了
但是针对上面这个问题我们将部门的id字段名改为did (did在员工表中也存在) 此时我们再去查询又发现还是可以把部门id查询出来
这个问题的原因就是mybatis的字段冲突了,我们写id的时候本来它就会根据前者去查,但是当我们写did的时候,本来查的就是一个东西所以没问题,而我们将字段改成pid就是将字段名区分开了,所以也没问题。
但是我们一般处理这个问题(字段名一样),我们都是在通过sql语句去查询的时候取别名来解决的。
SpringBoot进行多表查询相关推荐
- SpringBoot FK-关联表查询(三)
通过t_student表ID查询与其相关的信息,如某个学生的姓名和其所在班级 点击查看链接,如下 这里我们的班级名称是在t_class表中的,学生姓名是在t_student中的,并不是在同一张表上,这 ...
- SpringBoot FK-关联表查询(二)
上篇中已经介绍了如何创建外键,以下是实现的demo效果 从中我们可以查询出t_student中的学生信息以及其对应班级信息,并且可以根据id进行增删查改. 1.实体类代码 package com.yd ...
- springboot 联表查询
springboot 联表查询 业务分析: 在开发管理系统时,遇到需要将 User 和 对应的 Organization 联表查询起来,找到用户所对应的组织信息 数据库 user表中字段有:user_ ...
- SpringBoot整合Mongodb(三) 使用MongoTemplate进行连表查询
SpringBoot整合Mongodb(三) 使用MongoTemplate进行连表查询 由于在实际开发项目中,突然涉及到了需要Mongodb 进行连表操作的业务,所以我在私下进行了一波学习. 服务器 ...
- SpringBoot+MyBatis多表联合查询
SpringBoot+MyBatis多表联合查询 写在前面 联合查询在实际工作中用的并不多,因为很多表的数据比较大,或者说未来比较大的表,都要谨慎使用联合查询 数据准备 建表语句 create tab ...
- Springboot使用Specification连表查询LEFT
大致目录机构 package com.frank.leftQuery.entity;import lombok.AllArgsConstructor; import lombok.Data; impo ...
- Springboot中使用Mybatis框架对数据库进行联表查询,踩坑填坑
因为mybatis使用的基本是原生sql语句 所以首先从数据库开始说 以mysql数据库为例,对表的连接查询分为四种 内连接,外连接,交叉连接,和联合连接 内连接使用比较运算符根据每个表共有的列的值匹 ...
- 为什么强烈推荐你使用单表查询?(续篇)
在昨天的文章<为什么强烈建议你不要做联表查询?>中,大家提了一些比较有意义的问题,留言回复说不太清楚,今天单开这一篇做一些说明. 首先,在实际开发中,还是应该多使用单表查询,这样无论在性能 ...
- mycat的主从关系 垂直分库 水平分表 以及mycat分片联表查询的配置详解(mysql5.7系列)
主从关系 准备三台不同ip的虚拟机 (第一批)主从关系的配置 主192.168.47.131 配置/etc/my.cnf,在[mysqld]下配置 log-error=/var/log/mysqld. ...
最新文章
- 多目标跟踪 | FairMOT:统一检测、重识别的多目标跟踪框架,全新Baseline
- tf.keras.layers.Flatten() 示例
- 转pdf wps_华为手机自带图片转PDF的功能?今天才知道,当了5年花粉白当了
- 【读书笔记】重要的东西
- python py2exe_转:py2exe 生成 python 可执行文件
- 06MySQL基本函数的使用
- python correlation_相关性系数介绍+python代码实现 correlation analysis
- Maven-Eclipse使用maven创建HelloWorld Java项目
- day_4 Selenium请求库
- 英特尔nuc能代替主机吗_拆了拆了!Intel NUC装机!小机箱退烧器啊!主机显示器合体...
- 51单片机循迹小车c语言程序,51单片机循迹小车源程序
- 我的世界服务器彩虹云,《我的世界》彩虹云跑酷地图存档
- 计算机组成原理实验心得2000字,计算机组成原理实验一:基础汇编语言程序设计实验...
- 论文阅读笔记——野外和非侵入性遗传方法评估棕熊种群规模
- 三维提取等值面的重建方法Marching Cubes
- 【语音识别入门】Python音频处理示例(含完整代码)
- android答辩问题,安卓毕业设计答辩(流程、准备、模板及注意事项)
- 上海车牌拍卖服务器响应时间,上海拍牌技巧:”48秒+700” 最晚出价为55秒
- 常见的一句话muma
- win7桌面上计算机图标名称怎么改,win7电脑桌面图标排列方式如何设置 win7电脑桌面图标排列方法...