Mybatis入门到入坑(二)

本文详细介绍了Mybatis的动态SQL功能,包括if、Where、Trim、Choose、When、Otherwise和foreach元素的使用,以及如何简化SQL拼接。此外,还深入探讨了Mybatis的一级和二级缓存机制,解释了缓存在提高执行效率中的作用,并指出一级缓存默认开启,范围是SqlSession,而二级缓存需要手动开启,作用于SqlSessionFactory级别。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动态sql

动态 SQL 是 MyBatis 的强大特性之一。如果你使用过 JDBC 或其它类似的框架,你应该能理解根据不同条件拼接 SQL 语句有多痛苦,例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL,可以彻底摆脱这种痛苦

if

<select id="selectBy1" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from product where 1=1
        <if test="id !=null and id !=''">
            and id = #{id,jdbcType=NUMERIC}
        </if>
        <if test="name !=null and name !=''">
            and name = #{name,jdbcType=VARCHAR}
        </if>

    </select>
标签含有
if test=’‘条件

Where

<select id="selectBy1" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
            from product <where>
        <if test="id !=null and id !=''">
            and id = #{id,jdbcType=NUMERIC}
        </if>
        <if test="name !=null and name !=''">
            and name = #{name,jdbcType=VARCHAR}
        </if>
        </where>
    </select>

能将第一个匹配条件的内容前的and或者or去掉

trim

如果 where 元素与你期望的不太一样,你也可以通过自定义 trim 元素来定制 where 元素的功能。比如,和 where 元素等价的自定义 trim 元素为:

<trim prefix="WHERE" prefixOverrides="AND |OR ">
  ...
</trim>
标签值含有
prefixtrim前面添加指定内容
prefixOverridestrim前面删除指定内容
suffixtrim后面添加指定内容
suffixOverridestrim后面添加删除内容
能将第一个匹配条件的内容前的and或者or去掉

choose、when、otherwise

相当于ifeles。。。ifelse

<select id="findActiveBlogLike"
     resultType="Blog">
  SELECT * FROM BLOG WHERE state = ‘ACTIVE’
  <choose>
    <when test="title != null">
      AND title like #{title}
    </when>
    <when test="author != null and author.name != null">
      AND author_name like #{author.name}
    </when>
    <otherwise>
      AND featured = 1
    </otherwise>
  </choose>
</select>

表示if else 。。 ifelse 只有一个匹配一个,下面就不匹配

forerch

<if test="depIds.size()>0 and depIds !=null">
                and mptd.team_dep_id in(
                <foreach collection="depIds" separator="," item="depId">
                    #{depId,jdbcType=BIGINT}
                </foreach>
                )
            </if>
标签值含义
collection当前入参迭代器
item集合项
index索引
open以…开头
close以…结束
separator分隔符

SQL

SQL标签就是sql字段

<!--定义-->
<sql id="Base_Column_List">
        id,name,price,
        version
    </sql>
<!--使用-->
 <select id="select" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from product
    </select>

缓存机制

缓存是存在于内存中的临时数据,使用缓存减少和数据库的交互次数,提高执行效率。

一级缓存

默认开启,范围级别sqlSerssion,一级缓存无过期时间,只有生命周期

二级缓存

手动开启,范围级别sqlSerssionFactory,一级缓存无过期时间,只有生命周期。默认情况下,只启用了本地的会话缓存,它仅仅对一个会话中的数据进行缓存。 要启用全局的二级缓存,只需要在你的 SQL 映射文件中添加一行:

<cache/>

开启:设置全局配置属性加标签,在sqlSerssion关闭或者提交,实体类序列化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值