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

專注Java教育14年 全國咨詢/投訴熱線:400-8080-105
動力節點LOGO圖
始于2009,口口相傳的Java黃埔軍校
首頁 hot資訊 動態MyBatis標簽詳解

動態MyBatis標簽詳解

更新時間:2022-04-15 08:41:51 來源:動力節點 瀏覽2558次

MyBatis標簽是大家需要了解的,動力節點小編來為大家介紹一下MyBatis的動態標簽。

if label

if label test屬性中有一個,test屬性值是匹配OGNL必選判斷表達式,表達式的結果可以使true或者false,另外,所有非0的值都為true

1.數值型

(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 

對應關系:

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 .

對應關系:

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

標簽會自動判斷,如果沒有條件成立,那么在sql中就不會有...語句中 where關鍵字

如果有任何條件成立,會自動去掉多余的或者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 語句中也會出現多個字段相關的問題。在這種情況下,您可以使用 set label 。它的作用和 where 標簽相似,只有在有數據時才設置。set 元素可用于動態包含需要更新的列,忽略其他不更新的列,set 元素在行首動態插入 SET 關鍵字,并刪除多余的逗號。

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 標簽

trim 有四個參數,即:

prefix: Prefix(以什么開頭)、

prefixoverride: 去掉第一個(如“and”還是“or”)

suffix: suffix(以什么結尾)

suffixoverride:去掉最后一個標記的字符(如“,”)

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

有時候我們不想套用所有的條件,我只想選擇幾個選項中的一個。MyBatis 提供了選擇元素,按順序判斷 when 中的條件是否為真,如果一個成立,則為選擇結束。當您在何時選擇所有條件時,如果您對所有條件不滿意,則執行其他媒體SQL。類似于 Java 的 switch 語句,按 switch 選擇,when 按大小寫,否則為 default.if 是與 (and) 之間的關系,并且選擇 Yes 或 (or) 之間的關系.

<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 標簽

foreach 標簽通常用于在這種語法中。

collection :collection 一個屬性有三個取值 list、array、map 三個,對應的參數類型有:List、 Array 、map aggregate ,我上面傳的參數是array ,所以取值為array

item :表示迭代過程中每個元素的別名

index :表示每次迭代在迭代過程中的位置(下標)

open : 前綴

關閉:后綴

separator : 分隔符,表示迭代過程中每個元素是如何分隔的.

<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對應傳入實體類的一個字段,name屬性是賦予對應字段的變量名。在 value 屬性中可以使用字符串拼接等特殊處理。

usage :

    <select id="listProduct" resultType="Product">
        <bind name="likename" value="'%' + name + '%'" />
        select * from   product_  where name like #{
    likename}
    </select>

sql Fragment tags

通過這個標簽,我們可以定義可復用的 sql 語句片段,在執行的 sql 語句標簽中可以直接引用。

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

 

提交申請后,顧問老師會電話與您溝通安排學習

免費課程推薦 >>
技術文檔推薦 >>
主站蜘蛛池模板: 国产精品永久免费自在线观看 | 精品亚洲一区二区在线播放 | 久久久久青草 | 久久久久久国产精品视频 | 日韩欧美久久一区二区 | 国产高清视频免费 | 天天色天天舔 | 国产香蕉在线精彩视频 | 八戒久久精品一区二区三区 | 狠狠色噜噜 | 黄色影院免费观看 | 国产亚洲精品一品区99热 | 一级特黄性色生活片 | 久久一日本道色综合久久 | 黄色录像日本 | 亚洲福利精品一区二区三区 | 四虎精品免费久久 | 啪啪网站色大全免费 | 久久在线精品视频 | 亚洲色欧美 | 偷偷鲁国内视频视频在线 | 国产精品免费播放 | 亚洲精品一区二区三区五区 | 国产一区二区日韩欧美在线 | 全免费毛片在线播放 | 国产成人系列 | 国产一区二区三区不卡在线观看 | 欧美一级久久 | a亚洲欧美中文日韩在线v日本 | 欧美 日韩 中文字幕 | 99视频有精品视频免费观看 | 日本久久久久一级毛片 | 欧洲色网站 | 北条麻妃手机在线观看 | 国产91在线|亚洲 | 久久精品乱子伦免费 | 亚洲国产福利精品一区二区 | a级高清观看视频在线看 | 日本不卡不码高清免费观看 | 日本一区二区三区四区五区 | 免费视频不卡 |