Mybatis动态sql,缓存机制

动态sql

  1. 根据不同的情况执行不同的sql语句。mybatis中有标签实现动态sql,类似于jstl,动态sql就是在拼接sql,本质还是sql语句,只是在sql层面加上一些逻辑代码,拼接出正确的代码

  2. 动态sql必须在mapper.xml中编写

  3. if和where标签

  4. <!--where标签, 如果if标签里面有and,恰巧拼在了第一个,where会把and去掉-->
    <select id="getBlogWhere" resultType="blog" parameterType="map" >
        select * from blog
        <where>
            <if test="title != null">
                title = #{title}
            </if>
            <if test="author != null">
                and author =#{author}
            </if>
    
        </where>
    </select>
    
  5. choose标签

  6. <!--choose标签,相当于case,只进入一个,若有多个进入第一个-->
    <select id="getBlogChoose" resultType="blog" parameterType="map" >
        select * from blog
        <where>
            <choose>
                <when test="title != null">
                    title = #{title}
                </when>
                <when test="author != null">
                    and author =#{author}
                </when>
                <otherwise>
                    1=1
                </otherwise>
            </choose>
        </where>
    </select>
    
  7. set标签和sql语句块

  8. <!--自动拼接set,并且如果语句中有,会自动删去,-->
    <!--使用sql include 抽取sql片段 最好基于单表定义,最好不要有where,最好只用if-->
    <update id="updateTrim" parameterType="blog">
       <include refid="updateTrimsql"></include>
    </update>
    <sql id="updateTrimsql">
        update blog
        <set>
            <if test="title != null">
                title = #{title},
            </if>
            <if test="author != null">
                author = #{author},
            </if>
        </set>
        where id = #{id}
    </sql>
    
  9. foreach标签

  10. <!--foreach-->
    <!--对集合进行遍历,通常是在构建in语句的时候-->
    <!--拼接  select * from blog where 1=1 and (id = 1 or id =2 or id =3)-->
    <!--collection为map中的list的key的名字,item为list中取出来的值,open是拼接的开始,separator是分割符。
    close是拼接结束-->
    <select id="selectForeach" parameterType="map" resultType="blog">
        select * from blog
        <where>
            <foreach collection="ids" item="id" open="1 = 1 and (" separator="or" close=")">
                id = #{id}
            </foreach>
        </where>
    </select>
    
    <select id="getBlogById" resultType="blog">
         select * from blog where id = #{id}
    </select>
    

缓存机制

  1. 缓存:经常需要查询,但不需要修改,于是数据存到内存,方便快速使用。

  2. mybatis的缓存是对select来说

  3. 一级缓存:

  4. 默认开启

  5. 在一个session查询同一个记录的时候,在此session还没有关闭的时候,mybatis把此记录存到内存中,再次用此session查询这个记录,就不用去数据库查询了,直接从内存拉取

  6. 如果在session没有close的时候进行了增删改,缓存会刷新

  7. 可以通过sesson.clearCache()清理缓存

  8. 二级缓存:

  9. 需要手动开启

  10. 工作机制:一个会话查询记录,会存在会存在一级缓存中,若关闭此session,会存到二级缓存中。若查相同的记录,首先会去二级缓存查找,若没找到,再去一级缓存,若也没找到再进行查询数据库

  11. 二级缓存,不同的mapper有自己的map,即不同的mapper的查询是不同的换成你

  12. 开启方法

  13. 在核心配置文件中setting中开启缓存<setting name="cacheEnabled" value="true"/>(虽然默认是开启的)

  14. 在不同的mapper.xml中添加<cache/>标签即可,cache标签可自由配置属性,参考官网文档

  15. 注意:缓存的select语句需要在mapper.xml中,不能使用注解的select,select标签的属性可选择是否缓存

  16. pojo类需要序列化即盘pojo类implements Serializable