mybatis 中sql

1   if标签

if标签的基本功能就跟if功能差不多,只是在if标签中它会给出几个条件去判断,如果条件不成立则不会执行 if标签中的sql语句

<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>

2  where标签

where标签与sql中的where功能差不多,只是在where的基础上添加了一些新的功能

如果它包含的标签中有返回值的话就插入一个where。

此外如果标签返回的内容是以AND或OR开头的,则它会剔除掉。

<select id="getStudentListWhere" 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>
 </where>
</select>

3  set标签

使用set+if标签修改后,如果某项为null则不进行更新,而是保持数据库原值 。

<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>

4  trim标签

trim是更灵活的去处多余关键字的标签,他可以实践where和set的效果。

trim元素的主要功能是可以在自己包含的内容前加上某些前缀,也可以在其后加上某些后缀,与之对应的属性是 prefix和suffix;

可以把包含内容的首部某些内容覆盖,即忽略,也可以把尾部的某些内容覆盖,

对应的属性是prefixOverrides和 suffixOverrides;正因为trim有这样的功能,

所以我们也可以非常简单的利用trim来代替where元素的功能。




where例子的等效trim语句

<select id="getStudentListWhere" parameterType="StudentEntity" resultMap="studentResultMap">
 SELECT * from STUDENT_TBL ST
 <trim prefix="WHERE"="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>

 

set例子的等效trim语句

<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>

5 choose标签

choose (when, otherwise)

有时候并不想应用所有的条件,而只是想从多个选项中选择一个。MyBatis提供了choose 元素,

按顺序判断when中的条件出否成立,如果有一个成立,则choose结束。当choose中所有when的条件都不满则时,

则执行otherwise中的sql。类似于Java 的switch语句,choose为switch,when为case,otherwise则为default。

<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>
6 foreach

对于动态SQL 非常必须的,主是要迭代一个集合,通常是用于IN 条件。

List实例将使用“list”做为键,数组实例以“array”做为键。

原文地址:https://www.cnblogs.com/wwqqnn123456/p/7489149.html