[mybatis]映射文件_select_resultMap_关联查询_association分步查询延迟加载

association分步查询

场景一

查询Employee的同时查询员工对应的部门
Employee===Department
一个员工有与之对应的部门信息

Employee表:

在这里插入图片描述
Department表:

在这里插入图片描述

public interface DepartmentMapper {public Department getDeptById(Integer id);}
public interface EmployeeMapperPlus {public Employee getEmpByIdStep(Integer id);}
    <!--        public Department getDeptById(Integer id);
--><select id="getDeptById" resultType="com.atguigu.mybatis.bean.Department">select id,dept_name departmentName from tb1_dept where id = #{id}</select>
 <resultMap id="MyEmpStep" type="com.atguigu.mybatis.bean.Employee"><!--        1.先按照员工id查询员工信息--><!--        2.根据查询员工信息中的d_id值去部门表查出部门信息--><!--        3.部门设置到员工中--><id column="id" property="id"></id><result column="last_name" property="lastName"></result><result column="email" property="email"></result><result column="gender" property="gender"></result><!--        association定义关联对象的封装规则select:表明当前属性是调用select指定的方法查出的结果column:指定将哪一列的值传给这个方法流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性--><association property="dept" select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"column="d_id"></association></resultMap><!--    public Employee getEmpByIdStep(Integer id);--><select id = "getEmpByIdStep" resultMap="MyEmpStep">select * from tb1_employee where id = #{id}</select>
    @Testpublic void test03() throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession sqlSession = sqlSessionFactory.openSession();try{EmployeeMapperPlus mapper = sqlSession.getMapper(EmployeeMapperPlus.class);Employee emp = mapper.getEmpByIdStep(1);System.out.println(emp);System.out.println(emp.getDept());}finally {sqlSession.close();}}

在这里插入图片描述

延迟加载

Employee ===> Dept
我们每次查询Employee对象的时候,都将一起查询出来
分段查询的基础上,使用延迟加载
可以使部门信息在我们使用的时候再去查询

lazyLoadingEnabled & aggressiveLazyLoading

在这里插入图片描述

    <settings><setting name="lazyLoadingEnabled" value="true"/><setting name="aggressiveLazyLoading" value="false"/></settings>

在这里插入图片描述
在这里插入图片描述

场景二

查询部门的时候将部门对应的所有员工信息也查询出来

public class Department {private Integer id;private String departmentName;private List<Employee> emps;}
public interface DepartmentMapper {public Department getDeptByIdPlus(Integer id);}
<resultMap id="MyDept" type="com.atguigu.mybatis.bean.Department"><id column="did" property="id"></id><result column="dept_name" property="departmentName"></result><!--collection定义关联集合类型的属性的封装规则ofType:指定集合里面元素的类型--><collection property="emps" ofType="com.atguigu.mybatis.bean.Employee" ><!--定义这个集合中元素的封装规则--><id column="eid" property="id"></id><result column="last_name" property="lastName"></result><result column="email" property="email"></result><result column="gender" property="gender"></result></collection></resultMap><!--    public Department getDeptByIdPlus(Integer id);--><select id="getDeptByIdPlus" resultMap = "MyDept">SELECT d.id did, d.dept_name dept_name ,e.id eid,e.last_name last_name,e.email email,e.gender genderFROM tb1_dept d
LEFT JOIN tb1_employee e
ON d.id = e.d_id
WHERE d.id = #{id}</select>
@Testpublic void test04() throws IOException {SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();SqlSession sqlSession = sqlSessionFactory.openSession();try{DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);Department deptByIdPlus = mapper.getDeptByIdPlus(1);System.out.println(deptByIdPlus);System.out.println(deptByIdPlus.getEmps());}finally {sqlSession.close();}}

扩展:多列的值传递过去

在这里插入图片描述

将多列的值封装map传递
column="{key1=column1,key2=column2}"
fetchType=“lazy”:表示使用延迟加载;
lazy:延迟
eager:立即

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/308802.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C#中你想象的Task,很简单?

【导读】网上关于Task的文章如数家珍&#xff0c;不过有一部分并未谈到一个根本的问题&#xff0c;所创建的Task一定在线程池上运行&#xff1f;如何合理的使用Task&#xff1f;这里并不会去重新讲解每一个APi的使用&#xff0c;没有任何意义&#xff0c;这属于包括我在内的各位…

【复杂系统迁移 .NET Core平台系列】之应用发布与部署

源宝导读&#xff1a;微软跨平台技术框架—.NET Core已经日趋成熟&#xff0c;已经具备了支撑大型系统稳定运行的条件。本文将介绍明源云ERP平台从.NET Framework向.NET Core迁移过程中的实践经验。一、背景随着ERP的产品线越来越多&#xff0c;业务关联也日益复杂&#xff0c;…

[mybatis]映射文件_select_resultMap_discriminator鉴别器

discriminator 鉴别器&#xff1a;mybatis可以使用discriminator判断某列的值&#xff0c;然后根据某列的值改变封装行为 封装Employee: 如果查出的是女生&#xff1b;就把部门信息查询出来&#xff0c;否则不查询 如果查出的是男生&#xff1b;把last_name这一列的值赋给ema…

不懂数据库索引原理?因为你心里没有一点B树

什么是B树&#xff1f;B树是一种数据结构它按排序顺序在其节点中存储数据&#xff0c;B树存储数据使得每个节点按升序包含密钥&#xff0c;这些键中的每一个都有两个对另外两个子节点的引用&#xff0c;Te左侧子节点键小于当前键右侧子节点键多于当前键&#xff0c;如果单个节点…

[mybatis]缓存_二级缓存使用细节

二级缓存 开启全局二级缓存 <setting name"cacheEnabled" value"true"/>去mapper.xml中配置使用二级缓存 <cache></cache><cache eviction"FIFO" flushInterval"60000" readOnly"false" size"1…

5分钟快速接入钉钉实现钉钉考勤

一、前言由于今年疫情影响&#xff0c;假期的无限延长让大家都不得不进行线上办公&#xff0c;说到线上办公就毫无疑问&#xff0c;钉钉是这个疫情假期最大的赢家&#xff0c;APP的火热程度以及下载量甚至压过了微信&#xff0c;跃居App store免费排行榜第1名的位置。最早我们知…

[mybatis]缓存机制介绍_一级缓存二级缓存

两级缓存&#xff1a; 一级缓存&#xff1a;&#xff08;本地缓存&#xff09;&#xff1a;sqlSession级别的缓存。一级缓存是一直开启的&#xff1b;sqlSession级别的一个Map ​ 与数据库同一次会话期间查询到的数据会放在本地缓存中。 以后如果需要获取相同的数据&#xff0c…

使用Azure Application Insignhts监控ASP.NET Core应用程序

Application Insignhts是微软开发的一套监控程序。他可以对线上的应用程序进行全方位的监控&#xff0c;比如监控每秒的请求数&#xff0c;失败的请求&#xff0c;追踪异常&#xff0c;对每个请求进行监控&#xff0c;从http的耗时&#xff0c;到SQL查询的耗时&#xff0c;完完…

[mybatis]动态sql_内置参数_parameter_databaseid

mybatis内置参数 mybatis默认还有两个内置参数: _parameter&#xff1b;代表整个参数 单个参数:_parameter就是这个参数 多个参数:参数会被封装为一个map; _parameter就是代表这个map _databaseId&#xff1b;如果配置了DatabaseIdProvider标签 _databaseId就是代表当前数据…

[mybatis]逆向工程MGB基本编写

逆向工程 项目结构 依赖 <dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.7</version> </dependency>mgb.xml <?xml version"1.0" en…

[SpringBoot2]HelloWorld

导入依赖 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version></parent><dependencies><dependency><groupId>or…

[SpringBoot2]SpringBoot应用如何编写

最佳实践 ● 引入场景依赖 ○ https://docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-starter ● 查看自动配置了哪些&#xff08;选做&#xff09; ○ 自己分析&#xff0c;引入场景对应的自动配置一般都生效了 ○ 配置文件中d…

Linux内核:容器底层cgroup如何使用

在前面的文章中&#xff0c;我们探讨了容器底层 cgroup 的数据结构与代码实现&#xff0c;本期是 cgroup 系列的最后一篇文章&#xff0c;我们将继续探讨在 mount 成功后&#xff0c;我们如何使用 cgroup 来实现进程限制。在 mount 成功后&#xff0c;cgroup_root 已经存在了&a…

[SpringBoot2]容器功能_底层注解配置绑定_@Configuration@Import@Conditional@ImportResource

Configuration&Bean 告诉SpringBoot这是一个配置类配置文件 #############################Configuration使用示例###################################################### /*** 1、配置类里面使用Bean标注在方法上给容器注册组件&#xff0c;默认也是单实例的* 2、配置…

.NET Core微服务开发服务间调用篇-GRPC

在单体应用中&#xff0c;相互调用都是在一个进程内部调用&#xff0c;也就是说调用发生在本机内部&#xff0c;因此也被叫做本地方法调用&#xff1b;在微服务中&#xff0c;服务之间调用就变得比较复杂&#xff0c;需要跨网络调用&#xff0c;他们之间的调用相对于与本地方法…

[SpringBoot2]自动配置

自动配置 ● 自动配好Tomcat 引入Tomcat依赖。配置Tomcat <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><version>2.3.4.RELEASE</version><scope>compil…

基于.NetCore3.1搭建项目系列 —— 认证授权方案之Swagger加锁

1开始在之前的使用Swagger做Api文档中&#xff0c;我们已经使用Swagger进行开发接口文档&#xff0c;以及更加方便的使用。这一转换&#xff0c;让更多的接口可以以通俗易懂的方式展现给开发人员。而在后续的内容中&#xff0c;为了对api资源的保护&#xff0c;我们引入了认证授…

[SpringBoot2]依赖管理

依赖管理 父项目做依赖管理 <parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version> </parent>他的父项目 <parent><groupId…