MyBatis通过使用<if>,<choose>,<where>,<foreach>,<trim>元素提供了对构造动态SQL语句的高级别支持。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lit.chapter_2.mapper.CourseMapper">
<resultMap id="CourseResult" type="Course">
<id column="cou_id" property="couId" />
<result column="tea_id" property="teaId" />
<result column="cou_name" property="couName" />
<result column="start_dt" property="startDt" />
<result column="end_dt" property="endDt" />
</resultMap>
<!-- if元素被用来有条件地嵌入SQL片段,
如果测试条件被赋值为true,则相应地SQL片段将会被添加到SQL语句中。 -->
<select id="getCourseByDymic" parameterType="hashmap" resultMap="CourseResult">
select *
from course
where tea_id=#{id}
<if test="courseName!=null">and cou_name like #{courseName}</if>
<if test="startDt!=null">and start_dt >= #{startDt}</if>
<if test="endDt!=null"> and end_date <![CDATA[<= ]]> #{endDate}</if>
</select>
<!-- MyBatis计算choose测试条件的值,
且使用第一个值为TRUE的子句。如果没有条件为true,则使用otherwise内的子句 -->
<select id="searchCourseByChoose" parameterType="hashmap" resultMap="CourseResult">
select *
from course
<choose>
<when test="searchBy == 'teaId'">where tea_id = #{id}</when>
<when test="searchBy == 'courseName'">where cou_name like #{courseName}</when>
<otherwise>where start_dt >= #{startDt}</otherwise>
</choose>
</select>
<!-- where元素只有在其内部标签有返回内容时才会在动态语句上插入WHERE条件语句。
并且,如果WHERE子句以AND或者OR打头,则打头的AND或OR将会被移除。
如果tutor_id参数值为null,并且courseName参数值不为null,
则where标签会将AND name like#{courseName} 中的AND移除掉 -->
<select id="searchCourseByWhere" parameterType="hashmap" resultMap="CourseResult">
select *
from course
<where>
<if test="id!=null">tea_id = #{id}</if>
<if test="courseName!=null">and cou_name like #{courseName}</if>
<if test="startDt!=null">and start_dt >= #{startDt}</if>
<if test="endDt!=null"> and end_dt <![CDATA[<= ]]> #{endDt}</if>
</where>
</select>
<!-- trim元素和where元素类似,但是trim提供了在添加前缀/后缀 或者移除前缀/后缀方面提供更大的灵活性。
如果任意一个if条件为true,trim元素会插入WHERE,并且移除紧跟WHERE后面的AND或OR。-->
<select id="searchCourseByTrim" parameterType="hashmap" resultMap="CourseResult">
select *
from course
<trim prefix="where" prefixOverrides="and|or">
<if test="id!=null">tea_id = #{id}</if>
<if test="courseName!=null">and cou_name like #{courseName}</if>
</trim>
</select>
<!-- foreach 可以迭代遍历一个数组或者列表,构造AND/OR条件或一个IN子句 -->
<select id="searchCourseByForeach" parameterType="hashmap" resultMap="CourseResult">
select *
from course
<if test="teaIds!=null">
<trim prefix="where tea_id in(" suffixOverrides="," suffix=")">
<foreach item="teaId" collection="teaIds">
#{teaId},
</foreach>
</trim>
</if>
</select>
<!-- 使用foreach生成 IN子句 -->
<select id="searchCourseByForeach1" parameterType="hashmap" resultMap="CourseResult">
select *
from course
<if test="teaIds!=null">
<where>
tea_id in
<foreach item="teaId" collection="teaIds" open="(" separator="," close=")">
#{teaId}
</foreach>
</where>
</if>
</select>
<select id="searchCourseByForeach2" parameterType="hashmap" resultMap="CourseResult">
select *
from course
<if test="teaIds!=null">
<where>
<foreach item="teaId" collection="teaIds">
or tea_id = #{teaId}
</foreach>
</where>
</if>
</select>
<!-- set元素和where元素类似,如果其内部条件判断有任何内容返回时,他会插入SET SQL片段 -->
<update id="updateCourse" parameterType="Course">
update course
<set>
<if test="teaId!=null">tea_id = #{teaId},</if>
<if test="couName!=null">cou_name = #{couName},</if>
<if test="startDt!=null">start_dt = #{startDt},</if>
<if test="endDt!=null">end_dt = #{endDt},</if>
</set>
where cou_id=#{couId}
</update>
</mapper>