Mybatis动态SQL之if、choose、where、set、trim、foreach实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/UN3009/article/details/71405953
1.1 if标签
<select id=" getStudentListLikeName " parameterType="StudentEntity" resultMap="studentResultMap"> 
  SELECT * from STUDENT_TBL ST  
  <if test="studentName!=null and studentName!='' "> 
    WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')  
  </if> 
</select>
当studentName的值为null或’’的时候,我们并不进行where条件的判断,所以当studentName值为null或’’值,不附带这个条件,所以查询结果是全部。
1.2 where标签
<select id="getStudentListWhereEntity" parameterType="StudentEntity" resultMap="studentResultMap"> 
  SELECT * from STUDENT_TBL ST  
  <where> 
    <if test="studentName!=null and studentName!='' "> 
      ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')  
    </if> 
    <if test="studentSex!= null and studentSex!= '' "> 
      AND ST.STUDENT_SEX = #{studentSex}  
    </if> 
    <if test="studentBirthday!=null"> 
      AND ST.STUDENT_BIRTHDAY = #{studentBirthday}  
    </if> 
    <if test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' "> 
      AND ST.CLASS_ID = #{classEntity.classID}  
    </if> 
  </where>
</select>
1.3 set标签
当在update语句中使用if标签时,如果前面的if没有执行,则或导致逗号多余错误。使用set标签可以将动态的配置SET 关键字,和剔除追加到条件末尾的任何不相关的逗号。
没有使用if标签时,如果有一个参数为null,都会导致错误,如下示例:

Xml代码 
<!-- 更新学生信息 --> 
<update id="updateStudent" parameterType="StudentEntity"> 
  UPDATE STUDENT_TBL  
    SET STUDENT_TBL.STUDENT_NAME = #{studentName},  
      STUDENT_TBL.STUDENT_SEX = #{studentSex},  
      STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},  
      STUDENT_TBL.CLASS_ID = #{classEntity.classID}  
   WHERE STUDENT_TBL.STUDENT_ID = #{studentID};  
</update>

使用set+if标签修改后,如果某项为null则不进行更新,而是保持数据库原值。如下示例:
Xml代码 
<!-- 更新学生信息 --> 
<update id="updateStudent" parameterType="StudentEntity"> 
  UPDATE STUDENT_TBL  
  <set> 
    <if test="studentName!=null and studentName!='' "> 
      STUDENT_TBL.STUDENT_NAME = #{studentName},  
    </if> 
    <if test="studentSex!=null and studentSex!='' "> 
      STUDENT_TBL.STUDENT_SEX = #{studentSex},  
    </if> 
    <if test="studentBirthday!=null "> 
      STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},  
    </if> 
    <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' "> 
      STUDENT_TBL.CLASS_ID = #{classEntity.classID}  
    </if> 
  </set> 
  WHERE STUDENT_TBL.STUDENT_ID = #{studentID};  
</update>

1.4 trim
trim是更灵活的去处多余关键字的标签,他可以实践where和set的效果。
where例子的等效trim语句:

Xml代码 
<!-- 查询学生list,like姓名,=性别 --> 
<select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap"> 
  SELECT * from STUDENT_TBL ST  
  <trim prefix="WHERE" prefixOverrides="AND|OR"> 
    <if test="studentName!=null and studentName!='' "> 
      ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')  
    </if> 
    <if test="studentSex!= null and studentSex!= '' "> 
      AND ST.STUDENT_SEX = #{studentSex}  
    </if> 
  </trim> 
</select>

1.4.1 set例子的等效trim语句示例:
Xml代码 
<!-- 更新学生信息 --> 
<update id="updateStudent" parameterType="StudentEntity"> 
  UPDATE STUDENT_TBL  
  <trim prefix="SET" suffixOverrides=","> 
    <if test="studentName!=null and studentName!='' "> 
      STUDENT_TBL.STUDENT_NAME = #{studentName},  
    </if> 
    <if test="studentSex!=null and studentSex!='' "> 
      STUDENT_TBL.STUDENT_SEX = #{studentSex},  
    </if> 
    <if test="studentBirthday!=null "> 
      STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},  
    </if> 
    <if test="classEntity!=null and classEntity.classID!=null and classEntity.classID!='' "> 
      STUDENT_TBL.CLASS_ID = #{classEntity.classID}  
    </if> 
  </trim> 
  WHERE STUDENT_TBL.STUDENT_ID = #{studentID};  

</update>

1.4.2 set例子的等效trim语句示例:

Xml代码 
<!-- 更新学生信息 --> 
<update id="updateStudent" parameterType="userEntity"> 
  UPDATE user
  <trim prefix="SET" suffixOverrides="," suffix="WHERE id = #{userId}"> 
    <if test="userName!=null and userName!='' "> 

      user.userName = #{userName},
    </if> 
    <if test="userSex!=null and userSex!='' "> 
      user.userSex = #{userSex},  
    </if> 

  </trim> 
  WHERE STUDENT_TBL.STUDENT_ID = #{studentID};  

</update>

运行结果:UPDATE user SET userName = ?, password=? WHERE id = ?

代替where的就是添加 prefix="where"  prefixOverrides="and|or"  trim就会自动的添加前缀where和自动识别和去掉不用的and或or


1.5 choose (when, otherwise)
有时候我们并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,则执行otherwise中的sql。类似于Java 的switch 语句,choose为switch,when为case,otherwise则为default。
if是与(and)的关系,而choose是或(or)的关系。

例如下面例子,同样把所有可以限制的条件都写上,方面使用。选择条件顺序,when标签的从上到下的书写顺序:

Xml代码 
<!-- 查询学生list,like姓名、或=性别、或=生日、或=班级,使用choose --> 
<select id="getStudentListChooseEntity" parameterType="StudentEntity" resultMap="studentResultMap"> 
  SELECT * from STUDENT_TBL ST  
  <where> 
    <choose> 
      <when test="studentName!=null and studentName!='' "> 
          ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')  
      </when> 
      <when test="studentSex!= null and studentSex!= '' "> 
          AND ST.STUDENT_SEX = #{studentSex}  
      </when> 
      <when test="studentBirthday!=null"> 
        AND ST.STUDENT_BIRTHDAY = #{studentBirthday}  
      </when> 
      <when test="classEntity!=null and classEntity.classID !=null and classEntity.classID!='' "> 
        AND ST.CLASS_ID = #{classEntity.classID}  
      </when> 
      <otherwise> 
          
      </otherwise> 
    </choose> 
  </where> 
</select>  

1.6 foreach
对于动态SQL 非常必须的,主是要迭代一个集合,通常是用于IN 条件。
List 实例将使用“list”做为键,数组实例以“array” 做为键。

参数为list实例的写法:
SQL写法:

Xml代码 
<select id="getStudentListByClassIDs" resultMap="studentResultMap">
    SELECT * FROM STUDENT_TBL ST
        WHERE ST.CLASS_ID IN
    <foreach collection="list" item="classList" open="(" separator="," close=")">
        #{classList}
    </foreach>
</select>

1.6.1参数为Array实例的写法:
SQL语句:

Xml代码 
<select id="getStudentListByClassIDs" resultMap="studentResultMap">
    SELECT * FROM STUDENT_TBL ST
        WHERE ST.CLASS_ID IN
    <foreach collection="array" item="ids" open="(" separator="," close=")">
        #{ids}
    </foreach>

</select


猜你喜欢

转载自blog.csdn.net/UN3009/article/details/71405953