sb.append(" and pay="+search_pay);
}
String sql = sb.toString();
System.out.println(“SQL语句是:”+sql);
通常使用动态 SQL 不可能是独立的一部分,MyBatis 当然使用一种强大的动态 SQL 语言来改进这种情形,这种语言可以被用在任意的 SQL 映射语句中。
动态 SQL 元素和使用 JSTL 或其他类似基于 XML 的文本处理器相似。在 MyBatis 之前的版本中,有很多的元素需要来了解。MyBatis 3 大大提升了它们,现在用不到原先一半的元素就可以了。MyBatis 采用功能强大的基于 OGNL(Struts2语法) 的表达式来消除其他元素。
mybatis 的动态sql语句是基于OGNL表达式的。可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:
* **if 语句** (简单的条件判断)
* **choose (when,otherwise)** ,相当于java 语言中的 switch ,与 jstl 中的choose 很类似.
* **trim** (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀)
* **where** (主要是用来简化sql语句中where条件判断的,能智能的处理 and or ,不必担心多余导致语法错误)
* **set** (主要用于更新时)
* **foreach** (在实现 mybatis in 语句查询时特别有用)
### 2.分支判断
#### 2.1 if元素
根据 username 和 sex 来查询数据。如果username为空,那么将只根据sex来查询;反之只根据username来查询
首先不使用 动态SQL 来书写
select * from user where username=#{username} and sex=#{sex}
上面的查询语句,我们可以发现,如果 #{username} 为空,那么查询结果也是空,如何解决这个问题呢?使用 if 来判断
<select id="selectUserByUsernameAndSex" resultType="user"
parameterType=“User”>
select * from user where
username=#{username}
and sex=#{sex}
这样写我们可以看到,如果 sex 等于 null,那么查询语句为 select \* from user where username=#{username},但是如果usename 为空呢?那么查询语句为 select \* from user where and sex=#{sex},这是错误的 SQL 语句,如何解决呢?请看下面的 where 语句
#### 2.2 动态SQL:if+where 语句
select * from user
username=#{username}
and sex=#{sex}
这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。
#### 2.3 动态SQL:if+set 语句
同理,上面的对于查询 SQL 语句包含 where 关键字,如果在进行更新操作的时候,含有 set 关键词,我们怎么处理呢?
<update id="updateUserById" parameterType="User">
update user u
<set>
<if test="username != null and username != ''">
u.username = #{username},
</if>
<if test="sex != null and sex != ''">
u.sex = #{sex},
</if>
</set>
where id=#{id}
</update>
这样写,如果第一个条件 username 为空,那么 sql 语句为:update user u set u.sex=? where id=?
如果第一个条件不为空,那么 sql 语句为:update user u set u.username = ? ,u.sex = ? where id=?
如果出现多余逗号 会自动去掉!!
#### 2.4 动态SQL:choose(when,otherwise) 语句 了解
有时候,我们不想用到所有的查询条件,只想选择其中的一个,查询条件有一个满足即可,使用 choose 标签可以解决此类问题,类似于 Java 的 switch 语句
<select id="selectUserByChoose" resultType="User" parameterType="User">
select * from user
<where>
<choose>
<when test="id !='' and id != null">
id=#{id}
</when>
<when test="username !='' and username != null">
and username=#{username}
</when>
<otherwise>
and sex=#{sex}
</otherwise>
</choose>
</where>
</select>
也就是说,这里我们有三个条件,id,username,sex,只能选择一个作为查询条件
如果 id 不为空,那么查询语句为:select \* from user where id=?
如果 id 为空,那么看username 是否为空,如果不为空,那么语句为 select \* from user where username=?;
如果 username 为空,那么查询语句为 select \* from user where sex=?
#### 2.5 动态SQL:trim 语句 了解
trim标记是一个格式化的标记,可以完成set或者是where标记的功能
①、用 trim 改写上面第二点的 if+where 语句
<select id="selectUserByUsernameAndSex" resultType="user" parameterType="User">
select * from user
<trim prefix="where" prefixOverrides="and | or">
<if test="username != null">
and username=#{username}
</if>
<if test="sex != null">
and sex=#{sex}
</if>
</trim>
</select>
prefix:前缀
prefixoverride:去掉第一个and或者是or
②、用 trim 改写上面第三点的 if+set 语句
<!-- 根据 id 更新 user 表的数据 -->
<update id="updateUserById" parameterType="User">
update user u
<trim prefix="set" suffixOverrides=",">
<if test="username != null and username != ''">
u.username = #{username},
</if>
<if test="sex != null and sex != ''">
u.sex = #{sex},
</if>
</trim>
where id=#{id}
</update>
suffix:后缀
suffixoverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)
#### 2.6 动态SQL: SQL 片段
有时候可能某个 sql 语句我们用的特别多,为了增加代码的重用性,简化代码,我们需要将这些代码抽取出来,然后使用时直接调用。
比如:假如我们需要经常根据用户名和性别来进行联合查询,那么我们就把这个代码抽取出来,如下:
<sql id="querySQL">
<if test="username!=null and username!=''">
and username like #{username}
</if>
<if test="sex!=null and sex!=''">
and sex=#{sex}
</if>
</sql>
引用 sql 片段
select * from user where 1=1
<select id="selectUserByUsernameAndSex1" resultType="user" parameterType="User">
select * from user
<!--自动的去掉多余的 And 或者 OR -->
<where>
<!--引入SQL片段-->
<include refid="querySQL"/>
</where>
</select>
<select id="selectUserByUsernameAndSex2" resultType="user" parameterType="User">
select * from user
<!--去掉前面多余AND或者OR-->
<trim prefix="WHERE" prefixOverrides="AND|OR">
<!--引入SQL片段-->
<include refid="querySQL"/>
</trim>
</select>
### 最后的话
最近很多小伙伴找我要Linux学习资料,于是我翻箱倒柜,整理了一些优质资源,涵盖视频、电子书、PPT等共享给大家!
### 资料预览
给大家整理的视频资料:

给大家整理的电子书资料:

**如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!**
最后的话
最近很多小伙伴找我要Linux学习资料,于是我翻箱倒柜,整理了一些优质资源,涵盖视频、电子书、PPT等共享给大家!
资料预览
给大家整理的视频资料:
[外链图片转存中…(img-NxkECSSi-1726075275379)]
给大家整理的电子书资料:
[外链图片转存中…(img-aF1X5Ifb-1726075275380)]
如果本文对你有帮助,欢迎点赞、收藏、转发给朋友,让我有持续创作的动力!