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

第十二章MyBatis动态SQL

if标签与where标签

if标签

test如果为true就会拼接查询条件,否则不会

  • 当没有使用@Param,test出现arg0/param1
  • 当使用@Param,test为@Param指定的值
  • 当使用Pojo,test为对象的属性名
select * from car where
<if test="name!=null || name!='' ">name like concat('%',${name},'%')
</if>
<if test="price!=null || price!='' ">and price=#{price}
</if>
...

注意日期不能判断为空字符串

如何所有条件都不满足上述代码会报错

解决

如果用 where 1=1 后面判断必须加and

select * from car where 1=1
<if test="name!=null || name!='' ">and name like concat('%',${name},'%')
</if>
<if test="price!=null || price!='' ">and price=#{price}
</if>
...

where标签

  • 所有条件都为空时,where子句不会生成
  • 自动去掉前面多余的and,or
select * from car 
<where><if test="name!=null || name!='' ">name like concat('%',${name},'%')</if><if test="price!=null || price!='' ">and price=#{price}</if>
</where>
...

trim标签

  • prefix在标签前面动态的添加属性值
  • suffix在标签后面动态的添加属性值
  • suffixOverrides去除标签内容后面中指定的属性值
  • prefixOverrides去除标签内容前面中指定的属性值
select * from car 
<trim prefix="where" suffixOverrides="and | or" prefixOverrides="" suffix=""><if test="name!=null || name!='' ">name like concat('%',${name},'%')</if><if test="price!=null || price!='' ">and price=#{price}</if>
</trim>

set标签

  • 主要用在update标签中,只会提交不为空的条件
  • 可以动态去除语句中多余的,
update  car 
<set><if test="name!=null || name!='' ">name=#{name} , </if><if test="price!=null || price!='' ">price=#{price} ,</if>
</set>
where id=#{id}

choose when otherwise标签

一般在多条件中只执行某一个条件查询

用法类似与 if else if else

selecr * from car
<where><choose><when test="name!=null || name!='' ">name=#{name} </when><when test="price!=null || price!='' ">price=#{price} </when><otherwise>id=#{id} <otherwise></choose>
<where>

因为只会满足一种查询条件所有不需要加and


forEach标签

  • collection为循环列表
  • item为循环元素
  • separator为循环元素之间的分隔符
  • open为标签前面加属性值
  • close为标签后面加属性值

批量删除

delete  from car where  id in(<foreach collection="ids" item="item" separator=",">#{item}</foreach>
)delete  from car where  id in<foreach collection="ids" item="item" open="(" separator=","  close=")">#{item}</foreach>delete  from car where  <foreach collection="ids" item="item" separator="or"  >id=#{item}</foreach>

批量插入

insert into  car( name, price)
values<foreach collection="list" item="item" separator=",">(#{item.name},#{item.price})</foreach>

sql与include标签

主要用于字段的封装和复用

<sql id="CarSql">id,car_num as  carNum,brand,guide_price as guidePrice,produce_time as produceTime,car_type as carType
</sql><select id="selectAll" resultType="com.example.webapplication.pojo.Car">select<include refid="CarSql"></include>from car
</select>
http://www.lryc.cn/news/132494.html

相关文章:

  • redis--发布订阅
  • 链表2-两两交换链表中的节点删除链表的倒数第N个节点链表相交环形链表II
  • 数据结构之并查集
  • [element-ui] el-date-picker a-range-picker type=“daterange“ rules 校验
  • Dockers搭建个人网盘、私有仓库,Dockerfile制作Nginx、Lamp镜像
  • 2023 CCPC 华为云计算挑战赛 hdu7401 流量监控(树形dp)
  • 01.Django入门
  • 亿赛通电子文档安全管理系统任意文件上传漏洞(2023-HW)
  • docker限制容器日志大小
  • 底层驱动实现数码管显示温湿度数值功能
  • 03架构管理之测试管理
  • 30、devtools 依赖关于自动重启(自动加载页面)的知识
  • ES6 Promise/Async/Await使用
  • Word中对象方法(Methods)的理解及示例(上)
  • AutoDev 1.1.3 登场,个性化 AI 辅助:私有化大模型、自主设计 prompt、定义独特规则...
  • win11 python 调用edge调试过程
  • DS-排序回顾
  • clion软件ide的安装和环境配置@ubuntu
  • Cpp学习——类与对象3
  • 回归预测 | MATLAB实现PSO-RBF粒子群优化算法优化径向基函数神经网络多输入单输出回归预测(多指标,多图)
  • ahooks.js:一款强大的React Hooks库及其API使用教程(四)
  • FOSSASIA Summit 2023 - 开源亚洲行
  • QT 基本对话框
  • ​8th参考文献:[8]许少辉.乡村振兴战略下传统村落文化旅游设计[M]北京:中国建筑出版传媒,2022.
  • Azure静态网站托管
  • LeetCode 热题 100(五):54. 螺旋矩阵、234. 回文链表、21. 合并两个有序链表
  • 常用消息中间件介绍
  • 装饰器读取不到被装饰函数的参数-已解决
  • python爬虫爬取中关村在线电脑以及参数数据
  • chatGPT-对话爱因斯坦