MyBatis<foreach>标签的用法与实践

news/2024/9/30 8:26:59 标签: mybatis

foreach标签简介

在这里插入图片描述

实践

demo1
简单的一个批量更新,这里传入了一个List类型的集合作为参数,拼接到 in 的后面 ,来实现一个简单的批量更新

<update id="updateVislxble" parameterType="java.util.List">
        update models set visible =0
        where llm_id
        IN
        <foreach collection="allVisible" item="visible" open="(" separator="," close=")">
            #{visible}
        </foreach>
    </update>
  • collection=“allVisible”,allVisible代表传入的集合
  • item=“visible” ,visible代表集合的每一个元素
  • open=“(” 代表以( 开头
  • separator=“,” 代表以“,”分隔,
  • close=“)” 代表以“)”结束

本质上这里是个拼接,所有要保证这里list不为空,不然会报错

demo2
这里还是个批量更新,但是传入的参数多了起来,这里是的入参是个map,并且里面这个status是一个List类型的

  <select id="getModelEvaluateTaskPage" parameterType="java.util.Map" resultType="com.test.test">
        select * from evaluate_tasks t
        <where>
            <if test="status !=null">
                and  t.status  in
                <foreach item="item" index="index" collection="status"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="taskName !=null and taskName!=''">
                and  t.task_name like concat('%',#{taskName},'%')
            </if>
            <if test="startTime !=null and endTime!=null">
                and  t.created_time between #{startTime} and #{endTime}
            </if>
            and t.user_id = #{userId}
        </where>
        order by t.created_time ${sort}
    </select>

demo3
这里的场景传入了一个list,里面是多个对象,根据对象的属性A去更新属性B,这里额外使用了 case when then

 <update id="bitchUpdateStatueById" parameterType="java.util.List">
        UPDATE evaluate_tasks
        SET status = CASE service_name
        <foreach collection="updates" item="update" separator=" ">
            WHEN #{update.serviceName} THEN #{update.status}
        </foreach>
        END,
        updated_time = CASE service_name
        <foreach collection="updates" item="update" separator=" ">
            WHEN #{update.serviceName} THEN #{update.endTime}
        </foreach>
        END
        WHERE service_name IN
        <foreach collection="updates" item="update" open="(" separator="," close=")">
            #{update.serviceName}
        </foreach>
    </update>

http://www.niftyadmin.cn/n/5684720.html

相关文章

华为云LTS日志上报至观测云最佳实践

华为云LTS简介 华为云云日志服务&#xff08;Log Tank Service&#xff0c;简称 LTS&#xff09;&#xff0c;用于收集来自主机和云服务的日志数据&#xff0c;通过海量日志数据的分析与处理&#xff0c;可以将云服务和应用程序的可用性和性能最大化&#xff0c;为您提供实时、…

贝锐蒲公英网盘首发,秒建私有云,高速远程访问

虽然公共网盘带来了不少便利&#xff0c;但是大家对隐私泄露和重要数据泄密的担忧也随之增加。如果想要确保数据安全&#xff0c;自建私有云似乎是一条出路&#xff0c;然而面对搭建私有云的复杂步骤&#xff0c;许多人感到力不从心&#xff0c;NAS设备的成本也往往让人望而却步…

机器学习:opencv--背景建模

目录 一、背景建模是什么&#xff1f; 二、背景建模的目的 三、背景建模的方法及原理 四、代码实现 1.创建卷积核 2.创建混合高斯模型 3.处理图像 4.绘制人形轮廓 5.条件退出 一、背景建模是什么&#xff1f; 指在计算机视觉中&#xff0c;从视频序列中提取出静态背景…

常用激活函数总结

文章目录 什么是激活函数激活函数的作用常用激活函数1.Sigmoid函数2.Softmax函数3.Tanh函数4.Relu函数5.LeakyRelu函数6.PRelu函数7.ELU函数8.SELU函数 什么是激活函数 激活函数&#xff0c;通俗讲&#xff0c;就是一个函数&#xff0c;针对某个神经元&#xff0c;就是将输入经…

解决R语言bug ‘sh‘ is not recognized as an internal or external command

安装源码包‘httr2’ trying URL ‘https://cran.rstudio.com/src/contrib/httr2_1.0.5.tar.gz’ Content type ‘application/x-gzip’ length 230632 bytes (225 KB) downloaded 225 KB installing source package ‘httr2’ … ** package ‘httr2’ successfully unpacked…

paypal支付v2.0(php)支付代码

第一步&#xff1a;获取access_token: <?php$clientId ; // 替换为你的 PayPal Client ID $clientSecret ; // 替换为你的 PayPal Client Secret// PayPal API 请求的 URL $url "https://api-m.sandbox.paypal.com/v1/oauth2/token";// 初始化 cURL $ch …

SpringCloud-Alibaba第二代微服务快速入门

1.简介 Spring Cloud Alibaba其实是阿里的微服务解决方案&#xff0c;是阿里巴巴结合自身微服务实践,开源的微服务全家桶&#xff0c;在Spring Cloud项目中孵化成为Spring Cloud的子项目。第一代的Spring Cloud标准中很多组件已经停更,如&#xff1a;Eureak,zuul等。所以Sprin…

【MySQL】数据库中的内置函数

W...Y的主页 &#x1f60a; 代码仓库分享 &#x1f495; 目录 函数 日期函数 字符串函数 数学函数 ​编辑 其它函数 MySQL数据库提供了大量的内置函数&#xff0c;这些函数可以帮助你执行各种操作&#xff0c;如字符串处理、数值计算、日期和时间处理等&#xff01; 函数…