專注Java教育13年 全國咨詢/投訴熱線:400-8080-105

          首頁 hot資訊 動態MyBatis標簽詳解

          動態MyBatis標簽詳解

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

          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>

           

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

          免費課程推薦 >>
          技術文檔推薦 >>

          返回頂部
          自拍亚洲一区欧美另类 亚洲欧美日本国`产在线观18| 日本的人人为我我为人人| 性xxxxx15学生| 国产综合色香蕉精品五夜婷| 老司机资源在线观看| 无限资源在线观看完整版免费下载| 重庆护士门| 朋友的尤物人妻全文阅读| 天天夜日日日日碰日日摸日日澡| 无码av中文一二三区| 被公侵犯玩弄漂亮人妻| 人人视频| 护士巨好爽好大乳| 高清一卡二卡三卡四卡免费| 护士情欲短篇小说强| 日本av无码一区二区三区| 美国a片| 从紧窄的蜜道中缓缓退出| av片亚洲国产男人的天堂| 男女真人后进式猛烈qq动态图| 19岁rapper潮水| 和少妇高潮30p| 被强奷从反抗到舒服的视频| 婷婷久久综合九色综合88| 从后面糟蹋成功视频| 丝瓜成年app短视频网站| 战少宠妻不能停小说| 无码高清中字av| 无码少妇一区二区| 欧美人与动牲交视频| 亚洲男人第一无码AV网站| http://www.pbn77.com