<association>关联的结果查询
1.resultType和resultMap的区别
2.<association>关联的结果查询
3.新增返回主键4.<foreach/>标签原理及用法5.返回List<Map<String, String>>键值对格式6.<collection>映射集合结果查询7.Mybatis的执行流程8.MyBatis写入大量数据9.MyBatis读取大量数据(流式读取)10.MyBatis Generator 自动生成工具(dao / mapping / model)11.MyBatisPlus怎么忽略映射字段12.MyBatisPlus AutoGenrator代码自动生成13.<resultMap/> 标签支持继承 extends14.SpringData JPA、Hibernate、Mybatis三者的区别15.Mybatis代码生成——velocity模板元素16.Mybatis代码生成——velocity模板语法association:用于对象间包含关系映射
方式一:通过association标签来封装结果集
<mapper namespace="org.apache.ibatis.submitted.associationtest.Mapper"> <resultMap type="org.apache.ibatis.submitted.associationtest.Car" id="carResult"> <id column="carid" property="id"/> <result column="cartype" property="type"/> <association property="engine" resultMap="engineResult"/> <association property="brakes" resultMap="brakesResult"/> </resultMap> <resultMap type="org.apache.ibatis.submitted.associationtest.Engine" id="engineResult"> <result column="enginetype" property="type"/> <result column="enginecylinders" property="cylinders"/> </resultMap> <resultMap type="org.apache.ibatis.submitted.associationtest.Brakes" id="brakesResult"> <result column="brakesType" property="type"/> </resultMap> <select id="getCars" resultMap="carResult"> select * from cars </select> <select id="getCarsNonUnique" resultMap="carResult"> select 1 as carid, cartype, enginetype, enginecylinders, brakestype from cars </select> <select id="getCars2" resultMap="carResult"> select 1 as carid, cartype, enginetype, enginecylinders, brakestype from cars where carid in (1,2) </select> </mapper>
查询:
<select id="getEmpAndDept" resultMap="complexEmp2"> SELECT e.id id,e.last_name last_name,e.email email,e.gender gender,e.d_id d_id,d.id did,d.dept_name dept_name FROM tb1_emplyee e,tb1_dept d WHERE e.d_id=d.id AND e.id=#{id} </select>
方式二:通过association标签实现分段查找
<resultMap id="MyEmpByStep" type="com.test.beans.Employee"> <id column="id" property="id"/> <result column="last_name" property="lastName"/> <result column="email" property="email"/> <result column="gender" property="gender"/> <!-- association定义关联对象的封装规则 select:表明当前属性是调用select指定的方法查出的结果 column:指定将哪一列的值传给这个方法 流程:使用select指定的方法(传入column指定的这列参数的值)查出对象,并封装给property指定的属性 --> <association property="dept" select="com.test.dao.DepartMentMapper.getDeptById" column="d_id"> </association> </resultMap>
对应的Department映射文件:
<mapper namespace="com.test.dao.DepartMentMapper"> <select id="getDeptById" resultType="com.test.beans.Department"> SELECT id,dept_name departmentName FROM tb1_dept WHERE id=#{id} </select> </mapper>
查询:
<select id="getEmpByIdStep" resultMap="MyEmpByStep"> select * from tb1_emplyee where id=#{id} </select>
association标签还可以实现懒加载的功能
什么是懒加载呢?
前面的分步查询,每查询一次都会执行两次sql(一次查询员工,一次查询部门)
有时候我们并不需要插叙部门的情况,所以懒查询就可以帮我们解决这个问题,提高效率,减少资源的占用
懒加载的实现也非常简单,只要在全局配置文件中添加如下的配置代码即可:
<settings> <!--显示的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题 --> <setting name="lazyLoadingEnabled" value="true"/> <setting name="aggressiveLazyLoading" value="false"/> </settings>
合集:
MyBatis
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了