mybatis一对多关联查询两种方式
mybatis一对多关联查询两种方式
- 前提:
- 方式一:
- 方式二:
前提:
现在有两张表,学生表跟教师表,一个教师对应多个学生
教师表:
CREATE TABLE `teacher` (`id` int(11) ,`name` varchar(20) ,`age` int(11) ,`address` varchar(100) ,PRIMARY KEY (`id`)
)学生表:
CREATE TABLE `student` (`id` int(11) ,`ter_id` int(10) ,`name` varchar(255) ,`phone` varchar(255) ,`address` varchar(255) ,`birthDay` date ,PRIMARY KEY (`id`)
)
方式一:
1.直接写多表联合查询sql,mapper文件对应好相应的实体类即可。
需要注意的两个点:
A.用分页插件pageHelper的时候,该种方式会导致查询错乱。
B.如果几个表有字段名相同的情况,字段赋值可能被覆盖。我们可以给字段取别名的方式来解决,如下:
SELECTtea.id,tea.NAME,tea.age,tea.address,stu.id stu_id,stu.ter_id stu_ter_id,stu.NAME stu_name,stu.phone stu_phone,stu.address stu_address,stu.birthday stu_birthday
FROMteacher tea,student stu
WHEREtea.id = stu.ter_id
2.具体实现代码:
实体类Teacher.java 和 Student.java:
package com.shushan.entity;import java.util.List;public class Teacher {private Integer id;private String name; //姓名private Integer age; //年龄private String address; //地址private List<Student> stuList; //学生集合setter方法...getter方法...
}
package com.shushan.entity;import java.util.Date;public class Student {private Integer id;private Integer terId; //教师idprivate String name; //姓名private String phone; //电话private String address; //地址private Date birthDay; //生日setter方法...getter方法...
}
mapper.xml文件
<?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.shushan.dao.UserMultipleDao"><resultMap id="BaseResultMap" type="com.shushan.entity.Teacher"><id column="id" jdbcType="INTEGER" property="id" /><result column="name" jdbcType="VARCHAR" property="name" /><result column="age" jdbcType="INTEGER" property="age" /><result column="address" jdbcType="VARCHAR" property="address"/><!--一对多,方式一--><collection property="stuList" ofType="com.shushan.entity.Student" ><id column="stu_id" jdbcType="INTEGER" property="id" /><result column="stu_ter_id" jdbcType="INTEGER" property="terId" /><result column="stu_name" jdbcType="VARCHAR" property="name" /><result column="stu_phone" jdbcType="VARCHAR" property="phone" /><result column="stu_address" jdbcType="VARCHAR" property="address"/><result column="stu_birthDay" jdbcType="TIMESTAMP" property="birthDay"/></collection></resultMap><select id="getTeacherList" parameterType="com.shushan.entity.Teacher"resultMap="BaseResultMap">select tea.id, tea.name, tea.age, tea.address,stu.id stu_id, stu.ter_id stu_ter_id, stu.name stu_name, stu.phone stu_phone,stu.address stu_address, stu.birthday stu_birthdayfrom teacher tea, student stu where tea.id = stu.ter_id<if test="id != null">and tea.id = #{id}</if></select>
</mapper>
dao层代码:
package com.shushan.dao;import com.shushan.entity.Teacher;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;@Mapper
public interface UserMultipleDao {/*** 一对多查询:方式一* @param teacher* @return*/public List<Teacher> getTeacherList(Teacher teacher);}
方式二:
1.采用子查询的方式。mapper.xml中对应好主表跟次表查询关系。
2.代码实现
实体类Teacher.java 和 Student.java:
详细见方式一
mapper.xml
<?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.shushan.dao.UserMultipleTwoDao"><resultMap id="BaseResultMapTwo" type="com.shushan.entity.Teacher"><id column="id" jdbcType="INTEGER" property="id" /><result column="name" jdbcType="VARCHAR" property="name" /><result column="age" jdbcType="INTEGER" property="age" /><result column="address" jdbcType="VARCHAR" property="address"/><!--一对多,方式一--><collection property="stuList"column="{id=id}"ofType="com.shushan.entity.Teacher"javaType="ArrayList"select="com.shushan.dao.UserMultipleTwoDao.getStudent" /></resultMap><resultMap id="StudentMap" type="com.shushan.entity.Student"><id column="stu_id" jdbcType="INTEGER" property="id" /><result column="ter_id" jdbcType="INTEGER" property="terId" /><result column="name" jdbcType="VARCHAR" property="name" /><result column="phone" jdbcType="VARCHAR" property="phone" /><result column="address" jdbcType="VARCHAR" property="address"/><result column="birthDay" jdbcType="TIMESTAMP" property="birthDay"/></resultMap><!--方式二--><select id="getTeacherTwoList" parameterType="com.shushan.entity.Teacher"resultMap="BaseResultMapTwo">select id, name, age, address from teacher where 1=1<if test="id != null">and id = #{id}</if></select><select id="getStudent" resultMap="StudentMap" parameterType="java.util.Map" >select id, ter_id, name, phone, address, birthdayfrom studentwhere 1=1and ter_id = #{id}</select>
</mapper>
dao层代码:
package com.shushan.dao;import com.shushan.entity.Teacher;
import org.apache.ibatis.annotations.Mapper;import java.util.List;@Mapper
public interface UserMultipleTwoDao {/*** 一对多查询:方式二* @param teacher* @return*/public List<Teacher> getTeacherTwoList(Teacher teacher);}
mybatis一对多关联查询两种方式相关推荐
- Mybatis打印调试sql的两种方式
问题描述 在使用mybatis进行开发的时候,由于可以动态拼接sql,这样大大方便了我们.但是也有一定的问题,当我们动态sql拼接的块很多的时候,我们要想从*mapper.xml中直接找出完整的sql ...
- Mybatis一对多关联查询,返回值Map,字段自动映射
功能描述 由于查询字段和表名都要支持动态配置,故查询返回值需要为List<Map<String,Object>>,不定义值对象. 查询结果列需要支持自动映射,不配置类属性和数据 ...
- mybatis一对多关联查询_Mybatis 一对一、一对多的关联查询 ?
<mapper namespace="com.lcb.mapping.userMapper"> <!--association 一对一关联查询 --> &l ...
- mybatis一对多关联查询_一对一,一对多,多对多查询及延迟加载(N+1问题)分析
推荐学习 重识SSM,"超高频面试点+源码解析+实战PDF",一次性干掉全拿走 全网独家的"MySQL高级知识"集合,骨灰级收藏,手慢则无 "吃&qu ...
- Mybatis批量插入数据的两种方式
总体描述 软件开发过程中需要批量插入数据的场景有几种: 从离线文件(excel, csv等)导入大批量数据到系统. 从其它系统定时或者人工同步大批量数据到系统. 程序自身的某些算法执行时会生成大批量数 ...
- mybatis一对多关联查询将结果封装到嵌套list
DishDto中有个List集合保存DishFlavor对象,DishFlavor根据关联查询获得,一个dish对应多个dishflavor.想要联合查询自动将DishFlavor封装成集合需要用co ...
- 15、mybatis一对多关联查询 collection定义关联集合封装规则及懒加载
文章目录 1.collection定义关联集合封装规则单步查询 1).Dept增加集合属性 2).DeptMapper增加查询接口 3).DeptMapper.xml增加collection配置 4) ...
- mybatis中批量插入的两种方式(高效插入)
MyBatis简介 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以使用 ...
- 多表关联查询两种方法
简单总结一下,方便以后查看 A表(mxg_category) B表(mxg_label) B表的category_id 关联A表的ID 通过A表查询B表的id和name,并赋别名 label_id 和 ...
最新文章
- 【论文速读】基于投影方法的激光雷达点云处理比较
- 观察者模式的Java实现及应用
- 2018微信年度数据报告:00后最爱表情捂脸哭 80后呲牙笑
- 关于使用android系统设备充当web服务器的一点准备
- SVC和PendSV
- 计算机排版技能会操方案,实验九 Word的高级排版技巧.pdf
- Windows10/11安装Linux子系统Ubuntu 20.04LTS,轻松使用生信软件,效率秒杀虚拟机
- 小米相机曝光_小米11pro曝光,超级屏+双6400万+骁龙875,不愧是小米旗舰
- java命令javac java,使用命令行JAVAC编译Java
- 想加入Google AI中国团队?你可能得飞去美国面试
- Git详解之六 Git工具(转)
- 算数or算卦,和业务人谈“预测”到底在谈啥?
- Python实现将mp3音频格式转换为wav格式
- Nlp预处理方法(BPE Byte pair encoding、Normalization、Lemmatisation、Stemming…)
- C语言学习-- 计算机原理及二进制
- GameCenter首次登录很慢的解决方案
- 《创新者的基因》读书笔记
- Android 接入穿山甲激励视频广告步骤与错误总结
- C# ——字符添加角标
- Android 核心技术