Mybatis中的ResultMap介绍和一级缓存和二级缓存的介绍

ResultMap介绍(重点!!!)

ResultMap是在程序开发中经常使用到的一种返回值类型当开发人员不知道如何进行结果集映射时可以
考虑使用 ResultMap
Mybatis官方文档中是这样描述的:
resultMap 元素是 MyBatis 中最重要最强大的元素。它可以让你从 90% 的 JDBC ResultSets
数据提取代码中解放出来,并在一些情形下允许你进行一些 JDBC 不支持的操作。实际上,在为一些比如连接
的复杂语句编写映射代码的时候,一份 resultMap 能够代替实现同等功能的数千行代码。ResultMap 的设
计思想是,对简单的语句做到零配置,对于复杂一点的语句,只需要描述语句之间的关系就行了。

具体用法:

注意:

  1. select中的 resultMap 属性值要和 resultMap 标签中的 id 属性值保持一致
  2. column 表示要映射的 表中的字段名
  3. property 表示要映射的 java的属性名
     <!-- 编写查询语句   -->
<select id="findByResultMap" resultMap="resultRM_User">
    select id user_id,name user_name,age user_age,sex user_sex from user
</select>
     <!--手动配置映射规则 -->
<resultMap id="resultRM_User" type="com.atguigu.pojo.User">
<!--1.主键必须映射-->
    <id column="user_id" property="id"></id>
    <!--2.剩余字段-->
    <result column="user_name" property="name"/>
    <result column="user_age" property="age"/>
    <result column="user_sex" property="sex"/>
</resultMap>

驼峰映射规则

在程序开发过程中,经常会出现 java中的属性用到的是 驼峰命名规则 而 数据库中的表用的是 下划线命名规则 为了在执行SQL语句的过程中
将   字段名  和   属性名   进行 映射 Mybatis为我们提供了一个属性为 mapUnderscoreToCamelCase 默认值
false 默认是不开启的 将其设置为 true 则表示开启



在mybatis的配置文件中配置

<settings>
    <!--开启驼峰命名规则-->
    <setting mapUnderscoreToCamelCase="true"></setting>
</settings>

Mybatis查询中的注解模式(非重点)

Mybatis 为我们提供了用于进行SQL语句的4个注解分别用于对应增删改查操作

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Insert("SQL语句")
public void method insetUser(){
        ...
        }
@Delete("SQL语句")
public void method deleteUser(){
        ...
        }
@Update("SQL语句")
public void method updateUser(){
        ...
        }
@Select("SQL语句")
public void method selectUser(){
        ...
        }

多表查询

  • 一对一
  • 一对多
  • 多对多

一对一封装

标签:

association

属性:

  1. property java的映射属性 (实体类中的属性)
  2. javaType 返回的java类型
  3. autoMapping 自动映射属性。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.mapper.DeptUserMapper">


    <select id="findAll" resultMap="findDeptRM">
        SELECT dept_user.*,dept_name from dept_user,dept
        where dept_user.dept_id = dept.dept_id
    </select>

    <resultMap id="findDeptRM" type="com.atguigu.pojo.DeptUser" autoMapping="true">
        <!--主键id-->
        <id column="id" property="id"/>
        <!--其它属性-->
        <!--<result column="name" property="name"/>
        <result column="age" property="age"/>-->

        <!--一对一封装-->
        <association property="dept" javaType="com.atguigu.pojo.Dept" autoMapping="true">
            <id column="dept_id" property="deptId"/>
            <!--<result column="dept_name" property="deptName"/>-->
        </association>
    </resultMap>
</mapper>

一对多封装

标签:

collection

属性:

  1. property java的映射属性 (实体类中的属性)
  2. ofType 返回的java类型
  3. autoMapping 自动映射属性。
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.mapper.DeptMapper">


    <select id="findAll" resultMap="findDeptRM">
       <!-- select dept.dept_name,dept_user.* from dept,dept_user
        where  dept.dept_id = dept_user.dept_id -->
        select d.dept_id, d.dept_name,u.id,u.name,u.age from dept d LEFT JOIN dept_user u
                                                     on d.dept_id = u.dept_id
    </select>

    <resultMap id="findDeptRM" type="com.atguigu.pojo.Dept" autoMapping="true">
        <!--主键必须写 看懂业务主键-->
        <id column="dept_id" property="deptId"></id>
        <!--<result column="dept_name" property="deptName"></result>-->

        <!--一对多-->
        <collection property="users" ofType="com.atguigu.pojo.DeptUser" autoMapping="true">
            <id column="id" property="id"/>
            <!--<result column="name" property="name"/>-->
        </collection>
    </resultMap>
</mapper>

子查询

应用场景: 在海量数据查询的情况下,通常使用子查询会有更高的效率。 但如果数据量比较小的情况下 子查询反而会影响查询效率。

<!--
        column="dept_id" 子查询时 依赖的数据字段
        select="子查询的关键属性"
    -->
<resultMap id="deptRM" type="com.atguigu.pojo.Dept">
    <id column="dept_id" property="deptId"/>
    <result column="dept_name" property="deptName"/>
    <collection property="users" ofType="com.atguigu.pojo.DeptUser"
                select="findDeptUserByDeptId" column="dept_id"></collection>
</resultMap>

<select id="findDeptUserByDeptId" resultType="com.atguigu.pojo.DeptUser">
    select * from dept_user where dept_id = #{dept_id}
</select>

懒加载机制

Mybatis提供了懒加载机制 配合 子查询一起使用 这样会方便 查询

 <!--
        column="dept_id" 子查询时 依赖的数据字段
        select="子查询的关键属性"
        fetchType="lazy" 子查询懒加载  用户使用时 才查询数据库
        fetchType="eager" 查询所有的数据  -->
    <resultMap id="deptRM" type="com.atguigu.pojo.Dept">
        <id column="dept_id" property="deptId"/>
        <result column="dept_name" property="deptName"/>
        <collection property="users" ofType="com.atguigu.pojo.DeptUser"
                    select="findDeptUserByDeptId" column="dept_id" fetchType="lazy"></collection>
    </resultMap>

    <select id="findDeptUserByDeptId" resultType="com.atguigu.pojo.DeptUser">
        select * from dept_user where dept_id = #{dept_id}
    </select>

缓存机制

一级缓存(重点)

Mybatis 中默认的缓存机制 同一个SqlSession中数据共享

二级缓存(几乎不用)

需要 手动开启 在映射文件中添加 <cache></cache> 标签
但是需要注意的是 在手动开启时 缓存时 在查询完毕后 需要手动的关闭连接。

Mybatis中二级缓存的注解写法

@CacheNamespace 标识Mapper接口 标识开启二级缓存

注意事项:

  • 注解和映射文件的二级缓存不能同时使用
  • 如果使用注解模式 则必须配合@Select注解查询
  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用MyBatis实现三表联合查询可以通过编写SQL语句来实现。首先,在Mapper文件定义一个查询语句,使用JOIN语句将三个表连接起来,并指定连接条件。然后,在Java代码调用该查询语句,将结果映射到对应的实体类。 以下是一个示例的Mapper文件配置: ```xml <!-- 定义查询语句 --> <select id="getThreeTableData" resultMap="resultMap"> SELECT t1.*, t2.*, t3.* FROM table1 t1 JOIN table2 t2 ON t1.id = t2.table1_id JOIN table3 t3 ON t2.id = t3.table2_id WHERE t1.id = #{id} </select> <!-- 定义结果映射 --> <resultMap id="resultMap" type="com.example.entity.ThreeTableEntity"> <!-- 定义字段映射 --> <result property="field1" column="t1_field1"/> <result property="field2" column="t1_field2"/> <!-- 省略其他字段映射 --> <association property="table2" javaType="com.example.entity.Table2Entity"> <result property="field3" column="t2_field3"/> <!-- 省略其他字段映射 --> <association property="table3" javaType="com.example.entity.Table3Entity"> <result property="field4" column="t3_field4"/> <!-- 省略其他字段映射 --> </association> </association> </resultMap> ``` 在Java代码调用该查询语句: ```java public ThreeTableEntity getThreeTableData(int id) { return sqlSession.selectOne("com.example.mapper.ThreeTableMapper.getThreeTableData", id); } ``` 关于MyBatis一级缓存二级缓存的配置,一级缓存是默认开启的,它是指在同一个SqlSession,对于相同的查询语句和参数,MyBatis会将查询结果缓存起来,下次再执行相同的查询时,直接从缓存获取结果,提高查询性能。 而二级缓存是在多个SqlSession之间共享的缓存,需要手动进行配置。可以在MyBatis的配置文件添加以下配置: ```xml <!-- 开启二级缓存 --> <settings> <setting name="cacheEnabled" value="true"/> </settings> <!-- 配置二级缓存 --> <cache type="org.mybatis.caches.ehcache.EhcacheCache"/> ``` 需要注意的是,使用二级缓存时,需要确保查询的结果是可序列化的,并且实体类需要实现Serializable接口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值