Mybatis-自定义映射resultMap01

  解决字段名和属性不一致的情况:

  数据库表单:

  

 

 

   java中对应类:

  

 

 

   其中empName与emp_name不一致

    1.为字段取别名,保持和属性名一致

      案例:
    接口方法:

/**
     * 查询所有员工信息
     */
    List<Emp> SelectAllEmp();

    mapper文件:

<!--  List<Emp> SelectAllEmp();
        给表中属性取别名,与类的属性一致,就可以解决属性不相符,类的属性数据为空
    -->
    <select id="SelectAllEmp" resultType="emp">
        select eid,emp_name as empName,age,sex,email from t_emp
    </select>

    2.设置全局配置,将_自动映射为驼峰

    在mybatis主配置文件中添加全局变量:

<!--设置Mybatis的全局配置-->
    <settings>
        <!--
        将下划线_自动映射为驼峰,规则为将下划线后面的第一个单词首字母大写即可
            比如:emp_name====>empName
        -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    mapper文件:

 <select id="SelectAllEmp" resultType="emp">
        select *from t_emp
    </select>

    3.通过resultMap解决字段名和属性名的映射关系

      只需要在mapper文件中进行resultMap配置即可      

<!--
    resultMap:设置自定义映射关系
    id:唯一标识,不能重复
    type:设置映射关系中的实体类类型
-->
    <resultMap id="ResultMap" type="com.hrf.domain.Emp">
        <!--
            子标签:
                id:设置主键的映射关系
                result:设置普通字段的映射关系
                属性:
                    property:设置映射关系中的属性名,必须是type属性所设置的实体类类型中的属性(就是实体类中的属性)
                    column:设置映射关系中的字段名,必须是sql语句查询出来的字段名(数据库表中字段名)
        -->
        <id column="eid" property="eid"/>
        <result column="emp_name" property="empName"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
        <result column="email" property="email"/>
    </resultMap>
    <select id="SelectAllEmp" resultMap="ResultMap">
        select *from t_emp
    </select>

  处理数据库表多对一映射关系

    1.级联方式处理映射关系

      创建的实体类: 

            部门 

      

 

 

             员工:

      

 

 

   接口方法:

/**
     * 查询员工信息和对应的部门信息
     */
    Emp SelectEmpAngDept(int eid);

  mapper文件:

   <!--Emp SelectEmpAngDept(int eid);-->
    <resultMap id="ResultEmpAndDeptOne" type="Emp">
        <id column="eid" property="eid"/>
       <result column="emp_name" property="empName"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
        <result column="email" property="email"/>
        <result column="did" property="dept.did"/>
        <result column="dept_name" property="dept.deptName"/>
    </resultMap>
    <select id="SelectEmpAngDept" resultMap="ResultEmpAndDeptOne">
        select emp.*,dept.* from t_emp emp left join t_dept dept on emp.did = dept.did where emp.eid = 1   
    </select>

  测试方法:

   @Test
    public void ResultEmpAndDeptOne(){
        SqlSession session = MybatisUtils.getSqlSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        Emp emp = mapper.SelectEmpAngDept(1);
        System.out.println("员工信息和部门信息"+emp);
        session.close();
    }

    2.使用association处理映射关系

    在resultMap标签中设置association标签

      id标签就是设置主键映射 column是表中字段名,property是类型属性

      result设置非主键映射   column是表中字段名,property是类型属性

 <resultMap id="ResultEmpAndDeptOne" type="Emp">
        <id column="eid" property="eid"/>
        <result column="emp_name" property="empName"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
        <result column="email" property="email"/>
        <result column="did" property="dept.did"/>
<!--
        association:处理多对一的映射关系
        property:需要处理多对一的映射关系的属性名(此时设置的就是emp表中dept属性)
        javaType:该属性的类型
-->
        <association property="dept" javaType="Dept">
            <id column="did" property="did"/>
            <result column="dept_name" property="deptName"/>
        </association>
    </resultMap>
    <select id="SelectEmpAngDept" resultMap="ResultEmpAndDeptOne">
        select emp.*,dept.* from t_emp emp left join t_dept dept on emp.did = dept.did where emp.eid = 1
    </select>

      3.通过分步查询处理多对一的映射关系

      创建两条sql语句 ,将其中一条语句查询出来的结果通过resultMap的association标签映射给类型属性

        第一条sql语句:

<!--  Dept SelectDept(@Param("did") int did);  -->
    <select id="SelectDept" resultType="dept">
        select * from t_dept where did = #{did}
    </select>

       emp类型属性说明:

      

   第二条sql语句:

    <resultMap id="ResultEmpAndDeptOne" type="Emp">
        <id column="eid" property="eid"/>
        <result column="emp_name" property="empName"/>
        <result column="age" property="age"/>
        <result column="sex" property="sex"/>
        <result column="email" property="email"/>
        <result column="did" property="dept.did"/>
        <association property="dept"
                    select="com.hrf.dao.DeptMapper.SelectDept"
                     column="did"></association>
    </resultMap>
    <select id="SelectEmpAngDept" resultMap="ResultEmpAndDeptOne">
        select * from t_emp  where eid = #{eid}
    </select>

    通过测试类方法观察结果

 

   可以看到执行了两条sql语句

posted @ 2022-03-01 18:25  Soleili  阅读(53)  评论(0编辑  收藏  举报