Mybatis学习总结-04

Mybatis学习总结-04

学习的主要内容:

  • Mybatis 的缓存机制
  • Mybatis 的多表连接查询
  • Mybatis 中常用的注解

1、Mybatis 的缓存机制

1.1、什么是缓存

缓存是内存中开辟的一个区域, 用于存放一些数据(信息). 来提高数据的读取速度. 提高查询效率.

1.2、Mybatis 对缓存的支持
2.1、一级缓存

一级缓存是基于 SqlSession 对象做的缓存,同一个SqlSession对象中, 对同一个id的查询, MyBatis会进行数据的缓存. 一级缓存默认开启.

2.2、二级缓存

二级缓存是基于 SqlSessionFactory级别做的缓存. 二级缓存默认是关闭的, 如果要使用, 需要在指定的命名空间通过配置进行开启. 使用即可.

<!-- 开启二级缓存 -->
<!--
        eviction: 回收策略
                LRU: 最近最少使用
                FIFO: 先进先出
        flushInterval: 刷新间隔, 默认不刷新, 单位是毫秒
        readOnly: 是否是只读
                true: 表示只读
                false: 默认值, 表示可读可写, 要求实体类可序列化
        size: 记录数, 默认为1024
        type: 表示自定义缓存使用的全限定路径, 一般用于第三方缓存方案
-->
<cache
        eviction="LRU"
        flushInterval="60000"
        readOnly="false"
        size="1024" />

2、多表连接查询

2.1、<resultMap标签的使用

在MyBatis中, 查询标签有两个属性, resultType和resultMap. 都代表返回结果的类型. 区别在于:

  • resultType指定的一个类型, MyBatis会进行自动映射(Auto-Mapping). 列名和属性名一致则进行映射, 否则属性被赋值为null.
  • resultMap属性指定的是标签的id值. 表示MyBatis不进行自动映射, 需要程序员自己定义映射关系.

3、多表连接查询的几种情况

3.1、多对一查询

例一:(多个学生对一个老师)

实体类:

@Data
public class Student {
    private int id;
    private String name;
    private Teacher teacher;
}
@Data
public class Teacher {
    private int id;
    private String name;
}

进行连表查询:

<resultMap id="ManyStudentToOneTeacher" type="com.me.domain.Student">
    <result property="id" column="sid"/>
    <result property="name" column="sname"/>
    <association property="teacher" javaType="com.me.domain.Teacher">
        <result property="id" column="tid"/>
        <result property="name" column="tname"/>
    </association>
</resultMap>

<select id="selAll" resultMap="ManyStudentToOneTeacher">
    select
        s.*, t.name tname, t.id tid
    from
        tb_student s
    left join
        tb_teacher t
    on
        s.tid = t.id
</select>
3.2、一对多

例二:(一个学生对多个老师)

实体类:

public class Student {
    private int id;
    private String name;
    private int tid;
}
@Data
public class Teacher {
    private int id;
    private String name;
    private List<Student> students;
}

进行连表查询:

 <select id="getTeacher" resultMap="teacherStu">
     select s.id sid ,s.name sname ,t.id tid ,t.name tname
     from student s, teacher t
     where s.tid = t.id and t.id =#{id}
</select>
<resultMap id="teacherStu" type="com.me.domain.Teacher">
    <result property="id" column="tid"/>
    <result property="name" column="tname"/>
    <collection property="students" ofType="com.me.domain.Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <result property="tid" column="tid"/>
    </collection>
</resultMap>

<select id="selAll" resultMap="cmap">
    select
        t.*, s.id sid, s.name sname
    from
        tb_teacher t
    left join
        tb_student s
    on
        s.tid = st.id
</select>

4、Mybatis 中的常用注解

  • @Param, 修饰方法的参数, 作用是将参数列表封装成Map集合形式
  • @Select
  • @Insert
  • @Update
  • @Delete
  • @Results
  • @Result
  • @One
  • @Many
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值