MyBatis自定义映射resultMap

目录

一:字段名与属性名不一致

 方法一

方法二

方法三

二:处理多对一映射关系

1. 级联属性赋值(用的不多,虽然简单)

2.Association

3.分步查询

三:处理一对多映射关系

1.collection

2.分步查询


一:字段名与属性名不一致

 方法一

 通过字段别名解决字段名与属性名不一致的情况

字段名和属性名不一致的时候,会导致该字段的值无法查出,即显示null


解决方法:

        <!--List<Emp> getAllEmp();-->
        <select id="getAllEmp" resultType="Emp">
            select eid,emp_name empName ,age,sex,email,did from t_emp
        </select>

 

方法二

   设置MyBatis全局配置

    <!--设置MyBatis全局配置-->
    <settings>
        <!--将下划线自动映射成驼峰 emp_name:empName-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

方法三

自定义映射:resultMap

结构如下:

        <resultMap id="getAllEmpMap" type="Emp">
            <id property="" column=""></id>
            <result property="" column=""></result>
            <result property="" column=""></result>
            <result property="" column=""></result>
            <result property="" column=""></result>
        </resultMap>

resultMap:标签中的id为之后在select标签中resultMap的值

        type:要处理的映射关系的实体类类型

           id:主键的映射关系

     result:普通字段的映射关系

 property:设置映射关系属性名,必须为type属性所设置的实体类类型中的属性名

 column:设置映射关系字段名,必须为sql语句中查询的字段名

​         //标签中的id为下方select标签中resultMap的值
         //id标识主键映射关系
         //result标识普通字段
         //property标识属性名
         //column标识字段名
        <resultMap id="getAllEmpMap" 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="email" column="email"></result>
        </resultMap>

        <!--List<Emp> getAllEmp();-->
        <select id="getAllEmp" resultMap="getAllEmpMap">
            select * from t_emp
        </select>

​

二:处理多对一映射关系

1. 级联属性赋值(用的不多,虽然简单)

在多方实体中加入一方实体:

在员工与部门的关系中,将部门实体加入员工实体中

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
    private int eid;
    private String empName;
    private int age;
    private String sex;
    private String email;
    private int did;
    private Dept dept;
}

通过resultMap设置自定义映射关系

将dept中的属性通过 类名.属性名的方式将属性名映射为字段名

<resultMap id="getEmpAndDeptOne" 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="email" column="email"></result>
    <result property="dept.did" column="did"></result>
    <result property="dept.deptName" column="dept_name"></result>
</resultMap>

最后通过左连接查出两表对应的关系

<!--Emp getEmpAndDept(int eid);-->
<select id="getEmpAndDept" resultMap="getEmpAndDeptOne">
    select * from t_emp left join t_dept on t_emp.did = t_dept.did where eid = #{eid}
</select>

2.Association

通过resultMap中的association标签处理多对一映射关系

  1. 将一方的实体类型加到多方实体中
  2. 将association标签中的 property属性设置为一方实体在多方实体中的属性名
    private Dept dept;
  3. 将javaType属性设置为property所对应属性的实体类型
  4. association中的 id 即为 javaType对应实体的主键 result即为普通字段
    <resultMap id="getEmpAndDeptTwo" 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="email" column="email"></result>
        <association property="dept" javaType="Dept">
            <id property="did" column="did"></id>
            <result property="deptName" column="dept_name"></result>
        </association>
    </resultMap>

   <!--Emp getEmpAndDept(int eid);-->
    <select id="getEmpAndDept" resultMap="getEmpAndDeptTwo">
        select * from t_emp left join t_dept on t_emp.did = t_dept.did where eid = #{eid}
    </select>

3.分步查询

1:先查询员工信息

    /**
     * 通过分步查询查询员工信息及部门号
     * 第一步:
     * 先通过员工号查找员工信息
     */
    Emp getEmpAndDeptByStepOne(int eid);

2:根据员工信息查询其对应部门

    /**
     * 通过分步查询查询员工信息及部门号
     * 第二步:
     * 根据员工信息查询其所对应部门
     */
    Dept getEmpAndDeptByStepTwo(int did);

3.设置resultMap自定义映射关系

  • association中的select设置分布查询的sql的唯一标识(mapper接口的全类名.方法名)即为第二步操作所返回的类型为property属性赋值,例子中 property属性值为dept,所以select查询出来的类型也应该是Dept
  • column设置的是分布查询的条件,即第二步查询时的参数。
    <resultMap id="getEmpAndDeptByStepOne" 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="email" column="email"></result>
        <association property="dept" select="com.Li.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo" column="did">
        </association>
    </resultMap>


    <!--Emp getEmpAndDeptByStepOne(int eid);-->
    <select id="getEmpAndDeptByStepOne" resultMap="getEmpAndDeptByStepOne">
        select * from t_emp where eid = #{eid}
    </select>

优点:延迟加载

三:处理一对多映射关系

1.collection

在一方实体中增加多方实体的集合对象,通过collection处理映射关系
private List<Emp> emps;
    <resultMap id="getDeptAndEmpMap" type="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
        <!--
            collection:处理一对多映射关系
            oftype:标识该属性所对应集合中存储的数据的类型
        -->
        <collection property="emps" ofType="Emp">
            <id property="eid" column="eid"></id>
            <result property="empName" column="emp_name"></result>
            <result property="age" column="age"></result>
            <result property="email" column="email"></result>
            <result property="sex" column="sex"></result>
        </collection>
    </resultMap>

    <!--Dept getDeptAndEmp(int did);-->
    <select id="getDeptAndEmp" resultMap="getDeptAndEmpMap">
        select * from t_dept left join t_emp on t_dept.did = t_emp.did where t_dept.did = #{did}
    </select>

2.分步查询

总体思路:

  1. 根据部门id查询部门信息
  2. 根据部门信息查询部门中所对应的员工

步骤:

1.根据部门id查询部门信息

   /**
     * 通过分步查询查询部门及其所有成员信息
     * 第一步:
     * 查询部门信息
     */
    Dept getDeptAndEmpByStepOne(int did);

2.根据部门信息查询部门对应员工

    /**
     * 通过分步查询查询部门及其所有成员信息
     * 第二步:
     * 根据部门信息查询成员信息
     */
    List<Emp> getDeptAndEmpByStepTwo(int did);

3.在对应的Mapper映射文件中创建映射关系及编写sql语句

  • 在EmpMapper中
  •     <!--List<Emp> getDeptAndEmpByStepTwo(int did);-->
        <select id="getDeptAndEmpByStepTwo" resultType="Emp">
            select * from t_emp where did = #{did}
        </select>
  • 在DeptMapper中
  • <resultMap id="getDeptAndEmpByStepMap" type="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
        <collection property="emps" select="com.Li.mybatis.mapper.EmpMapper.getDeptAndEmpByStepTwo" column="did"></collection>
        </resultMap>
    
    <!--Dept getDeptAndEmpByStepOne(int did);-->
    <select id="getDeptAndEmpByStepOne" resultMap="getDeptAndEmpByStepMap">
         select * from t_dept where did = #{did}
    </select>

    与association的分步查询基本一致:

  • collection中的select设置分布查询的sql的唯一标识(mapper接口的全类名.方法名)即为第二步操作所返回的类型为property属性赋值,例子中 property属性值为emps,所以select查询出来的类型也应该是Emp
  • column设置的是分布查询的条件,即第二步查询时的参数。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Blone丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值