复习第3天

2022/5/31 23:19:40

本文主要是介绍复习第3天,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1. 动态SQL

  1. if标签
<select id="selectUsersByProperty" resultType="users">
    select * from users where 1=1
    <if test="userid != 0">
        and userid = #{userid}
    </if>
</select>
  1. choose标签
<select id="selectUsersByChoose" resultType="users">
    select * from users where 1=1
    <choose>
        <when test="username != null and usersex!= ''">
            and username=#{username}
        </when>
        <otherwise>
            and userid=1
        </otherwise>
    </choose>
</select>
  1. where标签
// 如果判断条件不为空则自动添加where关键字,并且会自动去掉第一个条件前面的and或or
<select id="selectUsersByPropertyWhere" resultType="users">
    select * from users
    <where>
        <if test="userid != 0">
            and userid = #{userid}
        </if>
        <if test="username != null and username != ''">
            and username = #{username}
        </if>
    </where>
</select>
  1. bind标签
// 允许在表达式外创建一个变量,并可以将其绑定到当前的SQL语句中,一般用于模糊查询
<select id="selectUsersByLikeName" resultType="users">
    <bind name="likeName" value"'%'+name+'%'"/>
    select * from users where username like #{likeName}
</select>
  1. set标签
// 自动去掉最后一个if语句的多余的逗号
<update id="usersUpdate">
    update users
    <set>
        <if test="username != null and username != ''">
            username = #{username},
        </if>
        <if test="usersex != null and usersex != ''">
            usersex = #{usersex},
        </if>
    </set>
    where userid = #{userid}
</update>
  1. foreach标签
<select id="selectUsersByIdUseCollection" resultType="users">
    select * from users where userid in
    <foreach collection="collection" item="userid" open="(" separator="," close=")">
        #{userid}
    </foreach>
</select>
  1. foreach标签迭代List,Set
<select id="selectUsersByIdUserCollection" resultType="users">
    select * from users where userid in
    <foreach collection="collection" item="userid" open="(" separator="," close=")">
        #{userid}
    </foreach>
</select>
  1. foreach标签迭代array
<select id="selectUsersByIdUseArray" resultType="users">
    select * from users where userid in
    <foreach collection="array" item="userid" open="(" separator="," close=")">
        #{userid}
    </foreach>
</select>
  1. 迭代Map
<select id="selectUsersCount" resultType="int">
    select count(*) from users where
    <foreach collection="suibian" separator="and" item="value" index="key">
        ${key} = #{value}
    </foreach>
</select>
  1. 使用foreach标签完成批量添加
<insert id="insertUsersBatch">
    insert into users values
    <foreach collection="collection" item="user" separator=",">
        (default, #{user.username}, #{user.usersex})
    </foreach>
</insert>

2. Mybatis缓存

Mybatis会将相同查询条件的SQL语句的查询结果存储在内存或者某种缓存介质当中,当下次遇到相同的查询SQL时候不在执行该SQL,
而是直接从缓存中获取结果,减少服务器的压力,尤其是在查询越多、缓存命中率越高的情况下,使用缓存对性能的提高明显。

1. 一级缓存(默认开启)

如何判断两次查询是完全相同的查询?
mybatis认为,对于两次查询,如果以下条件都完全一样,那么就认为它们是完全相同的两次查询:

  • 传入的statementId
  • 查询时要求的结果集中的结果范围
  • 这次查询所产生的最终要传递给Preparedstatement的sql语句字符串
  • 传递的参数值

2. 二级缓存

mybatis的二级缓存是application级别的缓存,它可以提高对数据库查询的效率,以提高应用的性能。。
二级缓存是sqlSessionFactory上的缓存,可以是由一个SqlSessionFactory创建的不同的SqlSession之间共享缓存数据。
SqlSession在执行commit()或者close()的时候将数据放入到二级缓存。

  1. 实体类必须是可序列化的
  2. 映射语句文件中的所有select语句将会被缓存
  3. 二级缓存是以namespace为单位的,不同namespace下的操作互不影响
  4. 如果在加入标签的前提下让个别select元素不使用缓存,可以使用useCache属性,设置为false
  5. 缓存会使用默认的 Least Recently Used(LRU,最近最少使用的)算法来收回。
  6. 根据时间表,比如 No Flush Interval,(CNFI 没有刷新间隔),缓存不会以任何时间顺序 来刷新。
  7. 缓存会存储列表集合或对象(无论查询方法返回什么)的 1024 个引用
  8. 缓存会被视为是 read/write(可读/可写)的缓存,意味着对象检索不是共享的,而且可以安全的被调用者修改,不干扰其他调用者或线程所做的潜在修改
1. 在映射配置文件中添加<cache/>
2. JavaBean对象必须实现序列化接口


这篇关于复习第3天的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程