MyBatis中if,where,set标签
<if>标签
<select id="findActiveBlogWithTitleLike"
resultType="Blog">
SELECT * FROM BLOG
WHERE state = ‘ACTIVE’
<if test="title != null">
AND title like #{title}
</if>
</select>
if标签通常伴随着where,set出现。当增加查询条件的时候有下面的代码
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG WHERE state = ‘ACTIVE’
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</select>
但是当state属性也需要动态表示的时候则变成
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG
WHERE
<if test="state != null">
state = #{state}
</if>
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</select>
此时会出现当state为null时,sql语句会变为 select * from BLOG WHERE AND...解决此问题则引入<where><set>等标签.
<where>标签
<select id="findActiveBlogLike"
resultType="Blog">
SELECT * FROM BLOG
<where>
<if test="state != null">
state = #{state}
</if>
<if test="title != null">
AND title like #{title}
</if>
<if test="author != null and author.name != null">
AND author_name like #{author.name}
</if>
</where>
</select>
where 元素知道只有在一个以上的if条件有值的情况下才去插入“WHERE”子句。而且,若最后的内容是“AND”或“OR”开头的,where 元素也知道如何将他们去除。
如果 where 元素没有按正常套路出牌,我们还是可以通过自定义 trim 元素来定制我们想要的功能。比如,和 where 元素等价的自定义 trim 元素为:
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
同理当需要更新数据时使用<set>标签
<update id="updateAuthorIfNecessary">
update Author
<set>
<if test="username != null">username=#{username},</if>
<if test="password != null">password=#{password},</if>
<if test="email != null">email=#{email},</if>
<if test="bio != null">bio=#{bio}</if>
</set>
where id=#{id}
</update>
MyBatis中if,where,set标签的更多相关文章
- Mybatis中的if test 标签
<if test='patrolResult != null and patrolResult != "" and patrolResult !="1"' ...
- mybatis中resultMap的使用
在mybatis中,使用<select>标签,必须要设置resultType属性 或 resultMap属性 否则会报错! resultType一般是返回简单类型的查询结果,涉及一张表 可 ...
- mybatis 中的where标签
mybatis中的where标签可以去除 开头的 and 或者 or 但是放在后面的不行 失败的: <select id="countNotesByParam" parame ...
- Mybatis中的ognl表达式。及myabtis where标签/if test标签/trim标签
1.mybatis默认支持使用ognl表达式来生成动态sql语句 MyBatis中可以使用OGNL的地方有两处: 动态SQL表达式中 ${param}参数中 上面这两处地方在MyBatis中处理的时候 ...
- mybatis中<include>标签的作用
MyBatis中sql标签定义SQL片段,include标签引用,可以复用SQL片段 sql标签中id属性对应include标签中的refid属性.通过include标签将sql片段和原sql片段进行 ...
- Mybatis中的in查询和foreach标签
Mybatis中的foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separato ...
- Mybatis中选择语句的使用:<choose>标签、分区排序 Row_num() over ()函数的使用呢
1.Mybatis中数据库语句的选择 使用: <choose> <when test="relationType=='L'"> ...
- mybatis中使用使用模块化sql
主要使用到mybatis中的标签 <sql id="tempId"> select * from student <sql> 使用的标签如下: <in ...
- Mybatis中的collection、association来处理结果映射
前不久的项目时间紧张,为了尽快完成原型开发,写了一段效率相当低的代码. 最近几天闲下来,主动把之前的代码优化了一下:) 标签:Java.Mybatis.MySQL 概况:本地系统从另外一个系统得到 ...
- Mybatis特殊字符处理,Mybatis中xml文件特殊字符的处理
Mybatis特殊字符处理,Mybatis中xml文件特殊字符的处理 >>>>>>>>>>>>>>>>& ...
随机推荐
- Appium1.9 之 Chromedriver安装方式
1.在 appium 官网上下载安装后,下载的是1.7.1的版本,安装之后是1.9.1最新版本. 2.appium安装之后,会发现涉及到 浏览器相关的业务时(我使用的是chrome)会提示 “No C ...
- JS实现奇偶数的判断
<html xmlns="http://www.w3.org/1999/xhtml" > <head> <title>标题页-学无忧(www.x ...
- C#异步编程のWPF Invoke和BeginInvoke
我们先看个简单的例子: private void Window_Loaded(object sender, RoutedEventArgs e) { Console.WriteLine("B ...
- 【转】windows 控制台cmd乱码的解决办法
windows 控制台cmd乱码的解决办法 我本机的系统环境: OS Name: Microsoft Windows 10 企业版 OS Version: 10.0.14393 N/A Build 1 ...
- php api 接口
<?php //简单形式 header('Content-Type:text/html;charset=utf-8'); //避免输出乱码 $output = array(); $a = @$_ ...
- openzeppelin-solidity/contracts的代码学习——payment
payment - A collection of smart contracts that can be used to manage payments through escrow arrange ...
- 深入浅出的webpack构建工具--webpack4+vue搭建环境 (十三)
深入浅出的webpack构建工具--webpack4+vue搭建环境 (十三) 从上面一系列的webpack配置的学习,我们现在来使用webpack来搭建vue的开发环境.首先我们来设想下我们的项目的 ...
- 理解Express express.static 和 __direname 及 __firename的含义
理解Express express.static 和 __direname 及 __firename的含义 一:理解 app.use(express.static(__direname + '/pub ...
- android 通讯类资料整理
https://github.com/koush/AndroidAsync(websocket) https://github.com/loopj/android-async-http http:// ...
- ASP.NET Core MVC中Controller的Action,默认既支持HttpGet,又支持HttpPost
我们知道ASP.NET Core MVC中Controller的Action上可以声明HttpGet和HttpPost特性标签,来限制可以访问Action的Http请求类型(GET.POST等). 那 ...