MyBatis——动态SQL

前言

MyBatis 的一个强大的特性之一通常是它的动态 SQL 能力。 如果你有使用 JDBC 或其他 相似框架的经验,你就明白条件地串联 SQL 字符串在一起是多么的痛苦,确保不能忘了空 格或在列表的最后省略逗号。动态 SQL 可以彻底处理这种痛苦。

通常使用动态 SQL 不可能是独立的一部分,MyBatis 当然使用一种强大的动态 SQL 语 言来改进这种情形,这种语言可以被用在任意映射的 SQL 语句中。

动态 SQL 元素和使用 JSTL 或其他相似的基于 XML 的文本处理器相似。在 MyBatis 之 前的版本中,有很多的元素需要来了解。MyBatis 3 大大提升了它们,现在用不到原先一半 的元素就能工作了。MyBatis 采用功能强大的基于 OGNL 的表达式来消除其他元素。

此篇博客基于上一篇的基础 

动态sql

 if语句

当方法有多个基本类型的参数 需要使用@Param指明将形参给xml文件中的那个参数

Dept select(@Param("deptno") int deptno,@Param("dname") String dname);

@Param 作用:指明方法的参数name 给xml文件中的name

<select id="select" resultMap="deptMap">
        select * from dept
        <where>
            <if test="deptno!=0">and deptno=#{deptno}</if>
            <if test="dname!=null"> and dname like concat('%',#{dname},'%')</if>
        </where>
</select>

Where语句

如果查询语句后面有多个条件

  • 如果有一个if以上的条件被执行,where才会被添加
  • 如果最后的内容以and或or开头,where标签可以自动把他们去除

Set语句

 <update id="updateDept" parameterType="Dept">
        update dept
        <set>
            <if test="dname!=null">dname=#{dname},</if>
            <if test="loc!=null">loc=#{loc},</if>
            <if test="deptno!=0">deptno=#{deptno},</if>
        </set>
        where deptno=#{deptno}
    </update>

choose、when、otherwise

 <select id="select" resultMap="deptMap">
        select * from dept
        <where>
            <choose>
                <when test="deptno!=0">and deptno=#{deptno}</when>
                <when test="dname!=null"> and dname like concat('%',#{dname},'%')</when>
                <otherwise>and loc=#{loc}</otherwise>
            </choose>
        </where>
    </select>

利用concat函数实现模糊查询 只能选择查询条件中的一条

trim

trim标记是一个格式化的标记,可以完成set或者是where标记的功能

<select id="select" resultType="dept" parameterType="Dept">
        select * from dept
        <trim prefix="where" prefixOverrides="and | or">
            <if test="dname!= null">
               and dname=#{dname}
            </if>
            <if test="loc!= null">
               and loc=#{loc}
            </if>
        </trim>
    </select>

用法: 

  • prefix:前缀      
  • prefixoverride:去掉第一个and或者是or
  • suffix:后缀  
  • suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)

foreach

参数为list,map集合

list,传入list集合

EmpDao

 //批量删除
    int deleteEmp(List ids);

EnpDao.xml

 <delete id="deleteEmp">
        delete from emp where id in
        <foreach collection="list" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </delete>

测试类

  EmpDao empDao = session.getMapper(EmpDao.class);
        List<Integer> list = new ArrayList<>();
        list.add(7934);
        list.add(7936);
        empDao.deleteEmp(list);

打印日志

 map

EmpDao

    //批量删除
    int deleteEmp(Map<String,Object> ids);

EmpDao.xml

  <delete id="deleteEmp">
        delete from emp where id in
        <foreach collection="list" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
        and deptno=#{deptno}
    </delete>

测试类

        EmpDao empDao = session.getMapper(EmpDao.class);
        HashMap<String, Object> map = new HashMap<>();
        List<Integer> list = new ArrayList<>();
        list.add(7934);
        list.add(7936);
        map.put("list",list);
        map.put("deptno",10);
        empDao.deleteEmp(map);

打印日志

MyBatis缓存机制

正如大多数持久层框架一样,MyBatis 同样提供了一级缓存二级缓存的支持

1.一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Session,当 Session flush close 之后,该Session中的所有 Cache 就将清空。

2. 二级缓存与一级缓存其机制相同,默认也是采用 PerpetualCache,HashMap存储,不同在于其存储作用域为 Mapper(Namespace),并且可自定义存储源,如 Ehcache。

3. 对于缓存数据更新机制,当某一个作用域(一级缓存Session/二级缓存Namespaces)的进行了 C/U/D 操作后,默认该作用域下所有 select 中的缓存将被clear。

4. 二级缓存不同于hibernate ,它是一个映射文件级别的缓存

Mybatis一级缓存

一级缓存: 也就Session级的缓存(默认开启)

必须是同一个Session,如果session对象已经close()过了就不可能用了

查询条件是一样的

没有执行过session.clearCache()清理缓存

没有执行过增删改的操作(这些操作都会清理缓存)

Mybatis二级缓存

  使用之前需要开启

  添加一个<cache>在userMapper.xml中

 说明:

1. 映射语句文件中的所有select语句将会被缓存。

2. 映射语句文件中的所有insert,update和delete语句会刷新缓存。

3. 缓存会使用Least Recently Used(LRU,最近最少使用的)算法来收回。

4. 缓存会根据指定的时间间隔来刷新。

5. 缓存会存储1024个对象              

二级缓存的设置

<cache

eviction="FIFO"  //回收策略为先进先出

flushInterval="60000" //自动刷新时间60s

size="512" //最多缓存512个引用对象

readOnly="true"/> //只读

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值