当前位置: 首页 > news >正文

【SSM】MyBatis(十.动态sql)

文章目录

  • 1.if
  • 2.where
  • 3.trim
  • 4.set
  • 5. choose when otherwise
  • 6.foreach
    • 6.1 批量删除
    • 6.2 批量增加
  • 7.sql

1.if

    <select id="selectByMultiCondition" resultType="Car">select * from t_car where 1 = 1<if test="brand != null and brand != ''">and brand like "%"#{brand}"%"</if><if test="guidePrice != null and guidePrice != ''">and guide_price > #{guidePrice}</if><if test="carType != null and carType != ''">and car_type = #{carType}</if></select>

2.where

where标签的作用:让where子句更加动态智能。
● 所有条件都为空时,where标签保证不会生成where子句。
● 自动去除某些条件前面多余的and或or。

    <select id="selectByMultiConditionWithWhere" resultType="Car">select * from t_car<where><if test="brand != null and brand != ''">and brand like "%"#{brand}"%"</if><if test="guidePrice != null and guidePrice != ''">and guide_price > #{guidePrice}</if><if test="carType != null and carType != ''">and car_type = #{carType}</if></where></select>

3.trim

trim标签的属性:
● prefix:在trim标签中的语句前添加内容
● suffix:在trim标签中的语句后添加内容
● prefixOverrides:前缀覆盖掉(去掉)
● suffixOverrides:后缀覆盖掉(去掉)

    <select id="selectByMultiConditionWithTrim" resultType="Car">select * from t_car<trim prefix="where" suffixOverrides="and|or"><if test="brand != null and brand != ''">brand like "%"#{brand}"%" and</if><if test="guidePrice != null and guidePrice != ''">guide_price > #{guidePrice} and</if><if test="carType != null and carType != ''">car_type = #{carType}</if></trim></select>

4.set

主要使用在update语句当中,用来生成set关键字,同时去掉最后多余的“,”
比如我们只更新提交的不为空的字段,如果提交的数据是空或者"",那么这个字段我们将不更新。

    <update id="updateBySet" parameterType="long">update t_car<set><if test="carNum != null and carNum !=''">car_num = #{carNum},</if><if test="brand != null and brand !=''">brand = #{brand},</if><if test="guidePrice != null and guidePrice !=''">guide_price = #{guidePrice},</if><if test="produceTime != null and produceTime !=''">produce_time = #{produceTime},</if><if test="carType != null and carType !=''">car_type = #{carType}</if></set>whereid = #{id}</update>

5. choose when otherwise

<choose><when></when><when></when><when></when><otherwise></otherwise>
</choose>

6.foreach

6.1 批量删除

<!--
collection:集合或数组
item:集合或数组中的元素
separator:分隔符
open:foreach标签中所有内容的开始
close:foreach标签中所有内容的结束
--><delete id="deleteByIds">delete from t_car where id in<foreach collection="ids" item="id" separator="," open="(" close=")">#{id}</foreach></delete>
<delete id="deleteBatchByForeach2">delete from t_car where<foreach collection="ids" item="id" separator="or">id = #{id}</foreach>
</delete>

6.2 批量增加

    <insert id="insertBatch">insert into t_carvalues<foreach collection="cars" item="car" separator=",">(null, #{car.carNum}, #{car.brand}, #{car.guidePrice}, #{car.produceTime}, #{car.carType})</foreach></insert>

7.sql

sql标签用来声明sql片段
include标签用来将声明的sql片段包含到某个sql语句当中
作用:代码复用。易维护。

http://www.lryc.cn/news/44342.html

相关文章:

  • 最近很多人都在说 “前端已死”,讲讲我的看法
  • 大家好,我是火旺技术
  • 【Java并发编程系列】全方位理解多线程几乎包含线程的所有操作哦
  • 天宝S6测量机器人/天宝S6全站仪参数/教程/Trimble 天宝全站仪
  • c++基础知识汇总
  • 重磅!基于GPT-4的全新智能编程助手 GitHub Copilot X 来了!
  • 第04章_运算符
  • Excel 文件比较工具:xlCompare 11.0 Crack
  • 802.1x认证原理
  • GPIO的八种模式分析
  • 携职教育:财会人常用必备,203个EXCEL快捷键汇总
  • 【美赛】2023年ICM问题Z:奥运会的未来(思路、代码)
  • CSS基础入门
  • 可重入锁、读写锁、邮戳锁 详解
  • HBase客户端、服务器端、列簇设计、HDFS相关优化,HBase写性能优化切入点,写异常问题检查点
  • DINO-DETR在COCO缩减数据集上实验结果分析
  • 语聊房app源码及架构设计
  • 什么是软件测试?5分钟带你快速了解!
  • [3D游戏开发实践] Cocos Cyberpunk 源码解读-手把手教你新增一个后效Shader
  • 构建产品帮助中心,促进SaaS企业的进步
  • 【Qt】Qt单元测试详解(四):Google Test
  • 容器引擎Docker的常用命令
  • vue尚品汇商城项目-day01【3.项目路由的分析】
  • 详解--高级IO
  • Android自定义闹钟
  • 第02章_MySQL环境搭建
  • java使用线程池和Future接口实现异步的实例
  • cocosCreator 事件系统
  • 刷题_20:字符串反转 and 公共子串计算
  • 如何在 Linux 命令行中比较两个目录,我教你五个命令!