大战熟女丰满人妻av-荡女精品导航-岛国aaaa级午夜福利片-岛国av动作片在线观看-岛国av无码免费无禁网站-岛国大片激情做爰视频

專注Java教育14年 全國(guó)咨詢/投訴熱線:400-8080-105
動(dòng)力節(jié)點(diǎn)LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁(yè) hot資訊 動(dòng)態(tài)MyBatis標(biāo)簽詳解

動(dòng)態(tài)MyBatis標(biāo)簽詳解

更新時(shí)間:2022-04-15 08:41:51 來(lái)源:動(dòng)力節(jié)點(diǎn) 瀏覽2878次

MyBatis標(biāo)簽是大家需要了解的,動(dòng)力節(jié)點(diǎn)小編來(lái)為大家介紹一下MyBatis的動(dòng)態(tài)標(biāo)簽。

if label

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

where label

標(biāo)簽會(huì)自動(dòng)判斷,如果沒(méi)有條件成立,那么在sql中就不會(huì)有...語(yǔ)句中 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>

set label

和 where Labels 類似,在 update 語(yǔ)句中也會(huì)出現(xiàn)多個(gè)字段相關(guān)的問(wè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 標(biāo)簽

trim 有四個(gè)參數(shù),即:

prefix: Prefix(以什么開(kāi)頭)、

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>

choose when else label

有時(shí)候我們不想套用所有的條件,我只想選擇幾個(gè)選項(xiàng)中的一個(gè)。MyBatis 提供了選擇元素,按順序判斷 when 中的條件是否為真,如果一個(gè)成立,則為選擇結(jié)束。當(dāng)您在何時(shí)選擇所有條件時(shí),如果您對(duì)所有條件不滿意,則執(zhí)行其他媒體SQL。類似于 Java 的 switch 語(yǔ)句,按 switch 選擇,when 按大小寫(xiě),否則為 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)簽

foreach 標(biāo)簽通常用于在這種語(yǔ)法中。

collection :collection 一個(gè)屬性有三個(gè)取值 list、array、map 三個(gè),對(duì)應(yīng)的參數(shù)類型有:List、 Array 、map aggregate ,我上面?zhèn)鞯膮?shù)是array ,所以取值為array

item :表示迭代過(guò)程中每個(gè)元素的別名

index :表示每次迭代在迭代過(guò)程中的位置(下標(biāo))

open : 前綴

關(guān)閉:后綴

separator : 分隔符,表示迭代過(guò)程中每個(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

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>

sql Fragment tags

通過(guò)這個(gè)標(biāo)簽,我們可以定義可復(fù)用的 sql 語(yǔ)句片段,在執(zhí)行的 sql 語(yǔ)句標(biāo)簽中可以直接引用。

這樣可以提高編碼效率,也可以有效地簡(jiǎn)化代碼,提高可讀性。sql label 封裝SQL語(yǔ)言,包括要調(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>

 

提交申請(qǐng)后,顧問(wèn)老師會(huì)電話與您溝通安排學(xué)習(xí)

  • 全國(guó)校區(qū) 2025-10-10 搶座中
免費(fèi)課程推薦 >>
技術(shù)文檔推薦 >>
主站蜘蛛池模板: 综合另类 | 黄色a级毛片 | 久久91视频| 高清亚洲综合色成在线播放放 | 91精品国产高清久久久久久io | 日本综合视频 | 亚洲色无码播放 | jazz欧美人免费xxxxxx | 日韩中文字幕久久精品 | 玖玖在线国产精品 | 亚洲综合精品香蕉久久网 | 一区二区三区中文 | 国产99高清一区二区 | 色综合久久夜色精品国产 | 无遮挡又黄又爽又色的视频免费 | 女人牲交视频一级毛片 | 国产一级特黄aa级特黄裸毛片 | 毛片免费观看的视频 | 日本波多野结衣字幕久久 | 国产精品久久久久久久久 | 久久国产影视免费精品 | 福利资源站 | 亚洲视频二区 | 日本涩涩网站 | 日韩一区二区三区视频 | 99爱在线观看精品视频 | 久久这里有精品 | 国产网红在线 | 亚洲区一区 | 国内国产精品天干天干 | 大片国产片日本观看免费视频 | 欧美精品成人一区二区视频一 | 国产成人精品亚洲日本在线观看 | 亚洲欧美日韩在线不卡 | 国产免费69成人精品视频 | 69久久| 日日狠狠 | 狠狠久久综合 | 免费在线精品视频 | 999国产精品999久久久久久 | 国产午夜视频在线观看第四页 |