Mybatis(八)-一动态SQL

动态SQL

有时候,静态的SQL语句并不能满足应用程序的需求。我们可以根据一些条件,来动态地创建SQL语句。
例如,在web应用程序中,有可能有一些搜索界面,需要输入一个或多个选项,然后根据这些已经选择的条件去执行检索操作。实现这种类型的搜索功能我们需要动态的构建SQL语句。如果用户提供了任何输入条件,我们需要将那个条件添加到SQL语句的WHERE子句中。
  MyBatis 通过使用<if>,<choose>,<where>,<foreach>,<trim>元素提供了对构造动态 SQL 语句的高级别支持。

IF条件

  <if>元素被用来有条件地嵌入SQL片段,如果测试条件被赋值为true,则相应地SQL片段会被添加到SQL语句中。例子如下:
<resultMap type="Course" id="CourseResult">
<id column="course_id" property="courseId" />
<result column="name" property="name" />
<result column="description" property="description" />
<result column="start_date" property="startDate" />
<result column="end_date" property="endDate" />
</resultMap>
<select id="searchCourses" parameterType="hashmap" resultMap="CourseResult"></select>
SELECT * FROM COURSES
WHERE TUTOR_ID= #{tutorId}
<if test="courseName != null">
AND NAME LIKE #{courseName}
</if>
<if test="startDate != null">
AND START_DATE >= #{startDate}
</if>
<if test="endDate != null">
AND END_DATE <= #{endDate}
</if>
</select>
在这里注意是使用OGNL表达式来构建动态SQL语句。

choose,when和otherwise条件例子如下
<span style="font-weight: normal;"><select id="searchCourses" parameterType="hashmap" resultMap="CourseResult">
SELECT * FROM COURSES
<choose>
<when test="searchBy == 'Tutor'">
WHERE TUTOR_ID= #{tutorId}
</when>
<when test="searchBy == 'CourseName'">
WHERE name like #{courseName}
</when>
<otherwise>
WHERE TUTOR start_date >= now()
</otherwise>
</choose>
</select></span>

where条件

有些时候我们查询条件可能没有要是写了Where可能就会出现问题所以这里有一个where标签问题:
  <where>元素只有在其内部标签有返回内容时才会在动态语句上插入 WHERE 条件语句。 并且,如果 WHERE 子句以AND 或者 OR 打头,则打头的 AND OR 将会被移除。
   如果 tutor_id 参数值为 null,并且 courseName 参数值不为 null,则<where>标签会将 AND name like#{courseName} 中的 AND 移除掉,生成的 SQL WHERE 子句为:where name like #{courseName}

<select id="searchCourses" parameterType="hashmap"
resultMap="CourseResult">
SELECT * FROM COURSES
<where>
<if test=" tutorId != null ">
TUTOR_ID= #{tutorId}
</if>
<if test="courseName != null">
AND name like #{courseName}
</if>
<if test="startDate != null">
AND start_date >= #{startDate}
</if>
<if test="endDate != null">
AND end_date <= #{endDate}
</if>
</where>
</select>

<trim>条件

  <trim>元素和<where>元素类似,但是<trim>提供了在添加前缀/后缀 或者 移除前缀/后缀方面提供更大的灵活性。
   
<span style="font-weight: normal;"><select id="searchCourses" parameterType="hashmap"
resultMap="CourseResult">
SELECT * FROM COURSES
<trim prefix="WHERE" prefixOverrides="AND | OR">
<if test=" tutorId != null ">
71
6 7 8 9
10
11
12
TUTOR_ID= #{tutorId}
</if>
<if test="courseName != null">
AND name like #{courseName}
</if>
</trim>
</select></span>

这里如果任意一个 <if>条件为 true<trim>元素会插入 WHERE,并且移除紧跟 WHERE 后面的 AND OR

foreach循环

 另外一个强大的动态 SQL 语句构造标签即是<foreach>。它可以迭代遍历一个数组或者列表,构造 AND/OR 条件或一个 IN 子句。

<select id="searchCoursesByTutors" parameterType="map"
resultMap="CourseResult">
SELECT * FROM COURSES
<if test="tutorIds != null">
<where>
<foreach item="tutorId" collection="tutorIds">
OR tutor_id=#{tutorId}
</foreach>
</where>
</if>
</select>

现在让我们来看一下怎样使用<foreach>生成 IN 子句:
<span style="font-weight: normal;"><span style="font-size:10px;"><select id="searchCoursesByTutors" parameterType="map"
resultMap="CourseResult">
SELECT * FROM COURSES
<if test="tutorIds != null">
<where>
tutor_id IN
<foreach item="tutorId" collection="tutorIds"
open="(" separator="," close=")">
#{tutorId}
</foreach>
</where>
</if>
</select></span></span>

set条件

<set>元素和<where>元素类似,如果其内部条件判断有任何内容返回时,他会插入 SET SQL 片段。

<span style="font-size:10px;"><update id="updateStudent" parameterType="Student">
update students
<set>
<if test="name != null">name=#{name},</if>
<if test="email != null">email=#{email},</if>
<if test="phone != null">phone=#{phone},</if>
</set>
where stud_id=#{id}
</update></span>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值