Mybatis(八)-一动态SQL

来源:互联网 发布:蓝韵网络 编辑:程序博客网 时间:2024/06/11 00:24

动态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 COURSESWHERE 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 ">716 7 8 9101112TUTOR_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>



0 0
原创粉丝点击