05 映射文件-select
select
返回 List
<!-- public List<Employee> getEmpByLastNameLike(String lastName);-->
<!-- resultType:如果返回的是一个集合,要写集合中元素的类型-->
<select id="getEmpByLastNameLike" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee where last_name like #{lastname}
</select>
记录封装map
//返回一条记录的map,key就是列名,值就是对应的值
public Map<String,Object> getEmpByIdReturnMap(Integer id);
//返回多条记录封装一个map,Map<Integer,Employee>:键是这条记录的主键,值是记录封装后的java对象
@MapKey("id")//封装这个map的时候使用哪个属性作为map的key
public Map<Integer,Employee> getEmpByLastNameLikeReturnMap(String lastName);
<!-- public Map<String,Object> getEmpByIdReturnMap(Integer id);-->
<select id="getEmpByIdReturnMap" resultType="map">
select * from tbl_employee where id=#{id}
</select>
<!-- public Map<Integer,Employee> getEmpByLastNameLikeReturnMap(String lastName);-->
<select id="getEmpByLastNameLikeReturnMap" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee where last_name like #{lastName}
</select>
自定义resultMap
<!-- 自定义javaBean的封装规则
type:自定义规则的java类型
id:唯一id 方便引用
-->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyEmp">
<!--指定主键列的封装规则
column:指定哪一列
property:指定对应的javaBean属性
-->
<id column="id" property="id"/>
<!--定义普通列封装会做-->
<result column="last_name" property="lastName"/>
<!--其他不指定的列会自动封装,我们要写resulMao就把全部的映射规则都写上-->
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</resultMap>
<!-- public Employee getEmpById(Integer id);-->
<select id="getEmpById" resultMap="MyEmp">
select * from tbl_employee where id = #{id}
</select>
场景一
<!--
场景1:查询emplpyee的同时查询员工对应的部门
一个员工有与之对应的部门信息
-->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDiEmp">
<id column="id" property="id"></id>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<result column="did" property="dept.id"/>
<result column="dept_name" property="dept.departmentName"/>
</resultMap>
<!--使用assocation定义单个对象的封装规则-->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MyDiEmp2">
<id column="id" property="id"></id>
<result column="last_name" property="lastName"/>
<result column="gender" property="gender"/>
<!--association 可以指定联合的javaBean对象
property:指定哪个属性是联合对象
javaType:指定这个属性对象的类型
-->
<association property="dept" javaType="com.atguigu.mybatis.bean.Department">
<id column="did" property="id"/>
<result column="dept_name" property="dept.departmentName"/>
</association>
<!-- public Employee getEmpAndDept(Integer id);-->
<select id="getEmpAndDept" resultMap="">
SELECT e.id id,e.last_name last_name,e.gender gender,e.d_id d_id,
d.id did,d.dept_name dept_name
FROM tbl_employee e,tbl_dept d
WHERE e.d_id = d.id AND e.id = #{id}
</select>
association 分步查询
<!-- 使用association进行分布查询
1、先按照员工id查询员工信息
2、根据查询员工信息中的d_id值去部门表查出部门信息
3、部门设置到员工中
-->
<resultMap id="com.atguigu.mybatis.bean.Employee" type="MyEmpByStep">
<id column="id" property="id"></id>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<!--关联对象的封装规则
select:表明当前属性是调用select指定的方法查出的结果
column:将那一列的值传给这个方法
流程:使用select指定的方法(闯入column指定的这列参数的值)查出对象,并封装给property指定的属性
-->
<association property="dept"
select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
column="d_id"></association>
</resultMap>
延迟加载
<!-- 可以延迟加载(懒加载) 按需加载
我们每次查询Employee对象的时候,都将一起查询出来
部门信息在我们使用的时候再去查询
分步查询的基础上加上 两个配置
在全部配置文件中加上
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
-->
collection定义关联集合封装规则
<!--
场景二:
查询部门的时候将部门对应的所有员工信息也查询出来
-->
<resultMap id="MyDept" type="com.atguigu.mybatis.bean.Department">
<id column="did" property="id"></id>
<result column="dept_name " property="departmentName"/>
<!-- collection定义集合类型的属性的封装规则
ofType:指定集合中元素的封装规则
-->
<collection property="emps" ofType="com.atguigu.mybatis.bean.Employee">
<!-- 定义这个集合中元素的封装规则-->
<id column="eid" property="id"></id>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</collection>
</resultMap>
<!-- public Department getDeptByIdPlus(Integer id);-->
<select id="getDeptByIdPlus" resultMap="">
select d.id did,dept_name dept_name,
e.id eid,e.last_name last_name,e.mail main,e.gender gender
from tbl_dept d
left join tbl_employee e
on d.id=e.id_id
where d,id=#{id}
</select>
collection分布查询
<resultMap id="MyDeptStep" type="com.atguigu.mybatis.bean.Department">
<id column="id" property="id"/>
<id column="dept_name" property="department"/>
<collection property="emps"
select="com.atgui.mybatis.dao.EmployeeMapperPlus.getEmpsbyDeptId"
column="id"></collection>
</resultMap>
<!-- public Department getDeptByIdStep(Integer id);-->
<select id="getDeptByIdStep" resultMap="">
select id,dept_name departmentName from tbl_dept where id = #{id}
</select>
扩展:多列的值传递过去
将多列的值封装map传递
colum="{key1=column1,key2=column2}"
fetchType="lazy" 表示使用延迟加载
eager:立即
lazy:延迟
<resultMap id="MyDeptStep" type="com.atguigu.mybatis.bean.Department">
<id column="id" property="id"/>
<id column="dept_name" property="department"/>
<collection property="emps"
select="com.atgui.mybatis.dao.EmployeeMapperPlus.getEmpsbyDeptId"
column="{deptId=id}" fetchType="lazy"></collection>
</resultMap>
discriminator鉴别器
<!-- <discriminator javaType=""></discriminator>
鉴别器:mybatis可以使用discriminator判断某列的值,然后根据某列的值改变封装行为封装Employee
实例:如果查出的女生,就把部门信息 查询出来,否则不查询
如果是男生,把lst_name这一列的值赋值给email
-->
<resultMap type="com.atguigu.mybatis.bean.Employee" id="myEmp">
<id column="id" property="id"></id>
<result column="last_name" property="lastName"/>
<result column="email" property="email"/>
<result column="gender" property="gender"/>
<!--
column:指定判定的列名
javaType:列值对应的Java类型-->
<discriminator javaType="string" column="gender">
<!-- 女生 javaType:指定封装的结果类型-->
<case value="0" resultType="com.atguigu.mybatis.bean.Employee">
<association property="dept"
select="com.atguigu.mybatis.dao.DepartmentMapper.getDeptById"
column="d_id">
</association>
</case>
<!--男生-->
<case value="1" resultType="com.atguigu.mybatis.bean.Employee">
<id column="id" property="id"></id>
<result column="last_name" property="lastName"/>
<result column="last_name" property="email"/>
<result column="gender" property="gender"/>
</case>
</discriminator>
</resultMap>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)