• 四、动态SQL(if_where)


    一、动态SQL

    根据条件的不同, SQL 语句也会随之动态的改变. MyBatis中, 提供了一组标签用于实现动态SQL.

    二、 <if>

    用于进行条件判断, test 属性用于指定判断条件. 为了拼接条件, 在 SQL 语句后强行添加 1=1 的恒成立条件.

    <select id="sel" resultType="user"> 
      select * from t_user where 1=1   
    <if test="username != null and username != ''">
        and username=#{username}   
    </if>   <if test="password != null and password != ''">
        and password=#{password}   </if> </select>

    三、<where>

    用于管理 where 子句. 有如下功能:

      a) 如果没有条件, 不会生成 where 关键字

      b) 如果有条件, 会自动添加 where 关键字

      c) 如果第一个条件中有and, 去除之

     1 <select id="sel" resultType="user"> 
     2     select * from t_user
     3     <where>
     4         <if test="username != null and username != ''">
     5              and username=#{username}
     6         </if>
     7 
     8         <if test="password != null and password != ''">
     9              and password=#{password}
    10         </if>
    11      </where>
    12 </select>            
  • 相关阅读:
    Android深度探索第一章读书笔记
    ocRuntime基本功
    关于堆栈和block之我见
    OC 与 swift 混编杂记
    UItableViewCell自适应高度的坑
    windows蓝屏处理
    MBProgressHUD自定义customView
    svn 配置忽略文件
    ios 开发项目常用第三方库:
    cocoaPod 更换镜像源
  • 原文地址:https://www.cnblogs.com/qiaoxin11/p/12855458.html
Copyright © 2020-2023  润新知