一、Mybatis標籤有哪些
Mybatis是一種優秀的持久層框架,它主要是對JDBC進行封裝,Mybatis的標籤可以說是它靈活性的主要體現。Mybatis的標籤主要分為兩類:CRUD標籤和SQL標籤。其中,CRUD標籤用於增刪改查操作,SQL標籤用於自定義SQL。下面是Mybatis中主要的標籤:
<select></select>
<insert></insert>
<update></update>
<delete></delete>
<if></if>
<choose></choose>
<when></when>
<otherwise></otherwise>
<where></where>
<set></set>
<foreach></foreach>
<bind></bind>
<trim></trim>
<if></if>
<otherwise></otherwise>
二、Mybatis的if標籤使用
Mybatis的if標籤主要是用於在SQL語句中添加條件判斷,用於動態拼接SQL語句。它的使用方法如下:
<select id="findUsers" parameterType="java.util.Map" resultType="User">
select * from user
where 1=1
<if test="username != null">and username like '%${username}%'</if>
<if test="email != null">and email=#{email}</if>
</select>
上面的代碼中,我們通過if標籤實現了動態的SQL語句拼接,如果username和email存在,則會將它們作為查詢條件動態添加到SQL語句中,如果不存在,則不會添加。
三、Mybatis標籤大全
除了if標籤之外,Mybatis還提供了很多其他的標籤,它們各自的作用也不同,下面是Mybatis標籤大全:
- <insert>:插入數據
- <update>:更新數據
- <delete>:刪除數據
- <select>:查詢數據
- <if>:動態添加SQL語句(上面已經介紹過了)
- <choose></choose>:當多個if的test條件都為false時,才會執行,相當於Java中的switch-case語句
- <when></when>:choose標籤下面的if條件,可以有多個when標籤
- <otherwise>:choose標籤下面所有的if的條件都為false時,就會執行otherwise裡面的代碼
- <where></where>:在SQL語句中動態添加WHERE關鍵字(類似於if標籤)
- <set></set>:在SQL語句中動態添加SET關鍵字
- <foreach></foreach>:可以遍歷一個集合,並在SQL語句中從這個集合中取值
- <bind></bind>:給OGNL表達式取別名
- <trim></trim>:可以去除SQL語句中的一些不必要的空格或者逗號,類似於Java中的trim()方法
四、Mybatisif標籤
除了Mybatis提供的標籤之外,我們還可以結合Java的語法,通過if語句來動態拼接SQL語句。
<select id="findUsers" parameterType="java.util.Map" resultType="User">
select * from user
where 1=1
<if test="username != null">and username like '%${username}%'</if>
<if test="email != null">and email like '%${email}%'</if>
<if test="age != null">and age = #{age}</if>
</select>
在上面的代碼中,我們結合了Java的if語句來動態拼接SQL語句,如果age不為null,則會加入age的等值判斷,否則不會加入。
五、Mybatistrim標籤
Mybatistrim標籤的作用是去除SQL語句中不必要的空格或者逗號。
<select id="findUsers" parameterType="java.util.Map" resultType="User">
select * from user
<trim prefix="where" prefixOverrides="and |or">
<if test="username != null">
and username like '%${username}%'
</if>
<if test="email != null">
and email like '%${email}%'
</if>
</trim>
</select>
上面的代碼中,我們用了<trim prefix=”where” prefixOverrides=”and |or”></trim>去掉了SQL語句中多餘的and或者or關鍵字。如果username和email存在,則會將它們作為查詢條件動態添加到SQL語句中,如果不存在,則不會添加。同時,<trim prefix=”where”></trim>中的prefix屬性表示在SQL語句中添加WHERE關鍵字。
六、Mybatis標籤
Mybatis標籤可以說是Mybatis的精華所在,通過靈活使用標籤,可以讓我們輕鬆地編寫出動態的SQL語句。下面我們再回顧一下Mybatis中主要使用的標籤:
- <if>
- <choose></choose>
- <when></when>
- <otherwise>
- <where></where>
- <set></set>
- <foreach></foreach>
- <bind></bind>
- <trim></trim>
- <if></if>
- <otherwise></otherwise>
七、Mybatis的else標籤使用
通過前面的介紹,我們已經知道了Mybatis中常用的標籤,現在可以說是輕鬆理解Mybatiselse標籤的作用。Mybatiselse標籤主要是在if標籤中使用,用於對if條件的反轉,效果相當於Java中的else分支,下面的代碼展示了如何使用Mybatiselse標籤:
<select id="findUsers" parameterType="java.util.Map" resultType="User">
select * from user
<if test="age != null">
<if test="age < 20">
and age < 20
</if>
<if test="age >= 20 and age < 30">
and age >= 20 and age < 30
</if>
<if test="age >= 30">
and age >= 30
</if>
<else>
select * from user
</if>
</select>
在上面的代碼中,我們使用了<else></else>標籤來實現if條件的反轉,如果age不為null,則會按照不同的年齡區間來動態的拼接SQL語句;如果age為null,則會查詢全部用戶信息。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/257634.html