网站首页 > 博客文章 正文
动态sql
通过mybatis提供的各种标签方法实现动态拼接sql。
1.1 if标签 if就是用来对输入映射的字段进行判断 一般是非空判断 null ""
<!-- 案例3:动态sql之if --> <select id="selectUsersIf" parameterType="user" resultType="user"> select * from users where 1=1 <if test="uname!=null and uname!=''"> and uname like "%"#{uname}"%" </if> <if test="sex!=null and sex!=''">and sex = #{sex} </if> </select>
1.2动态SQL <where /> 相当于 where关键字 <where />可以自动处理第一个前and 或者or。 当条件都没有的时候 where也不会加上 。
<!-- 案例四:动态sql之where 可以自动处理第一个前and 或者or。当条件都没有的时候 where也不会加上 --> <select id="selectUsersWhere" parameterType="user" resultType="user"> select * from users <where> <if test="uname!=null and uname!=''"> and uname like "%"#{uname}"%" </if> <if test="sex!=null and sex!=''">and sex = #{sex} </if> </where> </select>
1.3choose—when--when--otherwise when可以多个 otherwise只能有一个 类似于 switch case
需求:输入用户id 按照用户id进行精确查找 其他条件不看 没有输入 id 用户名模糊查找 都没有的话 查询id=1的用户 <!-- 案例5:动态sql之choose—when when otherwise --> ? <select id="selectUsersChoose" parameterType="user" resultType="user"> select * from users <where> <choose> <when test="uid!=null"> uid=#{uid}</when> <when test="uname!=null and uname!=''"> uname like "%"#{uname}"%"</when> <otherwise>uid=1</otherwise> </choose> </where> </select>
1.4 动态sql之set 代替set关键字 set标签可以帮助我们去掉最后一个逗号
<update id="updateSet" parameterType="user"> update users <set> <if test="uname!=null and uname!=''"> uname =#{uname},</if> <if test="upwd!=null and upwd!=''"> upwd =#{upwd},</if> <if test="sex!=null and sex!=''"> sex =#{sex},</if> <if test="birthday!=null"> birthday =#{birthday},</if> </set> where uid=#{uid} </update>
1.5 trim
1.trim代替where
<!-- 案例7:动态sql之trim 代替where -->
<select id="selectUsersTrimWhere" parameterType="user" resultType="user">
select * from users
<!--
prefix:指添加前缀修饰
suffix:添加后缀修饰
prefixOverrides:去掉前缀修饰
suffixOverrides:去掉后缀修饰
-->
<trim prefix="where" prefixOverrides="and|or" >
<if test="uname!=null and uname!=''"> and uname like "%"#{uname}"%" </if>
<if test="sex!=null and sex!=''">and sex = #{sex} </if>
</trim>
</select>
2.代替set
<!-- 案例8:动态sql之trim 代替set -->
<update id="updateTrimSet" parameterType="user">
update users
<trim prefix="set" suffixOverrides="," suffix="where uid=#{uid}" >
<if test="uname!=null and uname!=''"> uname =#{uname},</if>
<if test="upwd!=null and upwd!=''"> upwd =#{upwd},</if>
<if test="sex!=null and sex!=''"> sex =#{sex},</if>
<if test="birthday!=null"> birthday =#{birthday},</if>
</trim>
</update>
1.6动态sql foreach
用来进行遍历 数组 List
1.用来进行遍历
<!--案例9-1:动态sql之foreach 遍历数组 -->
<select id="selectUsersForeachArray" resultType="user">
select * from users where uid in
<!--
collection:要遍历的集合
item:当前正在遍历的对象的变量名
open:开始遍历
close:结束便利
index:下标
separator:分割
-->
<foreach collection="array" item="item" open="(" close=")" index="index" separator=",">
#{item}
</foreach>
</select>
2.用来遍历集合 批量删除
<!--案例9-2:动态sql之foreach 遍历List 批量删除 -->
<delete id="deleteUsersForeachList">
<if test="l!=null and l.size()>0">
delete from users where uid in
<foreach collection="l" item="item" open="(" close=")" index="index" separator=",">
#{item}
</foreach>
</if>
</delete>
1.7 sql片段
Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的,如下:
<!-- 定义一个sql片段 -->
<sql id="users_select_column">
select uid,uname,upwd,sex,birthday
</sql>
引用: <include refid="users_select_column"></include>
- 上一篇: 工作表查询时,结合数组,实现分工作表的数据归类汇总
- 下一篇: 如何通过执行SQL为低代码项目提速?
猜你喜欢
- 2024-10-04 TP5 where数组查询(模糊查询--多个查询条件)
- 2024-10-04 sql基础(六)(sql基本知识点)
- 2024-10-04 必知的php数组函数(必知的php数组函数有哪些)
- 2024-10-04 C# 数据操作系列 - 16 SqlSugar 完结篇(最后的精华)
- 2024-10-04 会SQL语句,就能快速开放你的数据接口API
- 2024-10-04 VBA数组与字典解决方案第46讲:进行数据的模糊分类汇总
- 2024-10-04 sqlite3 支持JSON(Sqlite3 支持网络访问)
- 2024-10-04 HeidiSQL 免费的可视化数据库管理工具
- 2024-10-04 实例讲解MyBatisPlus自定义sql注入器方法
- 2024-10-04 【实用技能】Seacms 8.7版本SQL注入分析
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- ifneq (61)
- 字符串长度在线 (61)
- googlecloud (64)
- messagesource (56)
- promise.race (63)
- 2019cad序列号和密钥激活码 (62)
- window.performance (66)
- qt删除文件夹 (72)
- mysqlcaching_sha2_password (64)
- ubuntu升级gcc (58)
- nacos启动失败 (64)
- ssh-add (70)
- jwt漏洞 (58)
- macos14下载 (58)
- yarnnode (62)
- abstractqueuedsynchronizer (64)
- source~/.bashrc没有那个文件或目录 (65)
- springboot整合activiti工作流 (70)
- jmeter插件下载 (61)
- 抓包分析 (60)
- idea创建mavenweb项目 (65)
- vue回到顶部 (57)
- qcombobox样式表 (68)
- tomcatundertow (58)
- pastemac (61)
本文暂时没有评论,来添加一个吧(●'◡'●)