好程序员Java分享Mybatis必会的动态SQL,前言:java
Mybatis可谓是java开发者必须会的一项技能。MyBatis 的强大特性之一即是它的动态 SQL。若是你有使用 JDBC 或其它相似框架的经验,你就能体会到根据不一样条件拼接 SQL 语句的痛苦。例如拼接时要确保不能忘记添加必要的空格,还要注意去掉列表最后一个列名的逗号。利用动态 SQL 这一特性能够完全摆脱这种痛苦。程序员
Mybatis动态sql
mybatis 动态SQL,经过 if, choose, when, otherwise, trim, where, set, foreach等标签,可组合成很是灵活的SQL语句,从而在提升 SQL 语句的准确性的同时,也大大提升了开发人员的效率。本文主要介绍这几个动态SQL.sql
具体示例
if标签 if就是用来对输入映射的字段进行判断 通常是非空判断 null 和""。数组
- <!-- 案例1:动态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>
动态SQL <where /> 至关于 where关键字 <where />能够自动处理第一个前and 或者or。 当条件都没有的时候 where也不会加上 。mybatis
- <!-- 案例2:动态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>
choose—when--when--otherwise when能够多个 otherwise只能有一个 相似于 switch case。框架
需求:输入用户id 按照用户id进行精确查找 其余条件不看 没有输入 id 用户名模糊查找 都没有的话 查询id=1的用户ide
- <!-- 案例3:动态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>
动态sql之set 代替set关键字 set标签能够帮助咱们去掉最后一个逗号ui
- <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>
Trim,trim代替wherespa
- <!-- 案例5:动态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>
Trim代替set:对象
- <!-- 案例6:动态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>
-
Foreach来遍历集合
- <!--案例7:动态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>
总结
熟练掌握以上Mysql的动态SQL,咱们能够更驾轻就熟的完成个人功能,写更少的代码,实现更多的功能。