动态SQL+缓存

动态SQL

什么是动态SQL:动态SQL就是指根据不同的条件生成不同的SQL语句

mapper类

List<Student> getStudent(Map map);

mapper.xml

where元素只会在至少有一个子元素的条件返回SQL子句的情况下才去插入"WHERE"子句。而且,若语句的开头为"AND“或"OR", where 元素也会将它们去除。

if

<mapper namespace="com.chao.mapper.StudentMapper">
    <select id="getStudent" resultType="Student" parameterType="map">
        select * from ts.student
        <where>
            <if test="tid != null">
                and tid = #{tid}
            </if>
            <if test="name != null">
                and name = #{name}
            </if>
        </where>
    </select>
</mapper>

choose

<select id="getStudent1" resultType="Student" parameterType="map">
    select * from ts.student
    <where>
        <choose>
            <when test="id != null" >
                id = #{id}
            </when>
            <when test="name != null">
                and name = #{name}
            </when>
            <otherwise>
                and tid = #{tid}
            </otherwise>
        </choose>
    </where>
</select>

set

set元素会动态前置SET关键字,同时也会删掉无关的逗号

if后面必须加逗号

<update id="updateStudent" parameterType="map">
    update ts.student
    <set>
        <if test="name != null">name=#{name},</if>
        <if test="tid != null">tid=#{tid},</if>
    </set>
    where id=#{id}
</update>

sql(脚本片段)

<sql id="if-tid-name">
    <if test="tid != null">
        and tid = #{tid}
    </if>
    <if test="name != null">
        and name = #{name}
    </if>
</sql>
<select id="getStudent" resultType="Student" parameterType="map">
    select * from ts.student
    <where>
        <include refid="if-tid-name"></include>
    </where>
</select>

测试

@Test
public void test1(){
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

    HashMap map = new HashMap();
    map.put("tid",1);
    map.put("name","张三");
    List<Student> studentList = mapper.getStudent(map);
    for (Student student : studentList) {
        System.out.println(student);
    }
    sqlSession.close();
}

    HashMap map = new HashMap();
    map.put("tid",1);
    map.put("name","张三");
    map.put("id",1);
    mapper.updateStudent(map);

缓存

  • MyBatis包含一个非常强大的查询缓存特性,它可以非常方便地定制和配置缓存。缓存可以极大的提升查询效率。

  • MyBatis系统中默认定义了两级缓存:一级缓存二级缓存

    • 默认情况下,只有一级缓存开启。(SqlSession级别的缓存,也称为本地缓存)
    • 二级缓存需要手动开启和配置,他是基于namespace级别的缓存。
    • 为了提高扩展性,MyBatis定义了缓存接口Cache。我们可以通过实现Cache接口来自定义二级缓存

缓存失效

一级缓存是SqlSession级别的缓存,是一直开启的,我们关闭不了它;

一级缓存失效情况:没有使用到当前的一级缓存,效果就是,还需要再向数据库中发起一次查询请求!

1、sqlSession不同

@Test
public void testQueryUserById(){
   SqlSession session = MybatisUtils.getSession();
   SqlSession session2 = MybatisUtils.getSession();
   UserMapper mapper = session.getMapper(UserMapper.class);
   UserMapper mapper2 = session2.getMapper(UserMapper.class);

   User user = mapper.queryUserById(1);
   System.out.println(user);
   User user2 = mapper2.queryUserById(1);
   System.out.println(user2);
   System.out.println(user==user2);

   session.close();
   session2.close();
}

观察结果:发现发送了两条SQL语句!

结论:每个sqlSession中的缓存相互独立

2、sqlSession相同,查询条件不同

@Test
public void testQueryUserById(){
   SqlSession session = MybatisUtils.getSession();
   UserMapper mapper = session.getMapper(UserMapper.class);
   UserMapper mapper2 = session.getMapper(UserMapper.class);

   User user = mapper.queryUserById(1);
   System.out.println(user);
   User user2 = mapper2.queryUserById(2);
   System.out.println(user2);
   System.out.println(user==user2);

   session.close();
}

观察结果:发现发送了两条SQL语句!很正常的理解

结论:当前缓存中,不存在这个数据

3、sqlSession相同,两次查询之间执行了增删改操作!

因为增删改操作可能会对当前数据产生影响

4、sqlSession相同,手动清除一级缓存

@Test
public void testQueryUserById(){
   SqlSession session = MybatisUtils.getSession();
   UserMapper mapper = session.getMapper(UserMapper.class);

   User user = mapper.queryUserById(1);
   System.out.println(user);

   session.clearCache();//手动清除缓存

   User user2 = mapper.queryUserById(1);
   System.out.println(user2);

   System.out.println(user==user2);

   session.close();
}

二级缓存

  • 二级缓存也叫全局缓存,一级缓存作用域太低了,所以诞生了二级缓存

  • 基于namespace级别的缓存,一个名称空间,对应一个二级缓存;

  • 工作机制

    • 一个会话查询一条数据,这个数据就会被放在当前会话的一级缓存中;
    • 如果当前会话关闭了,这个会话对应的一级缓存就没了;但是我们想要的是,会话关闭了,一级缓存中的数据被保存到二级缓存中;
    • 新的会话查询信息,就可以从二级缓存中获取内容;
    • 不同的mapper查出的数据会放在自己对应的缓存(map)中;

1、开启全局缓存 【mybatis-config.xml】

<setting name="cacheEnabled" value="true"/>

2、去每个mapper.xml中配置使用二级缓存,这个配置非常简单;【xxxMapper.xml】

<cache/>
官方示例=====>查看官方文档
<cache
 eviction="FIFO"
 flushInterval="60000"
 size="512"
 readOnly="true"/>
这个更高级的配置创建了一个 FIFO 缓存,每隔 60 秒刷新,最多可以存储结果对象或列表的 512 个引用,而且返回的对象被认为是只读的,因此对它们进行修改可能会在不同线程中的调用者产生冲突。

可用的清除策略有:

  • LRU – 最近最少使用:移除最长时间不被使用的对象。【默认】
  • FIFO – 先进先出:按对象进入缓存的顺序来移除它们。
  • SOFT – 软引用:基于垃圾回收器状态和软引用规则移除对象。
  • WEAK – 弱引用:更积极地基于垃圾收集器状态和弱引用规则移除对象。

缓存顺序

  • 先看二级缓存中有没有
  • 再看级缓存中有没有I
  • 查询数据库
原文地址:https://www.cnblogs.com/chaostudy/p/12989527.html