zoukankan      html  css  js  c++  java
  • MyBatis对入参对象的属性空判断

     

     
    <!-- 查询学生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或等于空字符串,我们就不进行此条件的判断。
    修改为:
     
    <!-- 查询学生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>
     
    完整版:
     
    <!-- 查询学生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>
     
    当if标签较多时,这样的组合可能会导致错误。
    例如:
    <!-- 查询学生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 开头的,则它会剔除掉。
     
    <!-- 查询学生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> 
     
    详见http://blog.csdn.net/ask_rent/article/details/6320326
  • 相关阅读:
    Android 内存泄漏原因
    Android Selector和Shape的用法
    Android 中的Activity、Window、View之间的关系
    Android Application 详细介绍
    深入理解Android异步消息处理机制
    利用图像识别技术解决非原生控件的定位问题
    一招让 IOS 自动化化快的飞起
    IOS 模拟器多开集成测试和那些坑
    利用UiWatchers 监听解决安卓自动化各种自动化各种非期待弹窗,弹层,升级,广告,对话框,来电等问题
    uiautomatorviewer 优化定位符生成,支持生成Java,Python自动化代码
  • 原文地址:https://www.cnblogs.com/zengda/p/5115194.html
Copyright © 2011-2022 走看看