偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

帶你了解下MyBatis的動態(tài)SQL!

運維 數據庫運維
MyBatis的強大特性之一便是它的動態(tài)SQL,以前拼接的時候需要注意的空格、列表最后的逗號等,現在都可以不用手動處理了,MyBatis采用功能強大的基于OGNL的表達式來實現,下面主要介紹下。

MyBatis的強大特性之一便是它的動態(tài)SQL,以前拼接的時候需要注意的空格、列表最后的逗號等,現在都可以不用手動處理了,MyBatis采用功能強大的基于OGNL的表達式來實現,下面主要介紹下。

[[283139]]

一、if標簽

if是最常用標簽,經常用在判斷語句上,可以實現某些簡單的條件選擇。基本使用示例如下:

  1. <select id="queryAllUsersByName" resultType="com.example.springboot.mybatisxml.entity.User"
  2.        select * from user where 1=1 
  3.        <if test="name != null and name != ''"
  4.            and name = #{name
  5.        </if> 
  6.        <if test="age != null "
  7.            and age = #{age} 
  8.        </if> 
  9.    </select

二、where標簽

上面的例子中使用了“1=1”,是為了避免后續(xù)條件不滿足時候報錯,那有沒有辦法避免這種寫法呢?

當然有,就是接下來要說的where,where標簽會自動判斷如果包含的標簽中有返回值的話,就在sql中插入一個where,如果where標簽最后返回的內容是以and 或者or開頭的,也會被自動移除掉,上面例子中換成where標簽后寫法如下:

  1. <select id="queryAllUsersByName" resultType="com.example.springboot.mybatisxml.entity.User"
  2.         select * from user  
  3.         <where
  4.             <if test="name != null and name != ''"
  5.                 and name = #{name
  6.             </if> 
  7.             <if test="age != null "
  8.                 and age = #{age} 
  9.             </if> 
  10.         </where
  11.     </select

三、trim標簽

trim的作用是去除特殊的字符串,它的prefix屬性代表語句的前綴,prefixOverrides屬性代表需要去除的哪些特殊字符串,prefixOverrides屬性會忽略通過管道分隔的字符,后綴的處理和前綴一樣。

trim標簽的主要屬性如下

  • prefix:前綴覆蓋并增加其內容。
  • suffix:后綴覆蓋并增加其內容。
  • prefixOverrides:前綴判斷的條件。
  • suffixOverrides:后綴判斷的條件。

舉兩個例子。

使用前綴屬性

  1. <select id="queryAllUsersByName" resultType="com.example.springboot.mybatisxml.entity.User"
  2.        select * from user 
  3.        <trim prefix="WHERE" prefixOverrides="AND |OR " > 
  4.            <if test="name != null and name != ''"
  5.                and name = #{name
  6.            </if> 
  7.            <if test="sex != null "
  8.                or sex = #{sex} 
  9.            </if> 
  10.            <if test="age != null "
  11.                and age = #{age} 
  12.            </if> 
  13.        </trim> 
  14.    </select

使用后綴屬性

  1. <update id="update" parameterType="Object"
  2.         UPDATE user 
  3.         <trim prefix=" SET " prefixOverrides=","
  4.             <if test="id != null ">,id=#{id}</if> 
  5.             <if test="name != null ">,name=#{name}</if> 
  6.             <if test="age != null ">,age=#{age}</if> 
  7.         </trim> 
  8.         WHERE ID=#{id} 
  9.     </update

四、foreach標簽

foreach的作用是遍歷集合,它能夠很好地支持數組和List、Set接口的集合的遍歷,往往和sql中的in組合比較多。

foreach標簽的主要屬性如下

  • item:表示循環(huán)中當前的元素。
  • index:表示當前元素在集合的位置下標。
  • collection:配置list的屬性名等。
  • open和close:配置的是以什么符號將這些集合元素包裝起來。
  • separator:配置的是各個元素的間隔符。

舉個例子:

  1. <select id="queryAllUsersByName" resultType="com.example.springboot.mybatisxml.entity.User"
  2.         select * from user where id in 
  3.         <foreach item="id" index="index" collection="userList" 
  4.                  open="(" separator="," close=")"
  5.             #{id} 
  6.         </foreach> 
  7.     </select
責任編輯:武曉燕 來源: Java碎碎念
相關推薦

2015-08-26 16:38:37

mybatissql

2021-03-02 09:15:24

MyBatisSQL數據庫

2021-12-06 22:44:51

Windows 10Windows微軟

2011-04-01 13:56:12

SQL Server數

2020-07-07 07:34:29

RedisSDS數據結構

2022-09-26 11:30:40

MQTT協(xié)議客戶端協(xié)議

2023-01-06 19:19:16

TensorFlow

2010-08-31 22:29:54

DHCP分配

2019-09-27 09:40:06

ElvishShellLinux

2010-07-05 16:20:32

NetBEUI協(xié)議

2020-03-10 14:30:14

動態(tài)路由RIP網關協(xié)議

2024-02-04 09:24:45

MyBatisSQL語句Spring

2020-02-10 10:55:37

路由協(xié)議OSPFLSA

2020-03-22 21:32:37

動態(tài)路由OSPFLSA

2018-07-30 13:51:06

區(qū)塊鏈物聯(lián)網大數據

2018-09-18 14:34:43

GIT系統(tǒng)實踐

2018-09-04 16:20:46

MySQ索引數據結構

2020-12-31 12:16:49

SAP云計算SAP產品

2021-02-03 16:22:43

新基建SAP

2024-06-12 08:36:25

點贊
收藏

51CTO技術棧公眾號