九、MyBatis动态SQL

@[toc]

九、动态SQL

9.1 if

总结:根据标签中test属性所对应的表达式决定标签中的内容是否需要拼接到SQL中。

sql 复制代码
User getUserByParamsWithIf(User user);
bash 复制代码
<select id="getUserByParamsWithIf" resultType="User">
        select * from litemall_user where 1=1
        <if test="username != '' and username != null">
            and username = #{username}
        </if>
        <if test="id != 0 and id != null">
            and id = #{id}
        </if>
    </select>

9.2 where

总结

  • 当where标签中有内容时,会自动生成where关键字,并且将内容前多余的and或or去掉。
  • 当where标签中没有内容时,此时where标签没有任何效果。
  • 注意:where标签不能将其中内容后面多余的and或or去掉。
sql 复制代码
User getUserByParamsWithWhere(User user);
bash 复制代码
<select id="getUserByParamsWithWhere" resultType="User">
        select * from litemall_user
        <where>
            <if test="username != '' and username != null">
                and username = #{username}
            </if>
            <if test="id != 0 and id != null">
                and id = #{id}
            </if>
        </where>
    </select>

9.3 trim

总结:trim用于去掉或添加标签中的内容

常用属性

  • prefix:在trim标签中的内容的前面添加某些内容

  • prefixOverrides:在trim标签中的内容的前面去掉某些内容

  • suffix:在trim标签中的内容的后面添加某些内容

  • suffixOverrides:在trim标签中的内容的后面去掉某些内容

sql 复制代码
User getUserByParamsWithTrim(User user);
bash 复制代码
<select id="getUserByParamsWithTrim" resultType="User">
        select * from litemall_user
        <trim prefix="where" suffixOverrides="and">
            <if test="username != '' and username != null">
                 username = #{username} and
            </if>
            <if test="gender != 0 and gender != null">
                 gender = #{gender} and
            </if>
            <if test="id != 0 and id != null">
                id = #{id} and
            </if>
        </trim>
    </select>

9.4 choose、when、otherwise

总结:相当于if..else if ..else ,其中when至少要有一个,otherwise最多只能有一个。

sql 复制代码
List<User> getUserByParamsWithChoose(User user);
bash 复制代码
<select id="getUserByParamsWithChoose" resultType="User">
        select * from litemall_user
        <where>
            <choose>
                <when test="username != '' and username != null">
                    username = #{username}
                </when>
                <when test="gender != 0 and gender != null">
                    gender = #{gender}
                </when>
                <when test="id != 0 and id != null">
                    id = #{id}
                </when>
            </choose>
        </where>
    </select>

9.5 foreach

总结:属性:

  • collection:设置要循环的数组或集合

  • item:表示集合或数组中的每一个数据

  • separator:设置循环体之间的分隔符

  • open:设置foreach标签中的内容的开始符

  • close:设置foreach标签中的内容的结束符

9.6 SQL片段

总结:sql片段,可以记录一段公共sql片段,在使用的地方通过include标签进行引入。

csharp 复制代码
User getUserByParamsWithSQLFragment();
bash 复制代码
<sql id="userColumns">
        id, username, password, mobile, gender, last_login_ip
</sql>
<select id="getUserByParamsWithSQLFragment" resultType="User">
        select <include refid="userColumns"></include> from litemall_user where id = 4
</select>

本人其他相关文章链接

1.一、MyBatis简介:MyBatis历史、MyBatis特性、和其它持久化层技术对比、Mybatis下载依赖包流程 2.二、搭建MyBatis采用xml方式,验证CRUD(增删改查操作) 3.三、MyBatis核心配置文件详解 4.四、MyBatis获取参数值的两种方式(重点) 5.五、MyBatis的增删改查模板(参数形式包括:String、对象、集合、数组、Map) 6.六、MyBatis特殊的SQL:模糊查询、动态设置表名、校验名称唯一性 7.七、MyBatis自定义映射resultMap 8.八、(了解即可)MyBatis懒加载(或者叫延迟加载) 9.九、MyBatis动态SQL 10.十、MyBatis的缓存 11.十一、MyBatis的逆向工程 12.十二、MyBatis分页插件

重要信息

相关推荐
想变成树袋熊19 分钟前
【自用】NLP算法面经(6)
人工智能·算法·自然语言处理
极地星光39 分钟前
TCP/IP 网络编程面试题及解答
网络·网络协议·tcp/ip
格林威41 分钟前
Baumer工业相机堡盟工业相机如何通过YoloV8深度学习模型实现沙滩小人检测识别(C#代码UI界面版)
人工智能·深度学习·数码相机·yolo·计算机视觉
checkcheckck1 小时前
spring ai 适配 流式回答、mcp、milvus向量数据库、rag、聊天会话记忆
人工智能
Microvision维视智造1 小时前
从“人工眼”到‘智能眼’:EZ-Vision视觉系统如何重构生产线视觉检测精度?
图像处理·人工智能·重构·视觉检测
巫婆理发2221 小时前
神经网络(多层感知机)(第二课第二周)
人工智能·深度学习·神经网络
lxmyzzs1 小时前
【打怪升级 - 03】YOLO11/YOLO12/YOLOv10/YOLOv8 完全指南:从理论到代码实战,新手入门必看教程
人工智能·神经网络·yolo·目标检测·计算机视觉
SEO_juper1 小时前
企业级 AI 工具选型报告:9 个技术平台的 ROI 对比与部署策略
人工智能·搜索引擎·百度·llm·工具·geo·数字营销
Coovally AI模型快速验证2 小时前
数据集分享 | 智慧农业实战数据集精选
人工智能·算法·目标检测·机器学习·计算机视觉·目标跟踪·无人机
xw33734095642 小时前
彩色转灰度的核心逻辑:三种经典方法及原理对比
人工智能·python·深度学习·opencv·计算机视觉