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

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

Xml代码  
<!-- 查询学生list,like姓名 -->   
<select id="getStudentListLikeName" parameterType="StudentEntity" resultMap="studentResultMap">   
    SELECT * from STUDENT_TBL ST     
WHERE ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName}),'%')    
</select>

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

修改为:

Xml代码  
<!-- 查询学生list,like姓名 -->   
<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或’’值,不附带这个条件,所以查询结果是全部。

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

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

Xml代码  
<!-- 查询学生list,like姓名,=性别、=生日、=班级,使用where,参数entity类型 -->   
<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>   
查询,姓名中有‘李’,男,生日在‘1985-05-28’,班级在‘20000002’的学生。

Java代码  
StudentEntity entity = new StudentEntity();    
entity.setStudentName("李");    
entity.setStudentSex("男");    
entity.setStudentBirthday(StringUtil.parse("1985-05-28"));    
entity.setClassEntity(classMapper.getClassByID("20000002"));    
List<StudentEntity> studentList = studentMapper.getStudentListWhereEntity(entity);    
for( StudentEntity entityTemp : studentList){    
    System.out.println(entityTemp.toString());    
}

3.2 where、set、trim标签

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

Xml代码  
<!-- 查询学生list,like姓名,=性别 -->   
<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>   
</select>   
 如果上面例子,参数studentName为null或’’,则或导致此sql组合成“WHERE AND”之类的关键字多余的错误SQL。
 这时我们可以使用where动态语句来解决。这个“where”标签会知道如果它包含的标签中有返回值的话,它就插入一个‘where’。此外,如果标签返回的内容是以AND 或OR 开头的,则它会剔除掉。
 上面例子修改为:

Xml代码  
<!-- 查询学生list,like姓名,=性别 -->   
<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.2.2 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>   
3.2.3 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>   
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>

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)的关系。

例如下面例子,同样把所有可以限制的条件都写上,方面使用。选择条件顺序,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>   
3.4 foreach
对于动态SQL 非常必须的,主是要迭代一个集合,通常是用于IN 条件。
List 实例将使用“list”做为键,数组实例以“array” 做为键。

3.4.1参数为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>   
接口的方法声明:

Java代码  
public List<StudentEntity> getStudentListByClassIDs(List<String> classList);   
public List<StudentEntity> getStudentListByClassIDs(List<String> classList); 测试代码,查询学生中,在20000002、20000003这两个班级的学生:

Java代码  
List<String> classList = new ArrayList<String>();    
classList.add("20000002");    
classList.add("20000003");    
   
List<StudentEntity> studentList = studentMapper.getStudentListByClassIDs(classList);    
for( StudentEntity entityTemp : studentList){    
    System.out.println(entityTemp.toString());    
}   
List<String> classList = new ArrayList<String>();
classList.add("20000002");
classList.add("20000003");

List<StudentEntity> studentList = studentMapper.getStudentListByClassIDs(classList);
for( StudentEntity entityTemp : studentList){
 System.out.println(entityTemp.toString());
}

3.4.2参数为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>

接口的方法声明:

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

Java代码  
String[] ids = new String[2];    
ids[0] = "20000002";    
ids[1] = "20000003";    
List<StudentEntity> studentList = studentMapper.getStudentListByClassIDs(ids);    
for( StudentEntity entityTemp : studentList){    
    System.out.println(entityTemp.toString());    
}

Mybatis的<where><foreach><set>等标签详解的更多相关文章

  1. MyBatis-动态SQL的if、choose、when、otherwise、trim、where、set、foreach使用(各种标签详解), 以及实体间关系配置

    比较全的文档:https://www.cnblogs.com/zhizhao/p/7808880.html 或 https://blog.csdn.net/zhll3377/article/detai ...

  2. Mybatis批量insert 返回主键值和foreach标签详解

    Mybatis批量insert 返回主键 Mybatis从3.3.1版本开始,支持批量插入后返回主键ID.首先对于支持自增主键的数据库使用useGenerateKeys和keyProperty,对于不 ...

  3. MyBatis 一级缓存、二级缓存全详解(一)

    目录 MyBatis 一级缓存.二级缓存全详解(一) 什么是缓存 什么是MyBatis中的缓存 MyBatis 中的一级缓存 初探一级缓存 探究一级缓存是如何失效的 一级缓存原理探究 还有其他要补充的 ...

  4. Meta标签详解(HTML JAVASCRIPT)

    Meta标签详解,在网上转的,希望对大家有用 您的个人网站即使做得再精彩,在“浩瀚如海”的网络空间中,也如一叶扁舟不易为人发现,如何推广 个人网站,人们首先想到的方法无外乎以下几种: ● 在搜索引擎中 ...

  5. 网页设计:Meta标签详解

    很多人忽视了HTML标签META的强大功效,一个好的META标签设计可以大大提高你的个人网站被搜索到的可能性,有兴趣吗,谁我来重新认识一下META标签吧! META标签是HTML语言HEAD区的一个辅 ...

  6. meta标签详解(meta标签的作用)///////////////////////////转

    meta标签详解(meta标签的作用) 很多人却忽视了HTML标签META的强大功效,一个好的META标签设计可以大大提高你的个人网站被搜索到的可能性,有兴趣吗,谁我来重新认识一下META标签吧   ...

  7. 引用 struts2标签详解 - wo的的日志 - 网易博客

    引用 元元 的 struts2标签详解   引用 COLD 的 struts2标签详解 要在jsp中使用Struts2的标志,先要指明标志的引入.通过jsp的代码的顶部加入以下的代码: <%@t ...

  8. html标签详解

    html标签详解   <!DOCTYPE> 标签 <!DOCTYPE> 声明必须是 HTML 文档的第一行,位于 <html> 标签之前. <!DOCTYPE ...

  9. html标签详解(2)

    http标签详解 声明 1:这里的文字都是我从我自己csdn账号拷贝过来,是本人学习总结的结晶,所以请尊重本作品.2:如要要转载本文章,则要说明文字的出处.3:如有哪里不对欢迎指出. 在上一篇文章中主 ...

随机推荐

  1. JS电子文档链接

    http://www.oschina.net/translate/learning-javascript-design-patterns   学用 JavaScript 设计模式 http://es6 ...

  2. DOM操作-倒排序子元素

    代码: —————————————————————————————— <script type="text/javascript">                // ...

  3. Entity Framework技巧系列之十一 - Tip 42 - 45

    提示42. 怎样使用Code-Only创建一个动态模型 背景: 当我们给出使用Code-Only的例子,总是由创建一个继承自ObjectContext的强类型的Context开始.这个类用于引导模型. ...

  4. Linux教程之配置权限受限制的SFTP

    SFTP 在Linux下是一个很方便很安全的文件传输工具,我常常用它在Linux服务器上替代传统的ftp来传输文件.众所周知SFTP账号是基于SSH账号的,默认情况下访问服务器的权限很大,下面的教程就 ...

  5. listview前几个item的图片怎么是空白的、listview更新了ui不起作用、在handler里更新了UI不起作用

    不是不起作用,不信你可以在更新ui代码附近加输出的log,说明程序是跑到那里了.但是未达到我们的想要的效果. 我们知道在listview里更新UI,listview的适配器Adapter里有个getV ...

  6. -Swift.h not find

    亲测成功. 随便新建一个swift文件,xcode问是否生成xxx-Bridging-Header.h文件,点YES.再编译,问题解决. By default, the generated heade ...

  7. 二分 Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) D

    http://codeforces.com/contest/722/problem/D 题目大意:给你一个没有重复元素的Y集合,再给你一个没有重复元素X集合,X集合有如下操作 ①挑选某个元素*2 ②某 ...

  8. Inno Setup入门(六)——在程序目录下创建文件

    创建文件夹可以使用[dirs]段实现,代码如下: [setup] ;全局设置,本段必须 AppName=Test AppVerName=TEST DefaultDirName="E:\TES ...

  9. android cordova h5总结

    最近项目 替换页面  把80%页面替换成h5了. 首页h5页面可以放在android本地.增加访问速度.节省用户流量 把服务器上的  js代码 压缩成zip格式  放在asset目录.当应用安装时候 ...

  10. springMVC中ajax的运用于注意事项

    ajax的运用: 注意事项: dataType:"json"在ajax中可写可不写(ajax能够自动识别返回值类型),写了更加规范,可以在ajax识别错误返回值类型的时候,指定返回 ...