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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Java编程swing组件JLabel详解以及使用示例

    Java编程swing组件JLabel详解以及使用示例

    这篇文章主要介绍了Java编程swing组件JLabel详解以及使用示例,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01
  • 使用java -jar修改SpringBoot中application.properties的配置项

    使用java -jar修改SpringBoot中application.properties的配置项

    这篇文章主要介绍了使用java -jar修改SpringBoot中application.properties的配置项问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-02-02
  • SpringBoot中配置双数据源的实现示例

    SpringBoot中配置双数据源的实现示例

    在许多应用程序中,可能会遇到需要连接多个数据库的情况,本文主要介绍了SpringBoot中配置双数据源的实现示例,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-08-08
  • SpringMVC请求、响应和拦截器的使用实例详解

    SpringMVC请求、响应和拦截器的使用实例详解

    拦截器(Interceptor) 它是一个Spring组件,并由Spring容器管理,并不依赖Tomcat等容器,是可以单独使用的,这篇文章给大家介绍SpringMVC请求、响应和拦截器的使用,感兴趣的朋友一起看看吧
    2024-03-03
  • hibernate 常用方法介绍

    hibernate 常用方法介绍

    这篇文章介绍了hibernate的常用方法,有需要的朋友可以参考一下
    2013-09-09
  • 详解Java中Collector接口的组成

    详解Java中Collector接口的组成

    今天给大家带来的是关于Java基础的相关知识,文章围绕着Collector接口的组成展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下
    2021-06-06
  • springboot2如何集成ElasticSearch6.4.3

    springboot2如何集成ElasticSearch6.4.3

    这篇文章主要介绍了springboot2如何集成ElasticSearch6.4.3问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-07-07
  • 基于springMvc+hibernate的web application的构建

    基于springMvc+hibernate的web application的构建

    下面小编就为大家带来一篇基于springMvc+hibernate的web application的构建。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • 关于Java的二叉树、红黑树、B+树详解

    关于Java的二叉树、红黑树、B+树详解

    这篇文章主要介绍了关于Java的二叉树、红黑树、B+树详解,能同时具备数组查找快的优点以及链表插入和删除快的优点的数据结构就是树,需要的朋友可以参考下
    2023-05-05
  • springboot参数传中文乱码的解决方案

    springboot参数传中文乱码的解决方案

    这篇文章主要介绍了springboot参数传中文乱码的解决方案,帮助大家更好的理解和学习使用springboot,感兴趣的朋友可以了解下
    2021-03-03

最新评论