动态sql与缓存(一级缓存,二级缓存)

动态SQL:MyBatis的映射文件中支持在基础SQL上添加一些逻辑操作,并动态拼接成完整的SQL之后再执行,以达到SQL复用、简化编程的效果。

:类似于一个标签用以定义sql片段

<mapper namespace="com.qf.mybatis.part2.dynamic.BookDao">
    <sql id="BOOKS_FIELD"> <!-- 定义SQL片段 -->
        SELECT id,name,author,publish,sort
    </sql>
    <select id="selectBookByCondition" resultType="com.qf.mybatis.part2.dynamic.Book">
				<include refid="BOOKS_FIELD" /> <!-- 通过ID引用SQL片段 -->
        FROM t_books
    </select>
</mapper>

:会自动忽略前后缀(如:and | or) 因为当你自动生成sql语句时,where后不能直接有and啊
还有就是where的用法:就是为了适应用户自由动态的定义各个属性名,当没有值时能查,传值来了也可以查(where一般只用于查

<select id="selectBookByCondition" resultType="com.qf.mybatis.part2.dynamic.Book">
    SELECT id , name , author , publish , sort
    FROM t_books
    <where> 
        <if test="id != null"> <!-- WHERE,会自动忽略前缀(如:and | or) -->
            id = #{id}
        </if>
        <if test="name != null">
            and name = #{name}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
        <if test="publish != null">
            and publish = #{publish}
        </if>
        <if test="sort != null">
            and sort = #{sort}
        </if>
    </where>
</select>

< set >:用于自动忽略后缀(如:,) -->;(一般用于update修改)

<update id="updateBookByCondition">
    UPDATE t_books
    <set>
        <if test="name != null"><!-- where子句中满足条件的if,会自动忽略后缀(如:,-->
            name = #{name} ,
        </if>
        <if test="author != null">
            author = #{author} ,
        </if>
        <if test="publish != null">
            publish = #{publish} ,
        </if>
        <if test="sort != null">
            sort = #{sort} ,
        </if>
    </set>
    WHERE id = #{id}
</update>

< trim >:< trim prefix="" suffix="" prefixOverrides="" suffixOverrides="" >代替< where > 、< set >
可以根据prefix和prefixOverrides设置where的自动忽略前缀;prefix和suffixOverrides设置set的自动忽略后缀。

<select id="selectBookByCondition" resultType="com.qf.mybatis.day2.dynamic.Book">
		SELECT id,name,author,publish,sort
    FROM t_books
    <trim prefix="WHERE" prefixOverrides="AND|OR"> <!-- 增加WHERE前缀,自动忽略前缀 -->
        <if test="id != null">
            and id = #{id}
        </if>
        <if test="name != null">
            and name = #{name}
        </if>
        <if test="author != null">
            and author = #{author}
        </if>
        <if test="publish != null">
            and publish = #{publish}
        </if>
        <if test="sort != null">
            and sort = #{sort}
        </if>
		</trim>
</select>
<update id="updateBookByCondition">
		UPDATE t_books
		<trim prefix="SET" suffixOverrides=","> <!-- 增加SET前缀,自动忽略后缀 -->
				<if test="name != null">
						name = #{name} ,
				</if>

				<if test="author != null">
						author = #{author} ,
				</if>

				<if test="publish != null">
						publish = #{publish} ,
				</if>

				<if test="sort != null">
						sort = #{sort}
				</if>
    </trim>
		WHERE id = #{id}
</update>

;用于批量的增删改查的操作,遍历多个对象(行)里面的所有信息。
增加的例子:(重要,批量插入数据)
Userdao:
integer insertManyUser(List users)
mapper.xml中:
在这里插入图片描述
测试类:插入张三和李四
在这里插入图片描述

删除的例子:
Userdao:
Integer delectManyUser(List ids);
mapper.xml中:删除
在这里插入图片描述
// open,separator和close表示边集合的开头结尾和分割,item表示存储id的值
#{id}//每次遍历得到的值

测试类:
在这里插入图片描述

缓存

一级缓存:SqlSession级别的缓存,同一个SqlSession的发起多次同构查询,会将数据保存在一级缓存中。

如下:要查同一个id,查了两次
在这里插入图片描述
但是结果只执行了一次sql(所以说明:默认开启一级缓存)
在这里插入图片描述
二级缓存:一般不使用
SqlSessionFactory级别的缓存,同一个SqlSessionFactory构建的SqlSession发起的多次同构查询,会将数据保存二级缓存使用流程:,
1,要在config设置setting开启全局缓存

<configuration>
	<properties .../>
  	
  	<!-- 注意书写位置 -->
    <settings>
        <setting name="cacheEnabled" value="true"/> <!-- mybaits-config.xml中开启全局缓存(默认开启) -->
    </settings>
  
  	<typeAliases></typeAliases>
</configuration>

2,要在mapper。xml的后边设置<catch

3,dml语句要提交事务,每一次必须关闭SqlSession才可缓存数据。

sqlSession3.commit(); //DML成功,数据发生变化,缓存清空

		sqlSession3.close();

mybatis的二级缓存是和sqlSessionFactory有关,那么不同的人进来会有不同的sqlsession生成,那么sqlSession越多,缓存命中的概率就越低,所以在实际开发中,我们会将特殊需求的查询结果做缓存,交给缓存数据库去做(nosql数据库)

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值