4.Mybatis 映射问题

一.自定义映射关系:ResultMap

属性:

  • id 属性:表示自定义映射的唯一标识
  • type 属性:查询的数据要映射的实体类的类型

子标签

  • id 子标签:设置主键的映射关系
  • result 子标签:设置普通字段的映射关系
  • association 子标签:设置多对一的映射关系
  • collection 子标签:设置一对多的映射关系
  • 子标签属性:
    • property 属性:设置映射关系中实体类中的属性名
    • column 属性:设置映射关系中表中的字段名
1、一对一映射:解决字段和属性不一样

若字段名和实体类中的属性名不一致,但是字段名符合数据库的规则(使用_),实体类中的属性 名符合Java的规则(使用驼峰)

a. resutltType:内置映射关系
  • 方案一,起别名:给SQL语句中的字段起别名(和属性名保持一致)
  • 方案二,全局配置:在MyBatis配置文件中添加< settings />标签
<settings>
  <!--将_自动映射成驼峰-->
  <setting name="mapUnderscoreToCamelCase" value="true"></setting>
</settings>
b. resultMap:自定义映射关系
<resultMap id="userMap" type="User">
  <id property="id" column="id"></id>
  <result property="userName" column="user_name"></result>
  <result property="password" column="password"></result>
  <result property="age" column="age"></result>
  <result property="sex" column="sex"></result>
</resultMap>

<!--List<User> testMohu(@Param("mohu") String mohu);-->
<select id="testMohu" resultMap="userMap">
  <!--select * from t_user where username like '%${mohu}%'-->
  select id,user_name,password,age,sex from t_user where user_name like
  concat('%',#{mohu},'%')
</select>
2、一对多的映射关系

设置实体类与实体类之间的对应关系,如:部门——员工

  • 部门(一):添加员工集合属性
  • 员工类(多):添加部门对象属性
a. 方案一:使用collection子标签,处理一对多的映射关系
  • property 属性:设置映射关系中实体类中的属性名
  • ofType 属性:设置collection标签所处理的集合中存储数据的类型
<resultMap id="deptEmpMap" type="Dept">
  <id property="did" column="did"></id>
  <result property="dname" column="dname"></result>
  <!--
  ofType:设置collection标签所处理的集合属性中存储数据的类型
  -->
  <collection property="emps" ofType="Emp">
    <id property="eid" column="eid"></id>
    <result property="ename" column="ename"></result>
    <result property="age" column="age"></result>
    <result property="sex" column="sex"></result>
  </collection>
</resultMap>

<!--Dept getDeptEmpByDid(@Param("did") int did);-->
<select id="getDeptEmpByDid" resultMap="deptEmpMap">
  select dept.*,emp.* from t_dept dept left join t_emp emp on dept.did =
  emp.did where dept.did = #{did}
</select>
b. 方案二:分步查询

第一步: DeptMapper查询部门信息

  • collection标签的select属性:sql的唯一标识—将第二步查询出来的结果赋值给property的属性值
  • collection标签的column属性:第二步查询的条件(先将部门查出来,以此条件查询员工表) | 以第一步查询出来的结果的某个字段作为第二部查询的条件。
  • collection标签的fetchType属性:eager属性值(立即加载,取消延迟加载)、lazy(延迟加载)
<resultMap id="deptAndEmpResultMapByStep" type="Dept">
  <id column="dept_id" property="deptId"></id>
  <result column="dept_name" property="deptName"></result>
  <collection property="emps"
    select="com.atguigu.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo"
    column="dept_id">
  </collection>
</resultMap>

<!--Dept getDeptAndEmpByStepOne(@Param("did") Integer did);-->
<select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpByStepResultMap">
  select * from t_dept where did = #{did}
</select>

第二步:EmpMapper根据did查询部门中所有员工

<!--List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);-->
<select id="getDeptAndEmpByStepTwo" resultType="Emp">
  select * from t_emp where dept_id = #{deptId}
</select>
3、多对一的映射关系

查询员工信息以及员工所对应的部门信息

  • 员工类(多):添加部门对象属性
  • 部门(一):添加员工集合属性
a.方案一:级联属性赋值
<resultMap id="empDeptMap" type="Emp">
  <id property="eid" column="eid"></id>
  <result property="empName" column="emp_name"></result>
  <result property="age" column="age"></result>
  <result property="sex" column="sex"></result>
  <result property="eamil" column="email"></result>
  <result column="did" property="dept.did"></result> 
  <result column="dname" property="dept.dname"></result>
</resultMap>

<!--Emp getEmpAndDeptByEid(@Param("eid") int eid);--> 
<select id="getEmpAndDeptByEid" resultMap="empDeptMap"> 
  select emp.*,dept.* from t_emp emp left join t_dept dept on emp.did = dept.did where emp.eid = #{eid} 
</select>
b.方案二:association标签
  • property 属性:需要处理多对一的映射关系的属性名
  • javaType 属性:该属性的类型
<resultMap id="empDeptMap" type="Emp">
  <id column="eid" property="eid"></id>
  <result column="ename" property="ename"></result>
  <result column="age" property="age"></result>
  <result column="sex" property="sex"></result>
  <association property="dept" javaType="Dept">
    <id column="did" property="did"></id>
    <result column="dname" property="dname"></result>
  </association>
</resultMap>

<!--Emp getEmpAndDeptByEid(@Param("eid") int eid);-->
<select id="getEmpAndDeptByEid" resultMap="empDeptMap">
  select emp.*,dept.* from t_emp emp left join t_dept dept on emp.did =
  dept.did where emp.eid = #{eid}
</select>
c.方案三:分步查询

第一步,EmpMapper:查询员工信息

  • association标签的select属性:sql的唯一标识—将第二步查询出来的结果赋值给property的属性值
  • association标签的column属性:第二步查询的条件(先将部门查出来,以此条件查询员工表) | 以第一步查询出来的结果的某个字段作为第二部查询的条件。
  • association标签的fetchType属性:eager属性值(立即加载,取消延迟加载)、lazy(延迟加载)
<!--Emp getEmpAndDeptByStepOne(@Param("eid")Integer eid);
-->
<resultMap id="empAndDeptByStepResultMap">
  <id property="eid" column="eid"></id>
  <result property="empName" column="emp_name"></result>
  <result property="age" column="age"></result>
  <result property="sex" column="sex"></result>
  <result property="eamil" column="email"></result>
  <!--column 分布查询的条件,select:sql的唯一标识——全类名.方法名-->
  <association property="dept" select="com.lemon.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="dept_id">
  </association>
</resultMap>

<select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
  select * from t_emp where eid = #{eid}
</select>

第二步,DeptMapper:通过did查询员工所对应的部门

<!--Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);-->
<select id="getEmpAndDeptByStepTwo" resultType="Dept">
  select * from t_dept where did = #{did}
</select>
4、延迟加载

分布查询可以实现延迟加载,需要在全局配置文件中设置全局配置信息:

  • lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载
  • aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载 此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。
  • 通过association和 collection中的fetchType属性设置当前的分步查询是否使用延迟加载, fetchType="lazy(延迟加 载)|eager(立即加载)"
<settings>
  <!--开启延迟加载-->
  <setting name="lazyLoadingEnabled" value="true"/>
  <!--按需加载-->
  <setting name="aggressiveLazyLoading" value="false"/>
</settings>
<resultMap id="deptEmpStep" type="Dept">
  <id property="did" column="did"></id>
  <result property="dname" column="dname"></result>
  <collection property="emps" fetchType="eager"
    select="com.atguigu.MyBatis.mapper.EmpMapper.getEmpListByDid" 
    column="did">
  </collection>
</resultMap>
<!--Dept getDeptByStep(@Param("did") int did);-->
<select id="getDeptByStep" resultMap="deptEmpStep">
  select * from t_dept where did = #{did}
</select>
<!--List<Emp> getEmpListByDid(@Param("did") int did);-->
<select id="getEmpListByDid" resultType="Emp">
	select * from t_emp where did = #{did}
</select>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值