欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解mybatis中的if-else的嵌套使用

 更新時(shí)間:2022年07月08日 09:12:11   作者:舒克日記  
本文主要介紹了mybatis中的if-else的嵌套使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

案例一:if-else

在mybatis的使用過程中,難免會存在使用if-else的邏輯,但是實(shí)際是沒有這種語法的,提供了choose標(biāo)簽來替代這種語法

   <select id="selectUserByState" resultType="com.bz.model.entity.User">
    SELECT
      *
    FROM
      user
    WHERE
      1=1
    <choose>
      <when test="state == 1">
        AND name = #{name1}
      </when>
     <when test="state == 2">
        AND name = #{name2}
      </when>
      <otherwise>
        AND name = #{name3}
      </otherwise>
    </choose>
  </select>

案例二:if嵌套

在實(shí)際的編碼過程中會對一些條件進(jìn)行重復(fù)判斷,并對內(nèi)深入if判斷,這時(shí)就可以使用if嵌套

  <select id="selectUserByState" resultType="com.bz.model.entity.User">
    SELECT
      *
    FROM
      user
    WHERE
     <if test=" gender!=null and gender!='' "> 
         <if test=" gender==male ">
             and name=#{name}
         </if>
     </if>
  </select>

MyBatis中if和choose的嵌套

    <!-- public List<VadtaxShow> findList(VadtaxShow vadtaxShow); -->
    <select id="findList" parameterType="com.cdqyzj_WC.Backstage.vaddedtax.domain.VaddeTax" resultType="com.cdqyzj_WC.Backstage.vaddedtax.domain.VaddeTax">
    SELECT t.addedId,t.taxType,t.totalSales,t.outputTax,t.inputTax,t.entryAmount, t.amountTax,t.retentionTax,
    t.createTime, t.taxTime,t.comId,c.comName,c.comType
    FROM t_g_vaddedtax AS t JOIN t_ucompany AS c ON c.comId = t.comId
    <where>
    1=1
            <if test="comType != '' and comType != null"> and c.comType = #{comType}</if>
            <if test="taxTime != null and taxTime != ''"> and t.taxTime =#{taxTime} </if>    
            <if test="taxType != null and taxType != '' "> and t.taxType =#{taxType} </if>
            <if test="comId != null and comId != '' and comId != 0 "> and t.comId =#{comId} </if>    
            <if test="start_times != null and end_times != null">
                <choose>
                    <when test="middle_times != null">
                        and t.createTime in ('${start_times}','${middle_times}', '${end_times}' )
                    </when>
                    <otherwise>
                        and t.createTime in ('${start_times}','${end_times}' )
                    </otherwise>
                </choose>
            </if>
            <if test="orderBy != null and orderType != '' ">
                     order by ${orderBy}  ${orderType}
            </if>
            <if test="pageSize != 0 ">
                    limit ${startRows},${pageSize}
            </if>
    </where>
    </select>

功能實(shí)現(xiàn)之后反思:要不我不對“middle_times”判空?這樣不就不用嵌套了嗎?

    <!-- public List<VadtaxShow> findList(VadtaxShow vadtaxShow); -->
    <select id="findList" parameterType="com.cdqyzj_WC.Backstage.vaddedtax.domain.VaddeTax" resultType="com.cdqyzj_WC.Backstage.vaddedtax.domain.VaddeTax">
    SELECT t.addedId,t.taxType,t.totalSales,t.outputTax,t.inputTax,t.entryAmount, t.amountTax,t.retentionTax,
    t.createTime, t.taxTime,t.comId,c.comName,c.comType
    FROM t_g_vaddedtax AS t JOIN t_ucompany AS c ON c.comId = t.comId
    <where>
    1=1
            <if test="comType != '' and comType != null"> and c.comType = #{comType}</if>
            <if test="taxTime != null and taxTime != ''"> and t.taxTime =#{taxTime} </if>    
            <if test="taxType != null and taxType != '' "> and t.taxType =#{taxType} </if>
            <if test="comId != null and comId != '' and comId != 0 "> and t.comId =#{comId} </if>    
            <if test="start_times != null and end_times != null">
                and t.createTime in ('${start_times}','${middle_times}', '${end_times}' )
            </if>
            <if test="orderBy != null and orderType != '' ">
                     order by ${orderBy}  ${orderType}
            </if>
            <if test="pageSize != 0 ">
                    limit ${startRows},${pageSize}
            </if>
    </where>
    </select>

到此這篇關(guān)于詳解mybatis中的if-else的嵌套使用的文章就介紹到這了,更多相關(guān)mybatis if-else嵌套內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論