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

Mybatis-动态SQL、 <if>、<where>

一.动态SQL

        1. 随着用户的输入或外部条件的变化而变化的SQL语句,我们称为动态SQL;

二. <if>

                判断条件是否成立,如果条件为true,则拼接SQL

<select id="pageList" resultType="com.wyyzs.pojo.Emp">select e.*, d.name deptName from emp e left join dept d on e.dept_id = d.id<where><if test="name != null and name != ''">and e.name like concat('%', #{name}, '%')</if><if test="gender != null">and e.gender = #{gender}</if><if test="begin != null">and e.entry_date >= #{begin}</if><if test="end != null">and e.entry_date &lt; #{end}</if></where>order by e.update_time desc</select>

三. <where>

        根据条件查询,来生成where关键字,并会自动去除条件前面多余的and或or,

        where标签只会智能的去除首个满足条件语句的前缀,所以建议在使用where标签时,每个语句都最好写上 and 前缀或者 or 前缀

<select id="pageList" resultType="com.wyyzs.pojo.Emp">select e.*, d.name deptName from emp e left join dept d on e.dept_id = d.id<where><if test="name != null and name != ''">and e.name like concat('%', #{name}, '%')</if><if test="gender != null">and e.gender = #{gender}</if><if test="begin != null">and e.entry_date >= #{begin}</if><if test="end != null">and e.entry_date &lt; #{end}</if></where>order by e.update_time desc</select>

四. <foreach>

        1.属性说明

                (1) collection:集合名称

                (2) item:集合遍历出来的元素/项 

                (3) separator:每一次遍历使用的分隔符

                (4) open:遍历开始前拼接的片段,foreach 代码的开始符号,一般是 ”(“,和 close=“)” 合用。常用在 in(),values() 时。该参数可选

                (5) close:遍历结束后拼接的片段,foreach 代码的关闭符号,一般是 ”)“,和 open=“(” 合用。常用在 in(),values()时。该参数可选。

                (6) index:在 list 和数组中,index 是元素序号;在 map 中,index 是元素 key。该参数可选 

  <insert id="batchAddEmpExpr">insert into emp_expr(emp_id, begin, end, company, job) values<foreach collection="exprList" item="item" separator=",">(#{item.empId}, #{item.begin}, #{item.end}, #{item.company}, #{item.job})</foreach></insert>

相关文章:

  • UE5 仿 鸣潮人物渲染
  • React-router 多类型历史记录栈
  • react小白学习快速上手
  • Django中间件讲解
  • Python小酷库系列:Python中的JSON工具库(2)
  • CQF预备知识:Python相关库 -- 准蒙特卡洛方法 scipy.stats
  • MySQL常用函数详解之数值函数
  • 【FastAPI高级实战】结合查询参数与SQLModel Joins实现高效多表查询(分页、过滤、计数)
  • 用AI配合MCP快速生成n8n工作流
  • 本地访问远程vps中的sqlite数据库中的内容之(二)使用Python和web访问远程sqlite
  • Go语言2个协程交替打印
  • 使用Netlify进行简单部署
  • Git+Jenkins-Docker搭建企业级CI/CD平台
  • 基于OpenManus的跨平台部署方案及远程访问安全机制
  • CSS 第四天 复合选择器、CSS特性、背景属性、显示模式
  • P6 QT项目----汽车仪表盘(6.2)
  • 原型模式Prototype Pattern
  • 第二十九场 蓝桥算法赛
  • 华为OD机试_2025 B卷_运维日志排序(Python,100分)(附详细解题思路)
  • 136. 只出现一次的数字
  • 桂林 网站建设/海外推广运营
  • wordpress调用用户数据库/成都seo
  • 如何快速做h5网站/武汉seo首页优化技巧
  • 家政行业网站建设方案/培训方案怎么做
  • 越秀高端网站建设/谷歌优化教程
  • 免费全能网站空间/今天的特大新闻有哪些