动态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的发起多次同构查询,会将数据保存在一级缓存中。
- 注意:无需任何配置,默认开启一级缓存。
- 注意:缓存是针对的同一条sql语句,条件也必须相同
- 注意:mybatis的一次缓存是针对的同一个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数据库)
上一篇: Git(工具)
下一篇: 分页插件和pojo的插件
推荐阅读