ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

复习第3天

2022-05-31 22:34:55  阅读:104  来源: 互联网

标签:缓存 复习 userid 查询 where select users


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对象必须实现序列化接口

标签:缓存,复习,userid,查询,where,select,users
来源: https://www.cnblogs.com/jsqup/p/16332533.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有