Mybatis的<where><foreach><set>等标签详解

た 入场券 2022-06-17 08:27 297阅读 0赞

sql语句where条件中,需要一些安全判断,例如按性别检索,如果传入的参数是空的,此时查询出的结果很可能是空的,也许我们需要参数为空 时,是查出全部的信息。这是我们可以使用动态sql,增加一个判断,当参数不符合要求的时候,我们可以不去判断此查询条件。
下文均采用MySQL语法和函数(例如字符串链接函数CONCAT

3.1 if标签
一个很普通的查询:

Xml代码

[html] view plain copy

  1. <**select id=”getStudentListLikeName” parameterType=”StudentEntity” resultMap=”studentResultMap”>**
  2. SELECT * from STUDENT_TBL ST
  3. WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT(‘%’, #{studentName}),’%’)
  4. </**select**>

但是此时如果studentName是null或空字符串,此语句很可能报错或查询结果为空。此时我们使用if动态sql语句先进行判断,如果值为null或等于空字符串,我们就不进行此条件的判断。

修改为:

Xml代码

[html] view plain copy

  1. <**select id=” getStudentListLikeName “ parameterType=”StudentEntity” resultMap=”studentResultMap”>**
  2. SELECT * from STUDENT_TBL ST
  3. <**if test=”studentName!=null and studentName!=’’ “>**
  4. WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT(‘%’, #{studentName}),’%’)
  5. </**if**>
  6. </**select**>

此时,当studentName的值为null或’’的时候,我们并不进行where条件的判断,所以当studentName值为null或’’值,不附带这个条件,所以查询结果是全部。

由于参数是Java的实体类,所以我们可以把所有条件都附加上,使用时比较灵活, new一个这样的实体类,我们需要限制那个条件,只需要附上相应的值就会where这个条件,相反不去赋值就可以不在where中判断。

代码中的where标签,请参考3.2.1.

Xml代码

[html] view plain copy

  1. <**select id=”getStudentListWhereEntity” parameterType=”StudentEntity” resultMap=”studentResultMap”>**
  2. SELECT * from STUDENT_TBL ST
  3. <**where**>
  4. <**if test=”studentName!=null and studentName!=’’ “>**
  5. ST.STUDENT_NAME LIKE CONCAT(CONCAT(‘%’, #{studentName}),’%’)
  6. </**if**>
  7. <**if test=”studentSex!= null and studentSex!= ‘’ “>**
  8. AND ST.STUDENT_SEX = #{studentSex}
  9. </**if**>
  10. <**if test=”studentBirthday!=null”>**
  11. AND ST.STUDENT_BIRTHDAY = #{studentBirthday}
  12. </**if**>
  13. <**if test=”classEntity!=null and classEntity.classID !=null and classEntity.classID!=’’ “>**
  14. AND ST.CLASS_ID = #{classEntity.classID}
  15. </**if**>
  16. </**where**>
  17. </**select**>

查询,姓名中有‘李’,男,生日在‘1985-05-28’,班级在‘20000002’的学生。

Java代码

[java] view plain copy

  1. StudentEntity entity = new StudentEntity();
  2. entity.setStudentName(“李”);
  3. entity.setStudentSex(“男”);
  4. entity.setStudentBirthday(StringUtil.parse(“1985-05-28”));
  5. entity.setClassEntity(classMapper.getClassByID(“20000002”));
  6. List studentList = studentMapper.getStudentListWhereEntity(entity);
  7. for( StudentEntity entityTemp : studentList){
  8. System.out.println(entityTemp.toString());
  9. }

3.2 where、set、trim标签

3.2.1 where
当if标签较多时,这样的组合可能会导致错误。例如,like姓名,等于指定性别等:

Xml代码

[html] view plain copy

  1. <**select id=”getStudentListWhere” parameterType=”StudentEntity” resultMap=”studentResultMap”>**
  2. SELECT * from STUDENT_TBL ST
  3. WHERE
  4. <**if test=”studentName!=null and studentName!=’’ “>**
  5. ST.STUDENT_NAME LIKE CONCAT(CONCAT(‘%’, #{studentName}),’%’)
  6. </**if**>
  7. <**if test=”studentSex!= null and studentSex!= ‘’ “>**
  8. AND ST.STUDENT_SEX = #{studentSex}
  9. </**if**>
  10. </**select**>

如果上面例子,参数studentName为null或’’,则或导致此sql组合成“WHERE AND”之类的关键字多余的错误SQL。
这时我们可以使用where动态语句来解决。这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。
上面例子修改为:

Xml代码

[html] view plain copy

  1. <**select id=”getStudentListWhere” parameterType=”StudentEntity” resultMap=”studentResultMap”>**
  2. SELECT * from STUDENT_TBL ST
  3. <**where**>
  4. <**if test=”studentName!=null and studentName!=’’ “>**
  5. ST.STUDENT_NAME LIKE CONCAT(CONCAT(‘%’, #{studentName}),’%’)
  6. </**if**>
  7. <**if test=”studentSex!= null and studentSex!= ‘’ “>**
  8. AND ST.STUDENT_SEX = #{studentSex}
  9. </**if**>
  10. </**where**>
  11. </**select**>

3.2.2 set
当在update语句中使用if标签时,如果前面的if没有执行,则或导致逗号多余错误。使用set标签可以将动态的配置SET 关键字,和剔除追加到条件末尾的任何不相关的逗号。
没有使用if标签时,如果有一个参数为null,都会导致错误,如下示例:

Xml代码

[html] view plain copy

  1. <**update id=”updateStudent” parameterType=”StudentEntity”>**
  2. UPDATE STUDENT_TBL
  3. SET STUDENT_TBL.STUDENT_NAME = #{studentName},
  4. STUDENT_TBL.STUDENT_SEX = #{studentSex},
  5. STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},
  6. STUDENT_TBL.CLASS_ID = #{classEntity.classID}
  7. WHERE STUDENT_TBL.STUDENT_ID = #{studentID};
  8. </**update**>

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

Xml代码

[html] view plain copy

  1. <**update id=”updateStudent” parameterType=”StudentEntity”>**
  2. UPDATE STUDENT_TBL
  3. <**set**>
  4. <**if test=”studentName!=null and studentName!=’’ “>**
  5. STUDENT_TBL.STUDENT_NAME = #{studentName},
  6. </**if**>
  7. <**if test=”studentSex!=null and studentSex!=’’ “>**
  8. STUDENT_TBL.STUDENT_SEX = #{studentSex},
  9. </**if**>
  10. <**if test=”studentBirthday!=null “>**
  11. STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},
  12. </**if**>
  13. <**if test=”classEntity!=null and classEntity.classID!=null and classEntity.classID!=’’ “>**
  14. STUDENT_TBL.CLASS_ID = #{classEntity.classID}
  15. </**if**>
  16. </**set**>
  17. WHERE STUDENT_TBL.STUDENT_ID = #{studentID};
  18. </**update**>

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

where例子的等效trim语句:

Xml代码

[html] view plain copy

  1. <**select id=”getStudentListWhere” parameterType=”StudentEntity” resultMap=”studentResultMap”>**
  2. SELECT * from STUDENT_TBL ST
  3. <**trim prefix=”WHERE” prefixOverrides=”AND|OR”>**
  4. <**if test=”studentName!=null and studentName!=’’ “>**
  5. ST.STUDENT_NAME LIKE CONCAT(CONCAT(‘%’, #{studentName}),’%’)
  6. </**if**>
  7. <**if test=”studentSex!= null and studentSex!= ‘’ “>**
  8. AND ST.STUDENT_SEX = #{studentSex}
  9. </**if**>
  10. </**trim**>
  11. </**select**>

set例子的等效trim语句:

Xml代码

[html] view plain copy

  1. <**update id=”updateStudent” parameterType=”StudentEntity”>**
  2. UPDATE STUDENT_TBL
  3. <**trim prefix=”SET” suffixOverrides=”,”>**
  4. <**if test=”studentName!=null and studentName!=’’ “>**
  5. STUDENT_TBL.STUDENT_NAME = #{studentName},
  6. </**if**>
  7. <**if test=”studentSex!=null and studentSex!=’’ “>**
  8. STUDENT_TBL.STUDENT_SEX = #{studentSex},
  9. </**if**>
  10. <**if test=”studentBirthday!=null “>**
  11. STUDENT_TBL.STUDENT_BIRTHDAY = #{studentBirthday},
  12. </**if**>
  13. <**if test=”classEntity!=null and classEntity.classID!=null and classEntity.classID!=’’ “>**
  14. STUDENT_TBL.CLASS_ID = #{classEntity.classID}
  15. </**if**>
  16. </**trim**>
  17. WHERE STUDENT_TBL.STUDENT_ID = #{studentID};
  18. </**update**>

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

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

Xml代码

[html] view plain copy

  1. <**select id=”getStudentListChooseEntity” parameterType=”StudentEntity” resultMap=”studentResultMap”>**
  2. SELECT * from STUDENT_TBL ST
  3. <**where**>
  4. <**choose**>
  5. <**when test=”studentName!=null and studentName!=’’ “>**
  6. ST.STUDENT_NAME LIKE CONCAT(CONCAT(‘%’, #{studentName}),’%’)
  7. </**when**>
  8. <**when test=”studentSex!= null and studentSex!= ‘’ “>**
  9. AND ST.STUDENT_SEX = #{studentSex}
  10. </**when**>
  11. <**when test=”studentBirthday!=null”>**
  12. AND ST.STUDENT_BIRTHDAY = #{studentBirthday}
  13. </**when**>
  14. <**when test=”classEntity!=null and classEntity.classID !=null and classEntity.classID!=’’ “>**
  15. AND ST.CLASS_ID = #{classEntity.classID}
  16. </**when**>
  17. <**otherwise**>
  18. </**otherwise**>
  19. </**choose**>
  20. </**where**>
  21. </**select**>

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

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

Xml代码

[html] view plain copy

  1. <**select id=”getStudentListByClassIDs” resultMap=”studentResultMap”>**
  2. SELECT * FROM STUDENT_TBL ST
  3. WHERE ST.CLASS_ID IN
  4. <**foreach collection=”list” item=”classList” open=”(“ separator=”,” close=”)”>**
  5. #{classList}
  6. </**foreach**>
  7. </**select**>

接口的方法声明:

[java] view plain copy

  1. public List getStudentListByClassIDs(List classList);
  2. public List getStudentListByClassIDs(List classList); 测试代码,查询学生中,在20000002、20000003这两个班级的学生:

[java] view plain copy

  1. List classList = new ArrayList();
  2. classList.add(“20000002”);
  3. classList.add(“20000003”);
  4. List studentList = studentMapper.getStudentListByClassIDs(classList);
  5. for( StudentEntity entityTemp : studentList){
  6. System.out.println(entityTemp.toString());
  7. }
  8. List classList = new ArrayList();
  9. classList.add(“20000002”);
  10. classList.add(“20000003”);
  11. List studentList = studentMapper.getStudentListByClassIDs(classList);
  12. for( StudentEntity entityTemp : studentList){
  13. System.out.println(entityTemp.toString());
  14. }

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

[html] view plain copy

  1. <**select id=”getStudentListByClassIDs” resultMap=”studentResultMap”>**
  2. SELECT * FROM STUDENT_TBL ST
  3. WHERE ST.CLASS_ID IN
  4. <**foreach collection=”array” item=”ids” open=”(“ separator=”,” close=”)”>**
  5. #{ids}
  6. </**foreach**>
  7. </**select**>

接口的方法声明:

[java] view plain copy

  1. public List getStudentListByClassIDs(String[] ids);
  2. public List getStudentListByClassIDs(String[] ids);测试代码,查询学生中,在20000002、20000003这两个班级的学生:

[java] view plain copy

  1. String[] ids = new String[2];
  2. ids[0] = “20000002”;
  3. ids[1] = “20000003”;
  4. List studentList = studentMapper.getStudentListByClassIDs(ids);
  5. for( StudentEntity entityTemp : studentList){
  6. System.out.println(entityTemp.toString());
  7. }

发表评论

表情:
评论列表 (有 0 条评论,297人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Mybatis标签详解

    sql语句where条件中,需要一些安全判断,例如按性别检索,如果传入的参数是空的,此时查询出的结果很可能是空的,也许我们需要参数为空 时,是查出全部的信息。这是我们可以使用动

    相关 Mybatis标签详解

    sql语句where条件中,需要一些安全判断,例如按性别检索,如果传入的参数是空的,此时查询出的结果很可能是空的,也许我们需要参数为空 时,是查出全部的信息。这是我们可以使用动

    相关 Mybatis标签

    sql语句where条件中,需要一些安全判断,例如按性别检索,如果传入的参数是空的,此时查询出的结果很可能是空的,也许我们需要参数为空 时,是查出全部的信息。这是我们可以使用动