zoukankan      html  css  js  c++  java
  • 动态SQL之、条件判断(转)

    错误方式一:
    在mybatis的动态sql语句中使用<if>标签可以判断sql中的条件是否成立。

    <select id="getPerson" resultType="com.lzj.bean.Employee">
    select * from tbl_employee
    where
    <if test="id!=null">
    id=#{id}
    </if>
    <if test="lastName!=null and lastName!=''">
    and last_name like #{lastName}
    </if>
    <if test="email!=null and email.trim()!=''">
    and email=#{email}
    </if>
    <if test="gender==0 or gender==1">
    and gender=#{gender}
    </if>
    </select>
    在上面的动态sql语句中存在一个问题,当第一条sql判断语句

    <if test="id!=null">
    id=#{id}
    </if>
    失败时,即id值为null,而lastName、email和gender判断成功后,最后sql语句就会变为:
    select * from tbl_employee where and last_name like #{lastName} and email=#{email} and gender=#{gender}
    where后面多一个and,执行sql时会失败。

    改正方式一:
    在where条件后面加了一条判断1=1,然后在id的判断后加上and关键字,这样当下面if条件中的任何一个判断失败后,都不会影响整个sql语句。

    <select id="getPerson" resultType="com.lzj.bean.Employee">
    select * from tbl_employee
    where 1=1
    <if test="id!=null">
    and id=#{id}
    </if>
    <if test="lastName!=null and lastName!=''">
    and last_name like #{lastName}
    </if>
    <if test="email!=null and email.trim()!=''">
    and email=#{email}
    </if>
    <if test="gender==0 or gender==1">
    and gender=#{gender}
    </if>
    </select>
    错误方式二:
    有些人习惯在每个if判断中的数据库字段的后面加and关键字,例如

    <select id="getPerson" resultType="com.lzj.bean.Employee">
    select * from tbl_employee
    where
    <if test="id!=null">
    id=#{id} and
    </if>
    <if test="lastName!=null and lastName!=''">
    last_name like #{lastName} and
    </if>
    <if test="email!=null and email.trim()!=''">
    email=#{email} and
    </if>
    <if test="gender==0 or gender==1">
    gender=#{gender}
    </if>
    </select>
    但是上述情况存在一个弊端,当最后一个if判断gender失败时,sql语句就变成了:
    select * from tbl_employee where id=#{id} and last_name like #{lastName} and email=#{email} and
    where条件的最后多一个and,sql语句执行的时候也会失败。
    改正方式二:
    在最后一个if语句中库表字段后加and关键字,然后在最后加1=1判断

    <select id="getPerson" resultType="com.lzj.bean.Employee">
    select * from tbl_employee
    where
    <if test="id!=null">
    id=#{id} and
    </if>
    <if test="lastName!=null and lastName!=''">
    last_name like #{lastName} and
    </if>
    <if test="email!=null and email.trim()!=''">
    email=#{email} and
    </if>
    <if test="gender==0 or gender==1">
    gender=#{gender} and
    </if>
    1=1
    </select>
    建议方式:
    用<where>和<if> 进行组合,当条件不成立时,if条件后的内容包括and也不会存在,因此不会对整个sql语句产生影响。注意and关键字要放在每个<if>语句中的库表字段赋值的前面。因为,一旦判断不成功,<where> 会把对应的and关键字去掉(还有or关键字)。

    <select id="getPerson" resultType="com.lzj.bean.Employee">
    select * from tbl_employee
    <where>
    <!-- test:判断表达式(OGNL)
    遇见特殊符号应该去写转义字符:&&、''等字符
    -->
    <if test="id!=null">
    id=#{id}
    </if>
    <if test="lastName!=null and lastName!=''">
    and last_name like #{lastName}
    </if>
    <if test="email!=null and email.trim()!=''">
    and email=#{email}
    </if>
    <!-- ognl会进行字符串与数字的转换判断 "0"==0 -->
    <if test="gender==0 or gender==1">
    and gender=#{gender}
    </if>
    </where>
    </select>

    上述很多特殊字符可以写成转义的形式,例如
    1
    <select id="getEmpsByConditionIf" resultType="com.atguigu.mybatis.bean.Employee">
    select * from tbl_employee
    <!-- where -->
    <where>
    <if test="id!=null">
    id=#{id}
    </if>
    <if test="lastName!=null &amp;&amp; lastName!=&quot;&quot;">
    and last_name like #{lastName}
    </if>
    <if test="email!=null and email.trim()!=&quot;&quot;">
    and email=#{email}
    </if>
    <if test="gender==0 or gender==1">
    and gender=#{gender}
    </if>
    </where>
    </select>

    注意,`<if>`失败后, `<where>` 关键字只会去掉库表字段赋值前面的and,不会去掉后面的and关键字,即注意,`<where>` 只会去掉`<if>` 语句中的最开始的and关键字。所以下面的形式是不可取的
    <select id="getPerson" resultType="com.lzj.bean.Employee">
    select * from tbl_employee
    <where>
    <if test="id!=null">
    id=#{id} and
    </if>
    <if test="lastName!=null and lastName!=''">
    last_name like #{lastName} and
    </if>
    <if test="email!=null and email.trim()!=''">
    email=#{email} and
    </if>
    <if test="gender==0 or gender==1">
    gender=#{gender}
    </if>
    </where>
    </select>
    ``
    因为,失败后,`不会自动去掉后面的and关键字,这种形式与错误方式二种原理相同。
    ---------------------
    作者:苍鹰蛟龙
    来源:CSDN
    原文:https://blog.csdn.net/u010502101/article/details/79117000
    版权声明:本文为博主原创文章,转载请附上博文链接!

  • 相关阅读:
    pandas 修改列顺序
    read_csv,to_csv 前n行
    郭盛华:未来黑客攻击的将远不止网络
    微软的 Linux 存储库停机 18 多个小时
    警惕黑客利用 Google Docs进行网络钓鱼
    苹果发布紧急补丁!修复被黑客利用的2个零日漏洞
    谷歌发布新框架以防止软件供应链攻击
    郭盛华:以知识见识锤炼真本领,年轻人要有理想
    通过 GDPR 加强密码政策,是企业网络的第一道防线
    肉类供应商遭黑客攻击,并支付了 1100 万美元的赎金
  • 原文地址:https://www.cnblogs.com/snowhite/p/10536760.html
Copyright © 2011-2022 走看看