自定义映射resultMap解决一对多和多对一的问题

注意字段和属性的区别(一种约定好的规则):
字段 数据库表 用_隔开:emp_name
属性 实 体 类 用驼峰的方式隔开:empName

引入

创建实体类:

public class Emp {
    //员工表
   private Integer eid;
   private String empName;
   private Integer age;
   private String sex;
   private String email;
   private Dept dept;
}
public class Dept {
    //部门表
    private Integer did;
    private String deptName;
}

EmpMapper接口:

public interface EmpMapper {
    /**
     * 查询所有的员工信息
     */
    List<Emp> getAllEmp();
}

EmpMapper.xml:

<mapper namespace="com.hty.mapper.EmpMapper">
	<!--    List<Emp> getAllEmp();-->
    <select id="getAllEmp" resultType="Emp">
        select * from t_emp
    </select>
</mapper>

测试函数:
打印出所有员工信息

@Test
    public void test01() throws IOException {
        //加载核心配置文件
        InputStream is= Resources.getResourceAsStream("SqlMapConfig.xml");
        //创建SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        //通过核心配置文件所对应的字节输入流创建工厂类SqlSessionFactory,生产SqlSession对象
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
        //创建SqlSession对象,此时通过SqlSession对象所操作的sql都必须手动提交或回滚事务
        //SqlSession sqlSession = sqlSessionFactory.openSession();
        //创建SqlSession对象,此时通过SqlSession对象所操作的sql都会自动提交
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        //通过代理模式创建UserMapper接口的代理实现类对象
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> list = empMapper.getAllEmp();
        list.forEach(emp -> System.out.println(emp));

    }

运行截图
打印出所有员工信息,结果empName这个属性值都为空

原因数据库字段名和实体类属性名不一致,所以字段和属性没有映射(匹配)成功,因此empName查询出来为null
解决方案:(在不改数据库表属性的前提下)解决字段名和属性名不一致的情况

二、解决方案

解决字段名和属性名不一致的情况
第一种 为字段起别名,保持和属性名的一致

    <select id="getAllEmp" resultType="Emp">
            select eid,emp_name empName,age,sex,email from t_emp
    </select>

字段和属性匹配成功,成功输出empName
在这里插入图片描述

第二种 设置全局配置,将_自动映射为驼峰

在核心配置文件设置一个标签settings (注意配置文件各个标签的先后顺序)

    <!--设置Mybatis的全局配置    -->
    <settings>
    <!--将下划线自动映射为驼峰,但必须符合某些规则 emp_name:empName-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

第三种 通过resultMap自定义的映射关系

resultMap 自定义映射关系(属性和字段映射)
resultType 只能解决默认的映射关系,即:属性和字段一致的情况


resultMap属性
resultMap:设置自定义映射关系
id:唯一标识,不能重复
type:设置映射关系的实体类型
子标签
id:设置主键的映射关系
result:设置普通字段的映射关系
属性
property 设置映射关系中的属性名(必须是type属性所设置的实体类类型中的属性名)
column 设置映射关系中的字段名(必须是SQL语句查询出的字段名)

    <resultMap id="empResultMap" 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="empResultMap">
        select * from t_emp
    </select>

resultMap更多由于多对一和一对多映射的情况

三、ResultMap解决多对一映射方法

难点:Emp实体类中的dept无法和表中查询出来的字段进行映射(解决多对一映射问题)
解决方法一:通过级联属性赋值解决 最简单的方式

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

EmpMapper接口方法:

    /**
     * 查询员工以及员工所对应的部门信息
     */
    Emp getEmpAndDept(@Param("eid") Integer eid);

EmpMapper.xml:

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

测试函数:

        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        Emp emp = empMapper.getEmpAndDept(1);
        System.out.println(emp);

结果:Emp实体类中的Dept对象里的数据成功输出
在这里插入图片描述

解决方法二:使用association标签绝对多对一的映射关系

    <resultMap id="empAndDeptResultMapTwo" 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>
        <!--        
		        association:处理多对一的映射关系
		        property:需要处理多对的映射关系的属性名
		        JavaType:该属性的类型(实体类)
        -->
        <association property="dept" javaType="Dept">
            <id property="did" column="did"></id>
            <result property="deptName" column="dept_name"></result>
        </association>
    </resultMap>

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

在这里插入图片描述

解决方法三:通过分布查询

分两步查询:先查员工,再查部门
在DeptMapper接口中声明方法:

public interface DeptMapper {
    /**
     * 通过分布查询查询员工所对应的部门信息
     * 分布查询第二步:通过did查询员工所对应的部门信息
     */
    Dept getEmpAndDeptByStepTwo(@Param("did") Integer did);
}

DeptMapper.xml:

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

    </select>

EmpMapper.xml:

    <!--
        select:设置分布查询的sql的唯一标识(namespace.SQLid或mapper接口的全类名.方法名)
        column:设置分布查询的条件
    -->
    <resultMap id="empAndDeptByStepResultMap" 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>
        <association property="dept"
                     select="com.hty.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="did"></association>
    </resultMap>

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

测试函数:

	Emp emp = empMapper.getEmpAndDeptByStepOne(3);

运行截图:
在这里插入图片描述
延迟加载:
如果我们只访问员工的信息(名字),不访问所在部门信息

//通过代理模式创建UserMapper接口的代理实现类对象
EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
Emp emp = empMapper.getEmpAndDeptByStepOne(3);
System.out.println(emp.getEmpName());

核心配置文件:在seting标签里开启延迟加载

	<!--设置Mybatis的全局配置-->
    <settings>
    <!--将下划线自动映射为驼峰,但必须符合某些规则 emp_name:empName-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    <!--开启延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
    </settings>

开启延迟加载:
在这里插入图片描述
没开启延迟加载
在这里插入图片描述
开启延迟加载只有1个SQL语句,没开启延迟加载2个SQL语句
原理:如果没有去访问的话,不会去执行没用到的SQL(简单说就是按需执行)
如果只获取部门信息,也只输出查询部门的SQL语句:
在这里插入图片描述

<!--fetchType:当开启了全局的延迟加载后,可通过此属性手动控制延迟加载的效果
没有开启全局延迟加载,都是默认立即加载
fetchType="lazy|eager" lazy:延迟加载  eager:立即加载
--!>
    <resultMap id="empAndDeptByStepResultMap" 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>
        <association property="dept"
select="com.hty.mapper.DeptMapper.getEmpAndDeptByStepTwo"
                     column="did"
                    fetchType="eager">
        </association>
    </resultMap>

解决一对多映射

多对一对应对象,一对多对应集合!!!

使用collection标签: collection:处理一对多的映射关系 ofType:标识该属性所对应的结合中存储数据的的类型

在实体类Dept添加集合:
get set方法省略

public class Dept {
    //部门表
    private Integer did;
    private String deptName;
    private List<Emp> emps;
 }

在DeptMapper接口:

    /**
     * 获取部门以及部门中所有的员工信息
     */
    Dept getDeptAndEmp(@Param("did") Integer did);

DeptMapper.xml配置文件:

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

    <resultMap id="deptAndEmpResultMap" type="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
        <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="sex" column="sex"></result>
            <result property="email" column="email"></result>
        </collection>
    </resultMap>

测试函数

 DeptMapper mapper = sqlSession.getMapper(DeptMapper.class);
Dept dept = mapper.getDeptAndEmp(1);
System.out.println(dept);

使用分布查询:
先查询部门信息,在查询员工信息
DeptMapper接口:

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

EmpMapper接口:

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

DeptMapper.xml 第一部 先查询部门id

<!--    Dept getDeptAndEmpByStepOne(@Param("did") Integer did);-->
    <select id="getDeptAndEmpByStepOne" resultMap="deptAndEmpByStepResultMap">
        select * from t_dept where did = #{did}
    </select>
    
    <resultMap id="deptAndEmpByStepResultMap" type="Dept">
        <id property="did" column="did"></id>
        <result property="deptName" column="dept_name"></result>
        <collection property="emps"
                    select="com.hty.mapper.EmpMapper.getDeptAndEmpByStepTwo"
                    column="did">

        </collection>
    </resultMap>

EmpMapper.xml 第二步:再根据部门id查询部门所有员工

<!--List<Emp> getDeptAndEmpByStepTwo(@Param("did") Integer did);-->

    <select id="getDeptAndEmpByStepTwo" resultType="Emp">
        select * from t_emp where did=#{did}
    </select>

在这里插入图片描述

总结:

1.ResultMap解决多对一映射方法
解决方法一:通过级联属性赋值解决 最简单的方式
解决方法二:使用association标签绝对多对一的映射关系
解决方法三:通过分布查询
解决一对多方法:
解决方法一:通过级联属性赋值解决 最简单的方式
解决方法二:使用collection标签绝对多对一的映射关系
解决方法三:通过分布查询

2.多对一对应对象,一对多对应集合!!!

使用association标签
association:处理多对一的映射关系
property:需要处理多对的映射关系的属性名
JavaType:该属性的类型(实体类)
使用collection标签: collection:处理一对多的映射关系 ofType:标识该属性所对应的结合中存储数据的的类型
注意区别JavaType和ofType

4.使用分布查询的好处:
实现解耦,避免单一职能;
大大加速了查询的效率,避免了多表连接查询时的低效率,对系统开销很大

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值