更新時(shí)間:2022-04-15 08:41:51 來源:動(dòng)力節(jié)點(diǎn) 瀏覽2766次
MyBatis標(biāo)簽是大家需要了解的,動(dòng)力節(jié)點(diǎn)小編來為大家介紹一下MyBatis的動(dòng)態(tài)標(biāo)簽。
if label test屬性中有一個(gè),test屬性值是匹配OGNL必選判斷表達(dá)式,表達(dá)式的結(jié)果可以使true或者false,另外,所有非0的值都為true
1.數(shù)值型
(1)for example :
If there is no special requirement when the parameter is numeric, you only need to judge whether it is null that will do .
<if test="id != null"></if>
(2)for example :
If there is a special demand , For example, judge whether it is greater than a certain number . Just add the corresponding conditional judgment .
<if test='id != null and id > 28'></if>
(3)for example :
mybatis There is another form for this greater than less than and so on .
<if test='id != null and id gt 28'></if>
<if test='id != null and id > 28'></if> The two are the same
<if test='id != null and id gte 28'></if>
<if test='id != null and id >= 28'></if> The two are the same
<if test='id != null and id lt 28'></if> normal
<if test='id != null and id < 28'></if> Report errors
<if test='id != null and id lte 28'></if> normal
<if test='id != null and id <= 28'></if> Report errors
對(duì)應(yīng)關(guān)系:
gt Corresponding >
gte Corresponding >=
lt Corresponding <( Will report a mistake The associated "test" Property values cannot contain '<' character )
lte Corresponding <=( Will report a mistake The associated "test" Property values cannot contain '<=' character )
2.字符串類型
(1)for example :
If you do not need to filter empty strings Just judge null that will do
<if test="username != null"></if>
(2)for example :
If you need to filter empty strings , Add an empty string to judge I won't support it && and || , So here we use and or To make logical and or judgments
<if test="username != null and '' != username"></if> perhaps <if test="username != null and '' neq username"></if>
(3)for example :
If you judge whether the string starts with a special character , Ending, etc . Call directly String The corresponding method can be used
<if test="username != null and username.indexOf('ji') == 0"> </if> <!-- Whether it starts with something -->
<if test="username != null and username.indexOf('ji') >= 0"> </if> <!-- Whether it contains a character -->
<if test="username != null and username.lastIndexOf('ji') > 0"></if> <!-- Does it end with something -->
(4)for example :
Whether it is a specific string , Some businesses need this .
<if test="username != null and 'hello' == username"></if> perhaps <if test="username != null and 'hello' eq username"></if>
Be careful :
<if test="username != null and 'hello' == username"></if> There is no problem with this form of writing when the parameter type is a string ,
However, when the parameter type is non string type, it needs to be written as <if test="username != null and 'hello'.toString() == username.toString()"></if>
Just write <if test="username != null and 'hello'.toString() == username"></if> There will also be a great possibility of hanging up .
Maybe you will say why non string should be written like this . This depends on the special needs .
對(duì)應(yīng)關(guān)系:
eq Corresponding ==
neq Corresponding !=
3 Judge list Is it empty
if Condition judgment can directly call the method of the object itself for logical judgment , therefore list Sentenced to empty . You can call .size()>0 perhaps .isEmpty()
for example :<if test="userList != null and userList.isNotEmpty()"></if> , <if test="userList != null and userList.size()>0"></if>
4 map Parameters are the same If the value is taken map.key(map Medium key name ) that will do
標(biāo)簽會(huì)自動(dòng)判斷,如果沒有條件成立,那么在sql中就不會(huì)有...語句中 where關(guān)鍵字
如果有任何條件成立,會(huì)自動(dòng)去掉多余的或者or。(我們不需要添加 1=1 這樣的侵入代碼)
usage :
<select id="listProduct" resultType="Product">
select * from product_
<where>
<if test="name!=null">
and name like concat('%',#{
name},'%')
</if>
<if test="price!=null">
and price > #{
price}
</if>
</where>
</select>
和 where Labels 類似,在 update 語句中也會(huì)出現(xiàn)多個(gè)字段相關(guān)的問題。在這種情況下,您可以使用 set label 。它的作用和 where 標(biāo)簽相似,只有在有數(shù)據(jù)時(shí)才設(shè)置。set 元素可用于動(dòng)態(tài)包含需要更新的列,忽略其他不更新的列,set 元素在行首動(dòng)態(tài)插入 SET 關(guān)鍵字,并刪除多余的逗號(hào)。
usage :
<update id="updateProduct" parameterType="Product" >
update product_
<set>
<if test="name != null">name=#{
name},</if>
<if test="price != null">price=#{
price}</if>
</set>
where id=#{
id}
</update>
trim 有四個(gè)參數(shù),即:
prefix: Prefix(以什么開頭)、
prefixoverride: 去掉第一個(gè)(如“and”還是“or”)
suffix: suffix(以什么結(jié)尾)
suffixoverride:去掉最后一個(gè)標(biāo)記的字符(如“,”)
usage :
<select id="listProduct" resultType="Product">
select *from product_
<trim prefix="WHERE" prefixOverrides="AND |OR ">
<if test="name!=null">
and name like concat('%',#{
name},'%')
</if>
<if test="price!=null and price!=0">
and price > #{
price}
</if>
</trim>
</select>
trim Used to customize the desired functions , such as where The label can be used trim To replace
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...
</trim>
<update id="updateProduct" parameterType="Product" >
update product_
<trim prefix="SET" suffixOverrides=",">
<if test="name != null">name=#{
name},</if>
<if test="price != null">price=#{
price},</if>
</trim>
where id=#{
id}
</update>
set The label can be used trim To replace , function set The code in the tag , The effect is the same .
<trim prefix="SET" suffixOverrides=",">
...
</trim>
有時(shí)候我們不想套用所有的條件,我只想選擇幾個(gè)選項(xiàng)中的一個(gè)。MyBatis 提供了選擇元素,按順序判斷 when 中的條件是否為真,如果一個(gè)成立,則為選擇結(jié)束。當(dāng)您在何時(shí)選擇所有條件時(shí),如果您對(duì)所有條件不滿意,則執(zhí)行其他媒體SQL。類似于 Java 的 switch 語句,按 switch 選擇,when 按大小寫,否則為 default.if 是與 (and) 之間的關(guān)系,并且選擇 Yes 或 (or) 之間的關(guān)系.
<select id="getUserList" resultType="com.it.bean.User" parameterType="com.it.bean.User">
SELECT <include refid="resultParam"></include> FROM User u
<where>
<choose>
<when test="username !=null and username != ''">
u.username LIKE CONCAT(CONCAT('%', #{
username}),'%')
</when >
<when test="sex != null">
AND u.sex = #{
sex}
</when >
<when test="birthday != null ">
AND u.birthday = #{
birthday}
</when >
<otherwise>
AND u.age = #{
age}
</otherwise>
</choose>
</where>
</select>
foreach 標(biāo)簽通常用于在這種語法中。
collection :collection 一個(gè)屬性有三個(gè)取值 list、array、map 三個(gè),對(duì)應(yīng)的參數(shù)類型有:List、 Array 、map aggregate ,我上面?zhèn)鞯膮?shù)是array ,所以取值為array
item :表示迭代過程中每個(gè)元素的別名
index :表示每次迭代在迭代過程中的位置(下標(biāo))
open : 前綴
關(guān)閉:后綴
separator : 分隔符,表示迭代過程中每個(gè)元素是如何分隔的.
<select id="listProduct" resultType="Product">
SELECT * FROM product_
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{
item}
</foreach>
</select>
bind在label中,value對(duì)應(yīng)傳入實(shí)體類的一個(gè)字段,name屬性是賦予對(duì)應(yīng)字段的變量名。在 value 屬性中可以使用字符串拼接等特殊處理。
usage :
<select id="listProduct" resultType="Product">
<bind name="likename" value="'%' + name + '%'" />
select * from product_ where name like #{
likename}
</select>
通過這個(gè)標(biāo)簽,我們可以定義可復(fù)用的 sql 語句片段,在執(zhí)行的 sql 語句標(biāo)簽中可以直接引用。
這樣可以提高編碼效率,也可以有效地簡化代碼,提高可讀性。sql label 封裝SQL語言,包括要調(diào)用的Tag。
usage :
<!-- Definition sql fragment -->
<sql id="orderAndItem"> o.order_id,o.cid,o.address,o.create_date,o.orderitem_id,i.orderitem_id,i.product_id,i.count
</sql>
<select id="findOrderAndItemsByOid" parameterType="java.lang.String" resultMap="BaseResultMap">
select
<!-- quote sql fragment -->
<include refid="orderAndItem" />
from ordertable o
join orderitem i on o.orderitem_id = i.orderitem_id
where o.order_id = #{
orderId}
</select>
0基礎(chǔ) 0學(xué)費(fèi) 15天面授
有基礎(chǔ) 直達(dá)就業(yè)
業(yè)余時(shí)間 高薪轉(zhuǎn)行
工作1~3年,加薪神器
工作3~5年,晉升架構(gòu)
提交申請(qǐng)后,顧問老師會(huì)電話與您溝通安排學(xué)習(xí)
初級(jí) 202925
初級(jí) 203221
初級(jí) 202629
初級(jí) 203743