Java自定义映射resultMap定义及用法

 更新时间:2022年11月09日 09:37:48   作者:学习使我快乐T  
MyBatis的每一个查询映射的返回类型都是ResultMap,当我们提供返回类型属性是resultType时,MyBatis会自动给我们把对应值赋给resultType所指定对象的属性,当我们提供返回类型是resultMap时,将数据库中列数据复制到对象的相应属性上,可以用于复制查询,两者不能同时用

搭建Mybatis框架

①引入相关的依赖

②一些配置文件给复制到我们的resources文件夹下

例如:jdbc.properties;log4j.xml

③创建mybatis-config核心配置文件

④创建utils包和pojo包和mapper包以及映射文件所对应的包

⑤创建数据库的表

t_emp

t_dept

⑥在pojo创建数据库对应的实体类

也就是创建属性,getset方法和构造器,还有重写toString方法

⑦mapper包下创建接口,看对表有什么操作需求

⑧在配置文件中创建mybatis-mapper,对应mapper接口

一、resultMap处理字段和属性的映射关系

①mapper接口:

    /**
     * 根据id查询员工信息
     * @param empId
     * @return
     */
    Emp getEmpByeEmpId(@Param("empId") Integer empId);

②mapper映射文件:

    <!--    Emp getEmpByEmpId(@Param("empId") Integer empId);-->
    <select id="getEmpByEmpId" resultMap="empResultMap">
        select * from t_emp where emp_id = #{empId}
    </select>

③ 测试类

    @Test
    public void testGetEmpByEmpId() {
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = mapper.getEmpByEmpId(1);
        System.out.println(emp);
    }

结果:

由于字段名和属性名不一致,而且没有创建映射关系,java中是驼峰的命名方式,而我们mysql中是下划线的命名方式,所以这时,我们就需要一些操作来将它们进行对应

Emp{empId=null,empName='null',age=20,gender='男'}

字段名和属性名不一致的情况,如何处理映射关系

1.为查询的字段设置别名,和属性名保持一致

2.当字段符合MySQL的要求使用_,而属性符合java的要求使用驼峰

此时可以在Mybatis的核心配置文件中设置一个全局配置,可以自动将下划线映射为驼峰

emp_id--》empId ,emp_name--》empName

3.使用resultMap自定义映射处理

方式一:起别名

select emp_id empId,emp_name empName,age,gender from t_emp where emp_id = #{empId}

方式二: 使用标签进行配置

    <settings>
        <!--将下划线映射为驼峰-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <select id="getEmpByEmpId" resultType="Emp">
        select * from t_emp where emp_id = #{empId}
    </select>

方式三:设置自定义映射

resultMap:设置自定义映射

    resultMap:设置自定义映射
    属性:
    id:表示自定义映射的唯一标识
    type:查询的数据要映射的实体类的类型
    子标签:
    id:设置主键的映射关系
    result:设置普通字段的映射关系
    association:设置多对一的映射关系(处理集合类型的属性)
    collection:设置一对多的映射关系(处理集合类型的属性)
    属性:
    property:设置映射关系中实体类中的属性名,必须是处理的实体类类型中的属性名
    column:设置映射关系中表中的字段名,必须是sql查询出的某个字段

    <resultMap id="empResultMap" type="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result>
    </resultMap>
    <!--    Emp getEmpByEmpId(@Param("empId") Integer empId);-->
    <select id="getEmpByEmpId" resultMap="empResultMap">
        select * from t_emp where emp_id = #{empId}
    </select>

二、多对一映射处理

处理多对一的映射关系:

1.级联方式处理

2.association :处理多对一的映射关系(处理实体类类型的属性)

property:设置需要处理映射关系的属性和属性名

javaType:设置要处理的属性的类型

3.分布查询

    /**
     * 获取员工以及对应的部门信息
     * @param empId
     * @return
     */
    Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);

方式一:级联方式处理

    <resultMap id="empAndDeptResultMap" type="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result>
        <result column="dept_id" property="dept.deptId"></result>
        <result column="dept_name" property="dept.deptName"></result>
    </resultMap>
<!--    Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);-->
    <select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap">
        select
        t_emp.*,t_dept.*
        from t_emp
        left join t_dept
        on t_emp.dept_id = t_dept.dept_id
        where t_emp.emp_id = #{empId}
    </select>

方式二:使用association处理映射关系

    <resultMap id="empAndDeptResultMap" type="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result>
        <association property="dept" javaType="Dept">
            <id column="dept_id" property="deptId"></id>
            <result column="dept_name" property="deptName"></result>
        </association>
    </resultMap>
<!--    Emp getEmpAndDeptByEmpId(@Param("empId") Integer empId);-->
    <select id="getEmpAndDeptByEmpId" resultMap="empAndDeptResultMap">
        select
        t_emp.*,t_dept.*
        from t_emp
        left join t_dept
        on t_emp.dept_id = t_dept.dept_id
        where t_emp.emp_id = #{empId}
    </select>

测试类:

    @Test
    public void testGetEmpAndDeptByEmpId(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = mapper.getEmpAndDeptByEmpId(2);
        System.out.println(emp);
        sqlSession.close();
    }

方式三:分布查询

EmpMapper接口中添加方法

    /**
     * 通过分布查询查询员工以及所对应的部门信息的第一步
     * @param empId
     * @return
     */
    Emp getEmpAndDeptByStep(@Param("empId") Integer empId);

EmpMapper映射配置文件中添加操作

property:设置需要处理映射关系的属性和属性名

select:设置分布查询的sql的唯一标识

column:将查询出的某个字段作为分布查询的sql的条件

fetchType:在开启了延迟加载的环境中,通过该属性设置当前的分布查询是否使用延迟加载

fetchType="eager(立即加载)\lazy(延迟加载)"

    <resultMap id="empAndDeptByStepResultMap" type="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result>
        <association property="dept"
                     select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="dept_id">
        </association>
    </resultMap>
<!--    Emp getEmpAndDeptByStep(@Param("empId") Integer empId);-->
    <select id="getEmpAndDeptByStepOne" resultMap="empAndDeptByStepResultMap">
        select * from t_emp where emp_id = #{empId}
    </select>

因为需要两个sql来查,所以我们把DeptMapper接口和对应的映射文件创建出来

DeptMapper接口

    /**
     * 通过分布查询查询员工以及所对应的部门信息的第二步
     * @param deptId
     * @return
     */
    Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);

DeptMapper映射文件

<!--    Dept getEmpAndDeptByStepTwo(@Param("deptId") Integer deptId);-->
    <select id="getEmpAndDeptByStepTwo" resultType="Dept">
        select * from t_dept where dept_id = #{deptId}
    </select>

分步查询的优点:可以实现延迟加载

但是必须在核心配置文件中设置全局配置信息:

lazyLoadingEnabled:延迟加载的全局开关。当开启时,所有关联对象都会延迟加载

aggressiveLazyLoading:当开启时,任何方法的调用都会加载该对象的所有属性。否则,每个属性会按需加载

此时就可以实现按需加载,获取的数据是什么,就只会执行相应的sql。此时可通过association和collection中的fetchType属性设置当前的分步查询是否使用延迟加载, fetchType="lazy(延迟加 载)|eager(立即加载)"

引入,延迟加载:

<!--开启延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--按需加载-->
<setting name="aggressiveLazyLoading" value="false"/>

因为在配置文件配置是全局加载,所以是针对于所有的分布查询,如果我们要想让某一个分布查询来实现一个完整的加载,这个时候就需要去association标签,也就是实现分布查询的地方,添加一个属性fatchType。

三、一对多映射处理

处理一对多的映射关系:

1.collection

2.分布查询

方式一:collection

例如一个部门对应多个员工

private List<Emp> emps;

①DeptMapper接口

    /**
     * 查询部门以及部门中的员工信息
     * @param deptId
     * @return
     */
    Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);

②DeptMapper映射配置文件

collection:用于处理一对多映射关系

ofType:设置集合类型的属性中存储的数据的类型

    <resultMap id="deptAndEmpResultMap" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
        <collection property="emps" ofType="Emp">
            <id column="emp_id" property="empId"></id>
            <result column="emp_name" property="empName"></result>
            <result column="age" property="age"></result>
            <result column="gender" property="gender"></result>
        </collection>
    </resultMap>
<!--    Dept getDeptAndEmpByDeptId(@Param("deptId") Integer deptId);-->
    <select id="getDeptAndEmpByDeptId" resultMap="deptAndEmpResultMap">
        select *
        from t_dept
        left join t_emp
        on t_dept.dept_id = t_emp.dept_id
        where t_dept.dept_id = #{deptId}
    </select>

③测试类:

    @Test
    public void testGetDeptAndEmpByDeptId(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        Dept dept = mapper.getDeptAndEmpByDeptId(1);
        System.out.println(dept);
        sqlSession.close();
    }

方式二:分布查询

DeptMapper接口

    /**
     * 通过分布查询查询部门以及部门中的员工信息的第一步
     * @param deptId
     * @return
     */
    Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);

DeptMapper映射文件

    <resultMap id="deptAndEmpResultMapByStep" type="Dept">
        <id column="dept_id" property="deptId"></id>
        <result column="dept_name" property="deptName"></result>
        <collection property="emps"
                    select=""
                    column="">
        </collection>
    </resultMap>
<!--    Dept getDeptAndEmpByStepOne(@Param("deptId") Integer deptId);-->
    <select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpResultMapByStep">
        select * from t_dept where dept_id = #{deptId}
    </select>

EmpMapper接口

/**
 * 通过分布查询查询部门以及部门中的员工信息的第二步
 * @param deptId
 * @return
 */
List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);

EmpMapper映射文件

<!--    List<Emp> getDeptAndEmpByStepTwo(@Param("deptId") Integer deptId);-->
    <select id="getDeptAndEmpByStepTwo" resultType="Emp">
        select * from t_emp where dept_id = #{deptId}
    </select>

然后把EmpMapper写的引入到DeptMapper中

    <resultMap id="empAndDeptByStepResultMap" type="Emp">
        <id column="emp_id" property="empId"></id>
        <result column="emp_name" property="empName"></result>
        <result column="age" property="age"></result>
        <result column="gender" property="gender"></result>
        <association property="dept" fetchType="eager"
                     select="com.atguigu.mybatis.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="dept_id">
        </association>
    </resultMap>

测试类:

    @Test
    public void testGetDeptAndEmpByStep(){
        SqlSession sqlSession = SqlSessionUtil.getSqlSession();
        DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
        Dept dept = mapper.getDeptAndEmpByStepOne(1);
//        System.out.println(dept);
        System.out.println(dept.getDeptName());
        sqlSession.close();
    }

到此这篇关于Java自定义映射resultMap使用方法详解的文章就介绍到这了,更多相关Java resultMap内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • springboot远程debug调试全过程

    springboot远程debug调试全过程

    这篇文章主要介绍了springboot远程debug调试全过程,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • Mybatis Plus 大数据游标分页的实现

    Mybatis Plus 大数据游标分页的实现

    使用MyBatis Plus的游标分页,我们可以轻松应对大数据量的场景,本文主要介绍了Mybatis Plus 大数据游标分页的实现,具有一定的参考价值,感兴趣的可以了解一下
    2024-07-07
  • SSM框架整合之junit测试的方法

    SSM框架整合之junit测试的方法

    本篇文章主要介绍了SSM框架整合之junit测试的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • 什么是递归?用Java写一个简单的递归程序

    什么是递归?用Java写一个简单的递归程序

    这篇文章主要介绍了什么是递归?用Java写一个简单的递归程序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • 详解Java的Hibernat框架中的Map映射与SortedMap映射

    详解Java的Hibernat框架中的Map映射与SortedMap映射

    这篇文章主要介绍了Java的Hibernat框架中的Map映射与SortedMap映射,Hibernat是Java的SSH三大web开发框架之一,需要的朋友可以参考下
    2015-12-12
  • 利用java实现二叉搜索树

    利用java实现二叉搜索树

    这篇文章主要介绍了利用java实现二叉搜索树,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下
    2021-04-04
  • java实现的汉字转五笔功能实例

    java实现的汉字转五笔功能实例

    这篇文章主要介绍了java实现的汉字转五笔功能,结合具体实例形式分析了java基于字符串遍历与编码转换等操作实现五笔编码获取的相关操作技巧,需要的朋友可以参考下
    2017-06-06
  • springboot以FTP方式上传文件到远程服务器

    springboot以FTP方式上传文件到远程服务器

    这篇文章主要介绍了springboot以FTP方式上传文件到远程服务器,需要的朋友可以参考下
    2019-12-12
  • JAVA Spring中让人头痛的JAVA大事务问题要如何解决你知道吗

    JAVA Spring中让人头痛的JAVA大事务问题要如何解决你知道吗

    这篇文章主要介绍了Java Spring事务使用及验证过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2021-09-09
  • SpringBoot中Token登录授权、续期和主动终止的方案流程分析

    SpringBoot中Token登录授权、续期和主动终止的方案流程分析

    SpringBoot项目中,基于Token的登录授权方案主要有两种:利用Session/Cookie和JWT,Cookie/Session方案有状态,不适合分布式架构,而JWT虽无状态,但存在过期时间不可强制失效、一次性等缺点,本文介绍SpringBoot中Token登录授权、续期和主动终止的方案,感兴趣的朋友一起看看吧
    2024-09-09

最新评论