网站首页 > 博客文章 正文
ybatis的动态SQL执行原理已经在前面的回答中进行了解释。
下面是使用Mybatis动态SQL的示例,每个示例都有具体的代码说明:
使用<if>标签进行条件判断:
<select id="getUserByNameAndAge" resultType="User">
SELECT * FROM users
WHERE 1=1
<if test="name != null">
AND name = #{name}
</if>
<if test="age != null">
AND age = #{age}
</if>
</select>
使用<choose>、<when>和<otherwise>标签进行多条件判断:
<select id="getUserByCondition" resultType="User">
SELECT * FROM users
<choose>
<when test="name != null">
WHERE name = #{name}
</when>
<when test="age != null">
WHERE age = #{age}
</when>
<otherwise>
WHERE 1=1
</otherwise>
</choose>
</select>
使用<trim>标签来去除或添加一些SQL语句片段:
<update id="updateUser" parameterType="User">
UPDATE users
<trim prefix="SET" suffixOverrides=",">
<if test="name != null">
name = #{name},
</if>
<if test="age != null">
age = #{age},
</if>
</trim>
WHERE id = #{id}
</update>
使用<where>标签来动态生成WHERE条件:
<select id="getUserByCondition" resultType="User">
SELECT * FROM users
<where>
<if test="name != null">
AND name = #{name}
</if>
<if test="age != null">
AND age = #{age}
</if>
</where>
</select>
使用<set>标签来动态生成UPDATE语句的SET部分:
<update id="updateUser" parameterType="User">
UPDATE users
<set>
<if test="name != null">
name = #{name},
</if>
<if test="age != null">
age = #{age},
</if>
</set>
WHERE id = #{id}
</update>
使用<foreach>标签来动态生成IN语句的参数:
<select id="getUsersByIds" resultType="User">
SELECT * FROM users
WHERE id IN
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</select>
使用${}来动态替换变量:
<select id="getUserByName" resultType="User">
SELECT * FROM users
WHERE name = '${name}'
</select>
使用<bind>标签来定义临时变量:
<select id="getUsersByName" resultType="User">
<bind name="likeName" value="'%' + name + '%'" />
SELECT * FROM users
WHERE name LIKE #{likeName}
</select>
使用<sql>标签定义可重用的SQL片段:
<sql id="userColumns">id, name, age</sql>
<select id="getUser" resultType="User">
SELECT <include refid="userColumns" /> FROM users
WHERE id = #{id}
</select>
使用<script>标签将多个SQL语句拼接在一起:
<select id="getUserByCondition" resultType="User">
<script>
SELECT * FROM users
WHERE 1=1
<if test="name != null">
AND name = #{name}
</if>
<if test="age != null">
AND age = #{age}
</if>
</script>
</select>
以上是10个使用Mybatis动态SQL的示例,每个示例都展示了不同的用法和场景。希望这些示例能够帮助你理解Mybatis动态SQL的使用方法和用途。如果你需要进一步的解释或有其他问题,请随时提问。
猜你喜欢
- 2025-06-03 程序员简历例句—范例Java、Python、C++模板
- 2025-06-03 SQLREST工具的功能概述及使用指南
- 2025-06-03 SpringBoot一个提升N倍性能的操作
- 2025-06-03 Jeecgboot3.2版-postgres脚本制作
- 2025-06-03 心心念念的前端代码生成利器,前后端一网打尽
- 2025-06-03 基于Spring+SpringMVC+Mybatis分布式敏捷开发系统架构(附源码)
- 2025-06-03 拒绝MyBatis慢查询!性能优化实战手册
- 2025-06-03 有了 SPL,看来用不着 ORM 了(spl使用)
- 2025-06-03 Gradle的学习(gradle入门到精通)
- 2025-06-03 SpringBoot整合MybatisPlus实现分页查询
你 发表评论:
欢迎- 07-08Google Cloud Platform 加入支持 Docker 的容器引擎
- 07-08日本KDDI与Google Cloud 签署合作备忘录,共探AI未来
- 07-08美国Infoblox与Google Cloud合作推出云原生网络和安全解决方案
- 07-08GoogleCloud为Spanner数据库引入HDD层,将冷存储成本降低80%
- 07-08谷歌推出Cloud Dataproc,缩短集群启动时间
- 07-08Infovista与Google Cloud携手推进射频网络规划革新
- 07-08比利时Odoo与Google Cloud建立增强合作,扩大全球影响力
- 07-08BT 和 Google Cloud 通过 Global Fabric 加速 AI 网络
- 最近发表
-
- Google Cloud Platform 加入支持 Docker 的容器引擎
- 日本KDDI与Google Cloud 签署合作备忘录,共探AI未来
- 美国Infoblox与Google Cloud合作推出云原生网络和安全解决方案
- GoogleCloud为Spanner数据库引入HDD层,将冷存储成本降低80%
- 谷歌推出Cloud Dataproc,缩短集群启动时间
- Infovista与Google Cloud携手推进射频网络规划革新
- 比利时Odoo与Google Cloud建立增强合作,扩大全球影响力
- BT 和 Google Cloud 通过 Global Fabric 加速 AI 网络
- NCSA和Google Cloud合作开发AI驱动的网络防御系统,加强泰国网络空间的安全性
- SAP将在沙特阿拉伯 Google Cloud 上推出BTP服务
- 标签列表
-
- 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)
本文暂时没有评论,来添加一个吧(●'◡'●)