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>

?

轉載于:https://www.cnblogs.com/dyc940210/p/7371672.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/252077.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/252077.shtml
英文地址,請注明出處:http://en.pswp.cn/news/252077.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python3基礎 __repr__ 類的實例對象的名字 可以打印文字(1)

引用自&#xff1a;http://www.bubuko.com/infodetail-1918622.html 這個__repr__的作用從下邊的例子中可以看出,返回實例化對象的表達 code: class MyClass() :def __str__(self) :return "我是MyClass的一個實例"def __repr__(self) :return "這回連print都省…

Day03:文件打開;錯誤處理

錯誤處理 try: #要執行的代碼 except 錯誤的類型&#xff08;可選&#xff09;: #發生錯誤時執行的代碼 finally: #有沒有發生錯誤都執行的代碼 復制代碼with open() as 變量名&#xff1a; with提供一種叫上下文管理協議的python技術&#xff0c;系統會自動關閉文件 open() 默…

Python: pip升級報錯了:You are using pip version 10.0.1, however version 20.3.3 is available.

1,Python使用命令&#xff1a;python -m pip install --upgrade pip升級pip的時候報了下面這個錯 2,換了個命令&#xff1a; python -m pip install --upgrade pip -i https://pypi.douban.com/simple 更新成功了&#xff0c;但又報了一個新的錯誤&#xff1a; AttributeError:…

新手上路之Hibernate:第一個Hibernate例子

一、Hibernate概述 &#xff08;一&#xff09;什么是Hibernate&#xff1f; Hibernate核心內容是ORM&#xff08;關系對象模型&#xff09;。可以將對象自動的生成數據庫中的信息&#xff0c;使得開發更加的面向對象。這樣作為程序員就可以使用面向對象的思想來操作數據庫&…

模板標簽及模板的繼承與引用

1.常用的模板標簽 - 作用是什么:提供各種邏輯 view.py: def index(request):#模板標簽 --常用標簽 總結&#xff1a;語法 {% tag %} {% endtag %} {% tag 參數 參數 %} 示例 展示頁index.html&#xff0c;包含for標簽&#xff0c;if標簽&#xff0c;url標簽 {% extends teacher…

文件夾操作之創建

創建文件夾可通過Directory類的CreateDirectory方法來實現格式為&#xff1a;Directory.CreateDirectory(“文件路徑”)&#xff1b;String path“C:\Users\Administrator\Desktop\51zxw”&#xff1b; If&#xff08;Directory.exists&#xff08;path&#xff09;&#xff09…

doxygen

http://www.doxygen.nl/轉載于:https://www.cnblogs.com/zengkefu/p/7383793.html

C#:RichTextBox 追加其它顏色的行列

1、新建靜態擴展方法public static class RichTextBoxExtension{public static void AppendTextColorful(this RichTextBox rtBox, string text, Color color, bool addNewLine true){if (addNewLine){text Environment.NewLine;}rtBox.SelectionStart rtBox.TextLength;rtB…

Golang實現一個密碼生成器

小地鼠防止有人偷他的果實&#xff0c;在家里上了一把鎖。這個鎖怎么來的呢&#xff1f;請往下看。。 package mainimport ("flag""fmt""math/rand""time" )var (length intcharset string )const (NUmStr "0123456789"C…

Java基礎知識(二)

1、String、StringBuffer、StringBuilder 操作少量數據->String單線程操作字符串緩沖區下操作大量數據->StringBuilder多線程操作字符串緩沖區下操作大量數據->StringBuffer可變性&#xff1a;String類中使用final關鍵字private final char value[]&#xff0c;所以St…

C# WPF:初識布局容器

StackPanel堆疊布局 StackPanel是簡單布局方式之一&#xff0c;可以很方便的進行縱向布局和橫向布局 StackPanel默認是縱向布局的 <Window x:Class"WpfApplication1.MainWindow" xmlns"http://schemas.microsoft.com/winfx/2006/xaml/presentation" …

Kibana源碼分析--Hapijs路由設置理解筆記

【ES6解構賦值】&#xff1a;https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment 【Joi APi】&#xff1a;https://github.com/hapijs/joi/blob/v13.1.2/API.md 轉載于:https://www.cnblogs.com/lishidefengchen/p/866874…

Python打包EXE神器 pyinstaller

最近由于項目需要&#xff0c;以前的python文件需要編輯為EXE供前端客戶使用。 由于最早接觸的是distutils&#xff0c;所以一開始準備使用distutils和py2exe搭配來進行python的exe化&#xff0c;也就是傳統的使用setup.py的方式來進行exe安裝。但是結果都不是很好&#xff0c;…

好程序員HTML5前端教程-css的引入方式和選擇器

好程序員HTML5前端教程-css的引入方式和選擇器 01.引入css方式&#xff08;重點掌握&#xff09; 行內樣式 內接樣式 外接樣式      3.1 鏈接式      3.1 導入式 css介紹 現在的互聯網前端分三層&#xff1a; HTML&#xff1a;超文本標記語言。從語義的角度描述頁面結…

4.4.6 數組也能無鎖:AtomicIntegerArray

數組也可以實現cas操作&#xff0c;有以下幾個類以及用法如下&#xff1a; public class AtomicTntegerArrayTest {public static void main(String[] args) {AtomicIntegerArray atomicIntegerArraynew AtomicIntegerArray(3);AtomicLongArray atomicIntegerArray1new AtomicL…

20種PLC元件編號和Modbus編號地址對應表

1、三菱&#xff1a; X元件支持Modbus之02功能碼&#xff1b; Y元件支持Modbus之01、05、15功能碼&#xff1b; D元件支持Modbus之03、06、16功能碼。 2、西門子&#xff1a; I元件支持Modbus之02功能碼&#xff1b; Q元件支持Modbus之01、05、15功能碼&#xff1b; V元件…

暑期學習

由于最后大作業的呈現情況與短學期所完成的還相差甚遠&#xff0c;所以在暑期的時候開始進一步的細化。 在這個過程之中產生了如下的問題&#xff1a; 已解決的有&#xff1a; 1.用a標簽在同一頁面實現跳轉。 要點&#xff1a;標記<a href"../home#pre">的時候…

五、RabbitMQ的消息屬性(讀書筆記)

2019獨角獸企業重金招聘Python工程師標準>>> 簡介 當使用RabbitMQ發布消息時&#xff0c;消息又AMQP規范中的三個低層幀類型組成&#xff1a; Basic.publish方法幀&#xff1b;內容頭幀&#xff1b;消息體幀&#xff1b;這三種幀類型按順序一起工作&#xff0c;以便…

異步和單線程

轉載于:https://www.cnblogs.com/sunmarvell/p/8674748.html

windows下解決mysql5中文亂碼的問題

1.問題描述&#xff1a;一開始無論是在命令行&#xff0c;還是在mysql的客戶端輸入中文都會出現 “???” 問題之類的亂碼問題&#xff1b; 2.解決辦法&#xff1a; 1&#xff09;cmd 進入mysql &#xff0c;命令mysql -uroot -p123456 2&#xff09;然后執行 show variable…