MyBatis ---- 动态SQL

news2024/9/22 6:43:36

MyBatis ---- 动态SQL

  • 1. if
  • 2. where
  • 3. trim
  • 4. choose、when、otherwise
  • 5. foreach
  • 6. SQL片段

MyBatis 框架的动态 SQL 技术是一种根据特定条件动态拼接 SQL 语句的功能,它存在的意义是为了解决拼接 SQL 语句字符串时的痛点问题。

1. if

    /**
     * 根据条件查询员工信息if
     * @param emp
     * @return
     */
    List<Emp> getEmpListByMoreTJ(Emp emp);
    <!--List<Emp> getEmpListByMoreTJ(Emp emp);-->
    <select id="getEmpListByMoreTJ" resultType="emp">
        select * from t_emp where
        <if test="empName != '' and empName != null">
            emp_name = #{empName}
        </if>
        <if test="age != '' and age != null">
            and age = #{age}
        </if>
        <if test="sex != '' and sex != null">
            and sex = #{sex}
        </if>
        <if test="email != '' and email != null">
            and email = #{email}
        </if>
    </select>

在这里插入图片描述
当 age、sex…等条件为空时,不会影响查询结果,但是若 empName 为空的话,查询语句就会变成 where 后面直接跟 and,这是不符合语法规则的,避免措施:
在查询语句中添加一个 1=1,恒成立条件,且不会影响查询效果

    <!--List<Emp> getEmpListByMoreTJ(Emp emp);-->
    <select id="getEmpListByMoreTJ" resultType="emp">
        select * from t_emp where 1=1
        <if test="empName != '' and empName != null">
            and emp_name = #{empName}
        </if>
        <if test="age != '' and age != null">
            and age = #{age}
        </if>
        <if test="sex != '' and sex != null">
            and sex = #{sex}
        </if>
        <if test="email != '' and email != null">
            and email = #{email}
        </if>
    </select>

在这里插入图片描述

2. where

    /**
     * 根据条件查询员工信息where
     * @param emp
     * @return
     */
    List<Emp> getEmpListByMoreTJ2(Emp emp);
    <!--List<Emp> getEmpListByMoreTJ2(Emp emp);-->
    <select id="getEmpListByMoreTJ2" resultType="emp">
        select * from t_emp
        <where>
            <if test="empName != '' and empName != null">
                emp_name = #{empName}
            </if>
            <if test="age != '' and age != null">
                and age = #{age}
            </if>
            <if test="sex != '' and sex != null">
                and sex = #{sex}
            </if>
            <if test="email != '' and email != null">
                and email = #{email}
            </if>
        </where>
    </select>

在这里插入图片描述

where 和 if 一般结合使用:
a> 若 where 标签找那个的 if 条件都不满足,则 where 标签没有任何功能,即不会添加 where 关键字
b> 若 where 标签中的 if 条件满足,则 where 标签会自动添加 where 关键字,并将条件最前方多余的 and 去掉
注意:where 标签不能去掉条件最后多余的 and

3. trim

    /**
     * 根据条件查询员工信息trim
     * @param emp
     * @return
     */
    List<Emp> getEmpListByMoreTJ3(Emp emp);
    <!--List<Emp> getEmpListByMoreTJ3(Emp emp);-->
    <select id="getEmpListByMoreTJ3" resultType="emp">
        select * from t_emp
        <trim prefix="where" suffixOverrides="and">
            <if test="empName != '' and empName != null">
                emp_name = #{empName} and
            </if>
            <if test="age != '' and age != null">
                age = #{age} and
            </if>
            <if test="sex != '' and sex != null">
                sex = #{sex} and
            </if>
            <if test="email != '' and email != null">
                email = #{email} and
            </if>
        </trim>
    </select>

在这里插入图片描述

4. choose、when、otherwise

choose、when、otherwise相当于if…else if…else

    /**
     * choose、when、otherwise
     * @param emp
     * @return
     */
    List<Emp> getEmpListByMoreTJ4(Emp emp);
    <!--List<Emp> getEmpListByMoreTJ4(Emp emp);-->
    <select id="getEmpListByMoreTJ4" resultType="emp">
        select * from t_emp
        <where>
            <choose>
                <when test="empName != '' and empName != null">
                    emp_name = #{empName}
                </when>
                <when test="age != '' and age != null">
                    age = #{age}
                </when>
                <when test="sex != '' and sex != null">
                    sex = #{sex}
                </when>
                <when test="email != '' and email != null">
                    email = #{email}
                </when>
                <otherwise>
                    did = 1
                </otherwise>
            </choose>
        </where>
    </select>
    @Test
    public void testGetEmpListByMoreTJ4(){

        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamicSQLMapper dynamicSQLMapper = sqlSession.getMapper(DynamicSQLMapper.class);
        List<Emp> empListByMoreTJ = dynamicSQLMapper.getEmpListByMoreTJ4(new Emp(null, null, null, null, null));
        empListByMoreTJ.forEach(emp -> System.out.println(emp));

    }

在这里插入图片描述

5. foreach

    /**
     * 通过数组批量删除
     * @param eids
     * @return
     */
    int deleteMoreByArray(@Param("eids") Integer[] eids);
    <!--int deleteMoreByArray(@Param("eids") Integer[] eids);-->
    <delete id="deleteMoreByArray">
        delete from t_emp where
        <foreach collection="eids" item="eid" separator="or">
            eid = #{eid}
        </foreach>
    </delete>
    @Test
    public void testDeleteMoreByArray(){

        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamicSQLMapper dynamicSQLMapper = sqlSession.getMapper(DynamicSQLMapper.class);
        int i = dynamicSQLMapper.deleteMoreByArray(new Integer[]{6, 7, 8});
        System.out.println(i);

    }

collection:设置要循环的数组或集合
item:表示集合或数组中的每一个数据
separator:设置循环体之间的分隔符
open:设置 foreach 标签中的内容的开始符
close:设置 foreach 标签中的内容的结束符

在这里插入图片描述

6. SQL片段

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

    <sql id="empColumns">eid, emp_name, age, sex</sql>

    <!--List<Emp> getEmpListByMoreTJ4(Emp emp);-->
    <select id="getEmpListByMoreTJ4" resultType="emp">
        select <include refid="empColumns"></include> from t_emp
        <where>
            <choose>
                <when test="empName != '' and empName != null">
                    emp_name = #{empName}
                </when>
                <when test="age != '' and age != null">
                    age = #{age}
                </when>
                <when test="sex != '' and sex != null">
                    sex = #{sex}
                </when>
                <when test="email != '' and email != null">
                    email = #{email}
                </when>
                <otherwise>
                    did = 1
                </otherwise>
            </choose>
        </where>
    </select>

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/60053.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

eBPF书籍和教程良心推荐

中文 BPF 性能工具&#xff08;书籍&#xff09;&#xff0c;作者 Brendan Gregg。本书的GitHub 回购。系统性能&#xff1a;企业与云&#xff0c;第 2 版 (2020)&#xff0c;作者&#xff1a;Brendan GreggJed Salazar 和 Natalia Reka Ivanko 的 eBPF 安全可观察性什么是 eB…

Metabase学习教程:系统管理-5

仪表板优化 如何使您的仪表板加载更快。 说到仪表板性能方面&#xff0c;基本上有四种方法可以让仪表板更快地加载&#xff1a; 要求更少的数据.缓存问题答案.组织数据以预测常见问题.提出有效的问题。图1。包含三个筛选器小部件的示例仪表板&#xff0c;它们使用Metabase附…

友宝在线在港交所上市申请“失效”:连续两年亏损,王滨为大股东

近日&#xff0c;贝多财经从港交所披露易了解到&#xff0c;Beijing UBOX Online Technology Corp.&#xff08;北京友宝在线科技股份有限公司&#xff0c;下称“友宝”或“友宝在线”&#xff09;的上市申请材料已经失效&#xff0c;目前已经无法查看。 其中&#xff0c;招股书…

期末前端web大作业:餐饮美食网站设计与实现——餐厅响应式网站制作html+css+javascript+jquery+bootstarp

&#x1f380; 精彩专栏推荐&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb;&#x1f447;&#x1f3fb; ✍️ 作者简介: 一个热爱把逻辑思维转变为代码的技术博主 &#x1f482; 作者主页: 【主页——&#x1f680;获取更多优质源码】 &#x1f393; web前端期末大作业…

Android开发的UI设计——Material Design

前言 Material Design 是用于指导用户在各种平台和设备上进行视觉、动作和互动设计的全面指南。如需在您的 Android 应用中使用 Material Design&#xff0c;请遵循 Material Design 规范中定义的准则&#xff0c;并使用 Material Design 支持库中提供的新组件和样式。 正篇 …

【软件安装】Ubuntu18.04及20.04中安装omnet++

注意&#xff1a;安装omnet首先看官方安装指导&#xff0c;不要直接百度。 omnet6.0.1官方安装指导omnet6.0只能在Ubuntu20.04及之后的版本使用&#xff0c;因为glibc版本不适配。 Ubuntu18.04安装omnet5.6.2 安装必要支持 更新apt-get $ sudo apt-get update安装依赖软件 $ s…

2022年四川省职业院校技能大赛网络搭建与应用赛项

2022年四川省职业院校技能大赛 网络搭建与应用赛项 &#xff08;一&#xff09; 技能要求 &#xff08;总分1000分&#xff09; 网络搭建与应用赛项执委会及专家组 2022年06月 竞赛说明 一、竞赛内容分布 “网络搭建与应用”竞赛共分三个部分&#xff0c;其中&#xff1a; 第一…

3个常用的损失函数

1. L2 loss &#xff08;均方损失&#xff09; 除以2就是可以在求导时2和1/2可以相乘抵消。 蓝色的曲线表示&#xff1a;y0时&#xff0c;变化预测值y’的函数。 绿色曲线表示&#xff1a;似然函数。e^-l。 是一个高斯分布。 橙色的线&#xff1a;表示损失函数的梯度 可以看到…

记录Windows下mysql更改my.ini文件中datadir路径后启动不起来的问题

1.mysql默认安装到了C盘&#xff0c;想将数据库存储路径改到别的盘下 将Data文件夹和日志复制到H盘 找到mysl服务&#xff0c;右键停止服务 更改my.ini文件中的路径 保存然后启动发现启动不起来 猜测原因1&#xff1a;文件夹没有权限 将文件夹权限给到所有的用户 右击 ”…

[附源码]Python计算机毕业设计Django青栞系统

项目运行 环境配置&#xff1a; Pychram社区版 python3.7.7 Mysql5.7 HBuilderXlist pipNavicat11Djangonodejs。 项目技术&#xff1a; django python Vue 等等组成&#xff0c;B/S模式 pychram管理等等。 环境需要 1.运行环境&#xff1a;最好是python3.7.7&#xff0c;…

Linux的进程创建

在Linux下面&#xff0c;对二进制程序有着严格的格式要求&#xff0c;这就是ELF&#xff0c;这个格式可以根据编译的结果不同&#xff0c;分为不同的格式。 ELF的三种类型 一&#xff1a;可重定位文件 在编译的时候&#xff0c;先做预处理工作&#xff0c;例如将头文件嵌入到…

VueX简单又详细的解读,看了就会用

一、VueX是什么 Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式 库。它采用集中式存储管理应用的所有组件的状态&#xff0c;并以相应的规则保证状态以一种可预测的方式发生变化。 二、为什么要用VueX “单向数据流”理念的简单示意&#xff1a; 当我们的应用遇到多个组…

Redis缓存

一.简介 缓存就是数据交换的缓冲区&#xff08;称作Cache [ kʃ ] &#xff09;&#xff0c;是存贮数据的临时地方&#xff0c;一般读写性能较高 二.添加Redis缓存 三.缓存更新策略 1.主动更新策略 Cache Aside Pattern(推荐) 需要调用者自己编码&#xff0c;但可控性高 Re…

SimSiam-Exploring Simple Siamese Pepresentation Learning

SimSiam Abstract 模型坍塌&#xff0c;在siamese中主要是输入数据经过卷积激活后收敛到同一个常数上&#xff0c;导致无论输入什么图像&#xff0c;输出结果都能相同。 而He提出的simple Siamese networks在没有采用之前的避免模型坍塌那些方法&#xff1a; 使用负样本lar…

K_A08_003 基于 STM32等单片机驱动L9110模块按键控制直流电机正反转加减速启停

目录 一、资源说明 二、基本参数 1、参数 2、引脚说明 三、驱动说明 L9110模块驱动时序 对应程序: PWM信号 四、部分代码说明 接线说明 1、STC89C52RCL9110模块 2、STM32F103C8T6L9110模块 五、基础知识学习与相关资料下载 六、视频效果展示与程序资料获取 七、项目主要…

【Android工具】群晖安卓客户端基础套件:Drive、video、Photos和DS video安卓TV客户端...

微信关注公众号 “DLGG创客DIY”设为“星标”&#xff0c;重磅干货&#xff0c;第一时间送达。最近终于把all in one搞起来了&#xff0c;all in one就是把一堆功能一堆软件装一台主机里。。all in one&#xff08;以后简称AIO&#xff09;相关内容回头慢慢聊。今天先聊聊群晖&…

从一个demo说elf文件

本文的demo是在linux环境下编译解析的&#xff0c;cpu是x86-64 首先我们先写一个功能简单的demo-SimpleSection.c。这个demo中有一个func1函数用来打印数据&#xff0c;一个已经初始化的全局变量global_init_var和未初始化的全局变量global_uninit_var&#xff0c;一个已初始化…

使用TS 封装 自定义hooks,实现不一样的 CRUD

文章目录使用TS 封装 自定义hooks&#xff0c;实现不一样的 CRUD自定义 hooks文件结构type.tsuseDelData.ts使用useFetchList.ts使用useInsert.ts使用部分的接口方法使用TS 封装 自定义hooks&#xff0c;实现不一样的 CRUD 这一篇主要是记录 查缺补漏&#xff0c;提升自己的 强…

三、内存管理 (一)存储器管理

目录 1.1程序运行的基本过程 1.1.1 编辑、编译、链接、装入 1.1.2链接的三种方式 1.1.3装入的三种方式 1.2内存管理基本概念 1.2.1内存保护 1.2.2内存空间扩充 1.2.3地址转换功能 1.2.4内存空间的分配与回收 1.2.4.1连续分配管理方式 1.2.4.1.1单一连续分配 1.2.4.1…

Http协议和Https协议

Http是不安全的&#xff0c;你的数据容易被黑客拦截&#xff0c;篡改&#xff0c;攻击 https要求对数据加密&#xff08;不能明文传输&#xff09;, 用抓包工具抓http请求&#xff0c;抓出来的都是明文的&#xff0c;你能看得懂的&#xff0c;抓https请求&#xff0c;抓出来的…