Mybatis-技术专区-如何清晰的解决出现「多对一模型」和「一对多模型」的问题
前提介绍
在mybatis如何进行多对一、一对多(一对一)的多表查询呢?本章带你认识如何非常顺滑的解决!
基础使用篇
一对一
association
association通常用来映射一对一的关系,例如,有个类user,对应的实体类如下:
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class Student {
private int id;
private String name;
/**
* 学生要关联一个老师
*/
private Teacher teacher;
}
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class Teacher {
private int id;
private String name;
}
Dao层进行Mapper查询操作
public interface TeacherMapper {
Teacher getTeacher(@Param("tid") int id);
Teacher getTeacher2(@Param("tid") int id);
}
Dao层进行Mapper.xml文件
<resultMap id="StudentTeacher" type="com.sunreal.pojo.Student">
<result column="id" property="id"></result>
<result column="name" property="name"></result>
<association property="teacher" column="id" javaType="com.sunreal.pojo.Teacher" select="getTeacher"/>
</resultMap>
<select id="getStudent" resultMap="StudentTeacher">
select *
from student
</select>
<select id="getTeacher" resultType="com.sunreal.pojo.Teacher">
select *
from teacher
where id = #{id}
</select>
<resultMap id="StudentTeacher2" type="com.sunreal.pojo.Student">
<result column="sid" property="id"></result>
<result column="sname" property="name"></result>
<association property="teacher" javaType="com.sunreal.pojo.Teacher">
<result property="name" column="tname"></result>
</association>
</resultMap>
<select id="getStudent2" resultMap="StudentTeacher2">
select s.id sid, s.name sname, t.name tname
from student s,
teacher t
where s.tid = t.id
</select>
- assocication:可以指定联合的JavaBean对象
- select:指定相关查询结果sqlid
- property="role“:指定哪个属性是联合的对象
- javaType:指定这个属性对象的类型
- column="{javabean熟悉=数据库字段,Javabean属性=数据库字段}"
<association property="role" javaType="com.queen.mybatis.bean.Role">
<id column="role_id" property="id"/>
<result column="roleName" property="roleName"/>
</association>
以上如果跨越命名空间的情况下:select:需要用namespace.selectId进行指定。
collection
@Alias("Student")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class Student {
private int id;
private String name;
private int tid;
}
@Alias("Teacher")
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class Teacher {
private int id;
private String name;
/**
* 一个老师包含多个学生
*/
private List<Student> studentList;
}
Dao层进行Mapper查询操作
public interface TeacherMapper {
Teacher getTeacher(@Param("tid") int id);
Teacher getTeacher2(@Param("tid") int id);
}
Dao层进行Mapper.xml文件
<resultMap id="TeacherStudent" type="Teacher">
<result property="id" column="tid"></result>
<result property="name" column="tname"></result>
<collection property="studentList" ofType="Student">
<result property="id" column="sid"></result>
<result property="name" column="sname"></result>
<result property="tid" column="tid"></result>
</collection>
</resultMap>
<select id="getTeacher" resultMap="TeacherStudent">
select s.id sid, s.name sname, t.name name, t.id tid
from student s,
teacher t
where s.tid = t.id
and t.id = #{tid}
</select>
<resultMap id="TeacherStudent2" type="Teacher">
<collection property="studentList" javaType="ArrayList" ofType="Student"
select="getStudentByTeacherId" column="id"/>
</resultMap>
<select id="getTeacher2" resultMap="TeacherStudent2">
select *
from teacher
where id = #{tid}
</select>
<select id="getStudentByTeacherId" resultType="Student">
select *
from student
where tid = #{tid}
</select>
注意:各个表之间尽量不要有重名字段,包括主键id,不然可能会造成数据混乱错误;
- JavaType和ofType都是用来指定对象类型的
- property="指的是对象内部(List类型)的属性信息字段名称"
- JavaType是用来指定pojo中属性的类型
- ofType指定的是映射到list集合属性中pojo的类型。
- column="{javabean熟悉=数据库字段,Javabean属性=数据库字段}"
- select:指定相关查询结果sqlid
”特叔“使用篇
一对一映射
实体列 class Tb_blog/TbBlog
private long blogId;
private String blogTitle;
private String blogContent;
private Date createTime;
private String blogType;
private String sId;
private Tb_author author;
List<TbAuthor> tbAuthorList;
实体类 class TbAuthor
private long id;
private String username;
private String password;
private String email;
private String address;
private String phone;
private TbBlog tbBlog;
private List<TbBlog> tbBlogList;
resultMap标签配置
<resultMap id="blogMap" type="Tb_blog" >
<id column="blogId" property="blogId"/>
<result column="blogTitle" property="blogTitle"/>
<result column="blogContent" property="blogContent"/>
<result column="blogType" property="blogType"/>
<result column="createTime" property="createTime"/>
<result column="sId" property="sId"/>
<result column="id" property="author.id"/> <!-- 映射第二张表的实体类属性 -->
<result column="username" property="author.username"/>
<result column="password" property="author.password"/>
<result column="email" property="author.email"/>
</resultMap>
<select id="selectBlogAndAuthor" resultMap="blogMap">
select * from tb_blog g inner join tb_author r
on g.blogId = r.id
</select>
在sql加入别名alias与field属性字段一样,也可以自动注入进入。
association标签配置
<resultMap id="blogMap" type="Tb_blog" >
<id column="blogId" property="blogId"/>
<result column="blogTitle" property="blogTitle"/>
<result column="blogContent" property="blogContent"/>
<result column="blogType" property="blogType"/>
<result column="createTime" property="createTime"/>
<!-- 一对一高效率写法 association一对一关联 property属性为实体类中的第二张表的属性名 -->
<association property="tb_author" javaType="TbAuthor"><!--javaType属性为 返回的实体类对象 -->
<id column="id" property="id"/>
<result column="username" property="username"/>
<result column="password" property="password"/>
<result column="email" property="email"/>
<result column="address" property="address"/>
</association>
</resultMap>
<select id="selectBlogAndAuthor" resultMap="blogMap">
select * from tb_blog g inner join tb_author r on g.blogId = r.id
</select>
collection标签配置
mapper接口定义
AuthorMapper.interface
//!通过id 和映射文件中 association的column属性的值sId关联 来嵌套查询 嵌套查询的第二条sql语句都要写条件来关联第一张表
List<TbAuthor> selectAuthorandBlogAssociation(int id);
BlogMapper.interface
List<TbBlog> selectBlogAndAuthorAssociation();
AuthorMapper.xml
<select id="selectAuthorandBlogAssociation" resultType="com.xqh.pojo.TbAuthor">
select * from tb_author where id=#{id}
</select>
<resultMap id="mapCollection" type="TbAuthor">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="email" column="email"/>
<result property="phone" column="phone"/>
<result property="address" column="address"/>
<collection property="tbBlogList" column="id"
select="com.xqh.mapper.BlogMapper.selectBlogAndAuthor"
fetchType="lazy">
</collection>
</resultMap>
<select id="selectAuthor_BlogList" resultMap="mapCollection">
select * from tb_author
</select>
BlogMapper.xml
<select id="selectBlogAndAuthor" resultType="com.xqh.pojo.TbBlog">
select * from tb_blog where sId = #{id}
</select>
总结
多表查询一对一映射
association标签
不嵌套 property=当前实体类中的第二种表的属性名 javaType=返回的实体类
嵌套 多加两个属性 column=当前实体类 关联的 第二张表 的外键字段 select=“第二条查询语句” (必须给第二条sql语句写参数限制 不然会获得所有值)
多表查询一对多
collection标签
不嵌套 property=当前实体类中的第二种表的属性名 ofType=返回是实体类
property=当前实体类中的第二种表的属性名 javaType=返回的实体类
嵌套 多加两个属性 column=当前实体类 关联的 第二张表 的外键字段 select=“第二条查询语句” (必须给第二条sql语句写参数限制 不然会获得所有值)
2.多表查询一对多
collection标签
不嵌套 property=当前实体类中的第二种表的属性名 ofType=返回是实体类
嵌套 多加一个属性 column=当前实体类 关联的 第二张表 的外键字段 select=“第二条查询语句” (必须给第二条sql语句写参数限制 不然会获得所有值) [ofType = collection一对多嵌套查询 嵌套查询所有结果 不需写返回类型因为 select已经映射]
Mybatis-技术专区-如何清晰的解决出现「多对一模型」和「一对多模型」的问题的更多相关文章
- 深入理解Mybatis技术与原理
目录 第1章 Mybatis简介 1.1 传统的JDBC编程 1.2 ORM模型 1.4 MyBatis 1.5 什么时候用MyBatis 第2章 MyBatis入门 2.2 MyBatis构成 2. ...
- 2017.2.9 深入浅出MyBatis技术原理与实践-第八章 MyBatis-Spring(二)-----配置文件详解
深入浅出MyBatis技术原理与实践-第八章 MyBatis-Spring(二) ------配置文件详解 8.2 MyBatis-Spring应用 8.2.1 概述 本文主要讲述通过注解配置MyBa ...
- Mybatis 关联对象不能输出的解决办法
Mybatis 关联对象不能输出的解决办法 1.如图所示,现在进行查询的时候并没有得到来自另一张表address项 2.我们进行如下配置: (1).在mybatis-config.xml 文件中配置, ...
- Mybatis技术内幕(一)——整体架构概览
Mybatis技术内幕(一)--整体架构概览 Mybatis的整体架构分为三层,分别是基础支持层.核心处理层和接口层. 如图所示: 一.基础支持层 基础支持层包含整个Mybatis的基础模块,这些模块 ...
- Mybatis技术原理理——整体流程理解
前言:2018年,是最杂乱的一年!所以你看我的博客,是不是很空! 网上有很多关于Mybatis原理介绍的博文,这里介绍两篇我个人很推荐的博文 Mybatis3.4.x技术内幕和 MyBaits源码分析 ...
- 微服务-技术专区-链路追踪(pinpoint)-部署使用
https://naver.github.io/pinpoint/ https://github.com/naver/pinpoint 背景 随着项目微服务的进行,微服务数量逐渐增加,服务间的调用也越 ...
- mybatis技术总结
一.框架概述 day1 1.什么是框架 框架是系统的可重用设计,是对J2EE底层技术的封装(JDBC,IO流,多线程,Servlet,Socket). 2.框架解决了哪些问题? 1.解决了技术整合问题 ...
- 2MyBatis入门--深入浅出MyBatis技术原理与实践(笔记)
什么是 MyBatis ? MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis ...
- 【MyBatis学习06】_parameter:解决There is no getter for property named in class java.lang.String
我们知道在mybatis的映射中传参数,只能传入一个.通过#{参数名} 即可获取传入的值. Mapper接口文件: public int delete(int id) throws Exception ...
随机推荐
- 【贪心+排序】排队接水 luogu-1223
题目描述 有n个人在一个水龙头前排队接水,假如每个人接水的时间为Ti,请编程找出这n个人排队的一种顺序,使得n个人的平均等待时间最小. 分析 注意要开longlong AC代码 #include &l ...
- js中的 true 与 false
可判断为 false 的情况: 0,-0,NaN,undedined,"",false,null,缺省的值 可判断为 true 的情况: 除false的其他情况均可,包括负数.&q ...
- linux统计nginx日志中请求访问量命令
Nginx 三种分配策略:轮询.权重.ip_hash(比如你登录了一个网站,登录信息已经保存到 a 机器,但当你做后续操作时的请求会到 b 机器,那么就获取不到你原来登录的信息,此时你就需要重新登录了 ...
- Magento 2.2 SQL注入漏洞
影响版本 2.2.* poc地址 https://github.com/ambionics/magento-exploits python3 magento-sqli.py http://192.1 ...
- Docker介绍及安装详解
1:Docker简介 Docker 是一种运行于 Linux 和 Windows 上的软件,用于创建.管理和编排容器.Docker 是在 GitHub 上开发的 Moby 开源项目的一部分.Docke ...
- SpringMVC 源码解析笔记
作者笔记仓库:https://github.com/seazean/javanotes 欢迎各位关注我的笔记仓库,clone 仓库到本地后使用 Typora 阅读效果更好. 一.调度函数 请求进入原生 ...
- NDIS LWF:NdisFSendNetBufferLists蓝屏(DRIVER_IRQL_NOT_EQUAL_OR_LESS)
调用NdisFSendNetBufferLists发送自定义数据包后蓝屏,蓝屏代码为DRIVER_IRQL_NOT_EQUAL_OR_LESS,如果创建的NBL都没问题,一定要确保该自定义的NBL要在 ...
- NAR | 张勇洪/周超/刘小云团队合作揭示2-羟基异丁酰化修饰调控光暗适应性反应机制
景杰生物 | 报道 组蛋白赖氨酸的翻译后修饰是表观遗传学密码的重要组成部分,它们动态地调节染色质的结构和功能,影响基因表达活性,参与生物体的环境适应性调控.赖氨酸酰化修饰家族(Acylation) ...
- 2021年最新字节跳动Android面试真题解析
概述 时间过得是真TM快,回想自己是16年从学校毕业,现在是出来工作的第五个年头啦.在不同的大小公司都待过,就在前段时间顺利的完成了一次跳槽涨薪,面试了几家公司,最终选择了字节跳动.今特此前来跟大家进 ...
- Spring Cloud Alibaba - Feign
Feign Feign简介 使用Feign实现消费者客户端 使用Feign+Ribbon实现客户端负载均衡 底层的负载均衡策略还是使用Ribbon通过Feign进行调用 Feign的相关配置 ribb ...