广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Java自定义映射resultMap定义及用法
  • 534
分享到

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

Java自定义映射resultMapJavaresultMap 2022-11-13 19:11:32 534人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录搭建mybatis框架一、resultMap处理字段和属性的映射关系二、多对一映射处理三、一对多映射处理搭建Mybatis框架 ①引入相关的依赖 ②一些配置文件给复制到我们的re

搭建Mybatis框架

①引入相关的依赖

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

例如:jdbc.properties;log4j.xml

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

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

⑤创建数据库的表

t_emp

t_dept

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

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

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

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

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

①mapper接口:

    
    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.分布查询

    
    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接口中添加方法

    
    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接口

    
    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接口

    
    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接口

    
    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接口


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

--结束END--

本文标题: Java自定义映射resultMap定义及用法

本文链接: https://www.lsjlt.com/news/171065.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作