网站首页 > 博客文章 正文
好程序员Java分享Mybatis必会的动态SQL,前言
Mybatis可谓是java开发者必须会的一项技能。MyBatis 的强大特性之一便是它的动态 SQL。如果你有使用 JDBC 或其它类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句的痛苦。例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL 这一特性可以彻底摆脱这种痛苦。
Mybatis动态sql
mybatis 动态SQL,通过 if, choose, when, otherwise, trim, where, set, foreach等标签,可组合成非常灵活的SQL语句,从而在提高 SQL 语句的准确性的同时,也大大提高了开发人员的效率。本文主要介绍这几个动态SQL.
具体示例
if标签 if就是用来对输入映射的字段进行判断 一般是非空判断 null 和""。
1. <!-- 案例1:动态sql之if -->
2. <select id="selectUsersIf" parameterType="user" resultType="user">
3. select * from users where 1=1
4. <if test="uname!=null and uname!=''"> and uname like "%"#{uname}"%" </if>
5. <if test="sex!=null and sex!=''">and sex = #{sex} </if>
6. </select>
动态SQL <where /> 相当于 where关键字 <where />可以自动处理第一个前and 或者or。 当条件都没有的时候 where也不会加上 。
1. <!-- 案例2:动态sql之where 可以自动处理第一个前and 或者or。当条件都没有的时候 where也不会加上 -->
2. <select id="selectUsersWhere" parameterType="user" resultType="user">
3. select * from users
4. <where>
5. <if test="uname!=null and uname!=''"> and uname like "%"#{uname}"%" </if>
6. <if test="sex!=null and sex!=''">and sex = #{sex} </if>
7. </where>
choose—when--when--otherwise when可以多个 otherwise只能有一个 类似于 switch case。
需求:输入用户id 按照用户id进行精确查找 其他条件不看 没有输入 id 用户名模糊查找 都没有的话 查询id=1的用户
1. <!-- 案例3:动态sql之choose—when when otherwise -->
2.
3. <select id="selectUsersChoose" parameterType="user" resultType="user">
4. select * from users
5. <where>
6. <choose>
7. <when test="uid!=null"> uid=#{uid}</when>
8. <when test="uname!=null and uname!=''"> uname like "%"#{uname}"%"</when>
9. <otherwise>uid=1</otherwise>
10.
11. </choose>
12. </where>
13. </select>
动态sql之set 代替set关键字 set标签可以帮助我们去掉最后一个逗号
1. <update id="updateSet" parameterType="user">
2.
3. update users
4.
5. <set>
6. <if test="uname!=null and uname!=''"> uname =#{uname},</if>
7. <if test="upwd!=null and upwd!=''"> upwd =#{upwd},</if>
8. <if test="sex!=null and sex!=''"> sex =#{sex},</if>
9. <if test="birthday!=null"> birthday =#{birthday},</if>
10. </set>
11. where uid=#{uid}
12. </update>
Trim,trim代替where
1. <!-- 案例5:动态sql之trim 代替where -->
2. <select id="selectUsersTrimWhere" parameterType="user" resultType="user">
3. select * from users
4. <!--
5. prefix:指添加前缀修饰
6. suffix:添加后缀修饰
7. prefixOverrides:去掉前缀修饰
8. suffixOverrides:去掉后缀修饰
9. -->
10. <trim prefix="where" prefixOverrides="and|or" >
11. <if test="uname!=null and uname!=''"> and uname like "%"#{uname}"%" </if>
12. <if test="sex!=null and sex!=''">and sex = #{sex} </if>
13. </trim>
14. </select>
Trim代替set:
1. <!-- 案例6:动态sql之trim 代替set -->
2. <update id="updateTrimSet" parameterType="user">
3.
4. update users
5.
6. <trim prefix="set" suffixOverrides="," suffix="where uid=#{uid}" >
7. <if test="uname!=null and uname!=''"> uname =#{uname},</if>
8. <if test="upwd!=null and upwd!=''"> upwd =#{upwd},</if>
9. <if test="sex!=null and sex!=''"> sex =#{sex},</if>
10. <if test="birthday!=null"> birthday =#{birthday},</if>
11. </trim>
12.
Foreach来遍历集合
1. <!--案例7:动态sql之foreach 遍历数组 -->
2. <select id="selectUsersForeachArray" resultType="user">
3. select * from users where uid in
4. <!--
5. collection:要遍历的集合
6. item:当前正在遍历的对象的变量名
7. open:开始遍历
8. close:结束便利
9. index:下标
10. separator:分割
11. -->
12. <foreach collection="array" item="item" open="(" close=")" index="index" separator=",">
13. #{item}
14. </foreach>
15.
16. </select>
总结
熟练掌握以上Mysql的动态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)
本文暂时没有评论,来添加一个吧(●'◡'●)