错误方式一:
在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
版权声明:本文为博主原创文章,转载请附上博文链接!

动态SQL之、条件判断(转)的更多相关文章

  1. Mybatis中动态SQL多条件查询

    Mybatis中动态SQL多条件查询 mybatis中用于实现动态SQL的元素有: if:用if实现条件的选择,用于定义where的字句的条件. choose(when otherwise)相当于Ja ...

  2. Mybatis学习笔记10 - 动态sql之if判断

    示例代码: 接口定义: package com.mybatis.dao; import com.mybatis.bean.Employee; import java.util.List; public ...

  3. 动态SQL屏幕条件选择(里面还有赋值的新语法)

    有时候屏幕条件中使用PARAMETERS时候,如果你为空的话,会查不出数据,但是可能你的想法是不想限制而已,但是系统默认理解为了空值,这个时候,如果取判断一下条件是不是空,在SQL里决定写不写的话,会 ...

  4. 动态SQL语句:定义(一)

    文章系列 动态SQL语句:定义(一) 静态SQL与动态SQL 静态SQL:程序运行前,具有固定的形式和结构的SQL. 动态SQL:程序运行时,能够动态改变形式或结构的SQL. 一些思考和想法 在实际的 ...

  5. mybatis入门-动态sql

    什么是动态sql 判断的动态sql mybatis核心就是对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装. 现有需求如下:需要查询用户,输入的是用户类,如果用户的性别类不为空 ...

  6. mybatis中的动态SQL

    在实际开发中,数据库的查询很难一蹴而就,我们往往要根据各种不同的场景拼接出不同的SQL语句,这无疑是一项复杂的工作,我们在使用mybatis时,mybatis给我们提供了动态SQL,可以让我们根据具体 ...

  7. 接口的绑定方案和动态SQL

    1. 接口绑定方案 MyBatis中, 提供了一套接口绑定方案. 程序员可以提供一个接口, 然后提供对应接口的一个mapper.xml文件. MyBatis会自动将接口和xml文件进行绑定. 实际上就 ...

  8. Mybatis学习第三天——输入输出映射以及动态SQL

    注意:以下传入数据与输出数据类型部分使用别名的方式,别名在SqlMapConfig.xml核心文件中配置 1.输入映射 1.1 传递简单数据类型 1.2 传递pojo中的类类型 1.3 传递Query ...

  9. LINQ to SQL 运行时动态构建查询条件

    在进行数据查询时,经常碰到需要动态构建查询条件.使用LINQ实现这个需求可能会比以前拼接SQL语句更麻烦一些.本文介绍了3种运行时动态构建查询条件的方法.本文中的例子最终实现的都是同一个功能,从Nor ...

随机推荐

  1. 从0开始的Python学习010return语句&DocStrings

    return语句 return语句用来从一个函数中 返回 即跳出函数.当然也可以从函数中返回一个值. #return 语句从一个函数返回 即跳出函数.我们也可选从函数返回一个值 def maximum ...

  2. SQLServer之数据类型解析

    数字 int.bigint.smallint 和 tinyint 使用整数数据的精确数字数据类型. 数据类型 范围 存储 tinyint 0 到 255. 1 字节 smallint -2^15 (- ...

  3. android菜鸟,了解android工程目录结构

  4. 算法"新"名词

    这个“新”是对于自己而言. 最近几天接触到很多新的名词,如: 回溯法(backtracking):以前知道,但很少用 动态规划(dynamic programming):序列型.矩阵型.区间型.背包等 ...

  5. 【实战代码】PHP实现读取一个1G的文件大小

    本文地址:http://www.cnblogs.com/aiweixiao/p/7535351.html 欢迎关注我的微信公众号哈 “ 程序员的文娱情怀” http://t.cn/RotyZtu [背 ...

  6. 面向对象_classmethod_staticmethod

    classmethod:类方法 主要用于改变静态属性 class Fruit_price: __discount= 1 def __init__(self,original_price): self. ...

  7. eclipse去除对js文件的检测

  8. Linux删除隐藏文件

    方法2.ls -a 查询隐藏文件 将后缀名为.swp的文件删除 rm -f .nginx.conf.swp 再次编辑文件不在出现提示警告!

  9. c++11のunique_lock和once_flag

    一. Unique _lock和lockguard一样,到那时比lockguard更加灵活,可以随时按照需要加锁开锁 std::unique_lock<std::mutex> locker ...

  10. Hadoop从入门到精通系列之--0.Hadoop生态体系

    https://blog.csdn.net/Haidaiya/article/details/84568588#%E4%B8%80%20%E5%A4%A7%E6%95%B0%E6%8D%AE%E7%9 ...