MyBatis if标签的用法
<!--
4.1.1 在WHERE条件中使用if
需求:
实现一个用户管理高级查询功能,根据输入的条件去检索用户信息。这个功能
还需要支持以下三种情况:当只有输入用户名时,需要根据用户名进行模糊查
询;当只有输入邮箱时,根据邮箱进行完全匹配;当同时输入用户名与邮箱时
用这两个条件去查询匹配的用户。
<if>便签有一个必填的属性test,test的属性值是一个符合OGNL要求的判断表达式,
表达式的结果可以是true或者false,初次之外所有的的非0值都为true,只有0为false。
且有如下规则:
1.判断条件property!=null或者property==null:适用于任何类型的字段,用于判断属性值是否为空
2.判断条件property!=''或者property=='':仅适用于String类型的字段,用于判断是否为空字符串
3.and和or:当有多个判断条件时,适用and或or进行连接,嵌套的判断可以适用小括号分组。
-->
<!--不能满足需求的代码,标记下模糊匹配的写法-->
<select id="selectByUser" resultType="tk.mybatis.simple.model.SysUser">
select
id,
use_name userName,
user_password userPassword,
user_email userEmail,
user_info userInfo,
head_img headImg,
create_time createTime
from sys_user
where
user_name like concat('%',#{userName},'%') and
uer_email=#{userEmail}
</select>
<!--改进后的代码-->
<select id="selectByUser" resultType="tk.mybatis.simple.model.SysUser">
select
id,
use_name userName,
user_password userPassword,
user_email userEmail,
user_info userInfo,
head_img headImg,
create_time createTime
from sys_user
where
1=1
<if test="userName!=null and userName!=''">
and user_name like concat('%',#{userName},'%')
</if>
<if test="userEmail!=null and userEmail!=''">
and user_email = #{userEmail}
</if>
</select>
<!--
4.1.3 在UPDATE更新列中使用if
需求:
只更新有变化的字段,需要注意,更新的时候不能将原来的值
但没有发生变化的字段更新为空或null。
-->
<!--需求实现的代码-->
<update id="updateByIdSelective">
update sys_user
set
<if test="userName!=null and userName!=''">
user_name=#{userName},
</if>
<if test="userEmail!=null and userEmail!=''">
user_email=#{userEmail},
</if>
<if test="userInfo!=null and userInfo!=''">
user_info=#{userInfo},
</if>
<if test="headImg!=null">
head_img=#{headImg},
</if>
<if test="createTime!=null">
create_time=#{createTime},
</if>
id=#{id}
where id=#{id}
</update>
<!--
4.1.3 在INSERT动态插入列中使用if
需求:
在数据库中插入数据的时候,如果某一列的参数值不为空,就使用传入的值,如果传入
的参数为空,就使用数据库中的默认值(通常是空),而不使用传入的空值。
-->
<insert id="insert2" useGeneratedKeys="true" keyProperty="id">
INSERT INTO sys_user
(id,user_name,user_password,
<if test="userEmail!=null and uerEmail!=''">
user_email,
</if>
user_info,head_img,create_time)
VALUES
(#{id},#{userName},#{userPassword},
<if test="userEmail!=null and uerEmail!=''">
#{userEmail},
</if>
#{userInfo},#{headImg,jdbcType=BLOB},#{createTime,jdbcType=TIMESTAMP})
</insert>
From《MyBatis从入门到精通》
MyBatis if标签的用法的更多相关文章
- MyBatis bind标签的用法
From<MyBatis从入门到精通> <!-- 4.5 bind用法 bind标签可以使用OGNL表达式创建一个变量并将其绑定到上下文中. 需求: concat函数连接字符串,在M ...
- MyBatis foreach标签的用法
From<MyBatis从入门到精通> 一.foreach实现in集合 1.映射文件中添加的代码: <!-- 4.4 foreach用法 SQL语句有时会使用IN关键字,例如id i ...
- MyBatis select标签的用法
From<MyBatis从入门到精通> 第一步,在接口中添加方法: public interface UserMapper { SysUser selectById(Long id); } ...
- mybatis001-动态标签Trim用法
Mybatis动态标签Trim用法 一.<trim></trim>标签用法 示例一: select * from user <trim prefix="WHER ...
- 9.mybatis动态SQL标签的用法
mybatis动态SQL标签的用法 动态 SQL MyBatis 的强大特性之一便是它的动态 SQL.如果你有使用 JDBC 或其他类似框架的经验,你就能体会到根据不同条件拼接 SQL 语句有多么 ...
- SpringMVC +mybatis+spring 结合easyui用法及常见问题总结
SpringMVC +mybatis+spring 结合easyui用法及常见问题总结 1.FormatString的用法. 2.用postAjaxFillGrid实现dataGrid 把form表单 ...
- Mybatis foreach标签含义
背景 考虑以下场景: InfoTable(信息表): Name Gender Age Score 张三 男 21 90 李四 女 20 87 王五 男 22 92 赵六 女 19 94 孙七 女 23 ...
- Java-MyBatis-杂项: MyBatis 中 in 的用法2
ylbtech-Java-MyBatis-杂项: MyBatis 中 in 的用法2 1.返回顶部 1. 一.简介 在SQL语法中如果我们想使用in的话直接可以像如下一样使用: select * fr ...
- Java-MyBatis:MyBatis 中 in 的用法
ylbtech-Java-MyBatis-杂项:MyBatis 中 in 的用法 1.返回顶部 1. foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元 ...
随机推荐
- QuickReport根据每行的内容长度动态调整DetailBand1的行高
procedure TPosPubFactureRep.DetailBand1BeforePrint(Sender: TQRCustomBand; var PrintBand: Boolean); v ...
- 微信小程序把玩(五)页面生命周期
原文:微信小程序把玩(五)页面生命周期 这里只要熟悉页面的基本生命周期即可,业务在指定生命周期函数内书写. 以下是官网给出的生命周期函数方法和状态图 上面的生周期函数图对于做Android 或者IOS ...
- 批处理(bat)实现SQLServer数据库备份与还原
原文:批处理(bat)实现SQLServer数据库备份与还原 备份数据库.bat @echo off set path=%path%;C:\Program Files (x86)\Microsoft ...
- [转]Android 如何有效的解决内存泄漏的问题
Android 如何有效的解决内存泄漏的问题 前言:最近在研究Handler的知识,其中涉及到一个问题,如何避免Handler带来的内存溢出问题.在网上找了很多资料,有很多都是互相抄的,没有实际的 ...
- C# winform 主界面打开并关闭登录界面
在winform 界面编程中,我们有时候要在主界面打开之前先显示登录界面,当登录界面用户信息校验正确后才打开主界面,而这时登陆界面也完成使命该功成身退了. 目前有两种方法可实现: 方法1. 隐藏登录界 ...
- Dynamic linking is coming to iOS, tvOS, and watchOS ports of Qt in the 5.9 release
http://blog.qt.io/blog/2017/01/23/qt-5-8-released/ Dynamic linking is coming to iOS, tvOS, and watch ...
- spring+rabbitmq+stomp搭建websocket消息推送(非spring boot方式)
前言: 两年前做过spring+activemq+stomp的ws推送,那个做起来很简单,但现在公司用的mq中间件是rabbitmq,因此需要通过rabbitmq去做ws通信.仔细搜了搜百度/谷歌,网 ...
- java多线程之管道流
java语言中提供了各种各样的流供我们操纵数据,其中管道流(pipeStream)是一种特殊的流,用于在不同线程间直接传送数据. 一个线程发送数据到输出管道,另一个线程从输入管道读取数据,通过使用管道 ...
- python列表和字典的迭代
1.列表和字典的迭代 程序开发中,对列表和字典进行迭代是非常常见的事情. 字典一般可以选择对key进行迭代.对value迭代和对key/value一起迭代 >>> d = {'a': ...
- Laravel --- 【转】安装调试利器 Laravel Debugbar
[转]http://www.tuicool.com/articles/qYfmmur 1.简介 Laravel Debugbar 在 Laravel 5 中集成了 PHP Debug Bar ,用于显 ...