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元 ...
随机推荐
- vs2017 cordova调试android app
方案是:virtualbox + androidx86 7.0+_x64.iso虚拟机方式
- Qt PNG 背景透明
本文主要是解决Qt中QGraphicsAbstractShapeItem,QPixmap,QPainter等组件的透明化显示问题. 在Qt中定义了一个常量,用于设置透明的颜色,即Qt::transpa ...
- .NET环境下有关打印页面设置、打印机设置、打印预览对话框的实现
原文:.NET环境下有关打印页面设置.打印机设置.打印预览对话框的实现 我个人认为,开发MIS,首先就得解决网格的问题,而开发工具为我们提供了如DataGrid.MSHFlexGrid的控件.其次,是 ...
- Tensorflow数据读取机制
展示如何将数据输入到计算图中 Dataset可以看作是相同类型"元素"的有序列表,在实际使用时,单个元素可以是向量.字符串.图片甚至是tuple或dict. 数据集对象实例化: d ...
- SOA 相关开发调试软件
开发工具 IntelliJ IDEA:https://www.jetbrains.com/idea/ SOA调试 soapui:http://www.soapui.org/ wcfstorm:http ...
- MinGW64 how-to(内含编译openssl,libjpeg,libcurl等例子)
Index of contents Setting up the MinGW 64 environment Step 1) building libiconv Step 2) building lib ...
- <iOS小技巧>UIview指定设置控件圆角
一.用法: 众所周知,设置控件的圆角使用layer.cornerRadius属性即可,但是这样设置成的结果是4个边角都是圆角类型. 利用班赛尔曲线画角: //利用班赛尔曲线画角 UIB ...
- sql 日志恢复
可能有不少朋友遇到过这样的问题: update或delete语句忘带了where子句,或where子句精度不够,执行之后造成了严重的后果,这种情况的数据恢复只能利用事务日志的备份来进行,所以如果你的S ...
- wed前端html/css简单理解
开发工具: txt文本 / dreamwave:DW(cs6/cc) / Hbuilder / webstorm / sublime / vscode 前端: 知识架构: 3层: 结构 / 表现 / ...
- Liferay6.1 配置友好的URL映射
说明:以下内容和官方文档相差不大,如果您英文较好,建议直接去读官方文档,地址是:https://dev.liferay.com/develop/tutorials/-/knowledge_base/6 ...