mybatis sql中的條件語句


1.mybatis判斷是否為空或null

<if test="type!=null and type!=''">  
    AND type = #{type}  
</if> 

2.Mybatis中的like查詢

今天要做一個模糊查詢

用的Mybatis

開始寫的是:

select id,bookName,author,publisher,donor,status,createDate,lastUpdate from book   
        <where>  
            <if test="bookName!=null">  
                bookName like '%#{bookName}%'  
            </if>  
            <if test="author!=null">  
                and author like '%#{author}%'  
            </if> 

 

最后改為:

select id,bookName,author,publisher,donor,status,createDate,lastUpdate from book  
        <where>  
            <if test="bookName!=null">  
                bookName like CONCAT('%','${bookName}','%' )  
            </if>  
            <if test="author!=null">  
                and author like CONCAT('%','${author}','%' )  
            </if> 

 

主要還是MyBatis傳值的問題啊

如果不是字符串就沒法替換了

---------------------------------------------------------------------------

yBatis 的動態SQL 是基於OGNL 表達式的,它可以幫助我們方便的在SQL 語句中實現某些邏輯。

MyBatis 中用於實現動態SQL 的元素主要有:

  • if
  • choose (when ,otherwise )
  • trim
  • where
  • set
  • foreach

if

if 就是簡單的條件判斷,利用if 語句我們可以實現某些簡單的條件選擇。先來看如下一個例子:

delete from user   
<where>  
        <if test="id != null">       
    and id= #{id}  
        </if>  
         <if test="code!= null">     
    and code= #{code}  
        </if>  
</where>

if語句可以方便條件不同的SQL重用,但當where后面的if全部不滿足時,就會導致空條件而刪除或更新整個表。

可以在語句里面加上這樣一個語句,避免全表更新或者刪除。

delete from user   
<where>  
     <if test="id == null and code = null">  
           and exception  
    </if>  
        <if test="id != null">       
    and id= #{id}  
        </if>  
         <if test="code!= null">     
    and code= #{code}  
        </if>  
</where>

choose

choose 元素的作用就相當於 JAVA 中的 switch 語句,基本上跟 JSTL 中的 choose 的作用和用法是一樣的,通常都是與 when 和 otherwise 搭配的。看如下一個例子:

<choose>  
   <when test="title != null">  
       and title = #{title}  
   </when>  

   <when test="content != null">  
       and content = #{content}  
   </when>  

   <otherwise>  
         and owner = "owner1"  
    </otherwise>  
</choose>

 

when 元素表示當 when 中的條件滿足的時候就輸出其中的內容,跟 JAVA 中的 switch 效果差不多的是按照條件的順序,當 when 中有條件滿足的時 候,就會跳出 choose ,即所有的 when 和 otherwise 條件中,只有一個會輸出,當所有的我很條件都不滿足的時候就輸出 otherwise 中的 內容。

where

where 語句的作用主要是簡化 SQL 語句中 where 中的條件判斷的,先看一個例子

<where>  
    <if test="title != null">  
        and title = #{title}  
    </if>  
    <if test="content != null">  
        and content = #{content}  
    </if>  
    <if test="owner != null">  
        and owner = #{owner}  
    </if>  
</where>

where 元素的作用是會在寫入 where 元素的地方輸出一個 where ,另外一個好處是你不需要考慮 where 元素里面的條件輸出是什么樣子 的, MyBatis 會智能的幫你處理,如果所有的條件都不滿足那么 MyBatis 就會查出所有的記錄,如果輸出后是 and 開頭的, MyBatis 會把第一個 and 忽略,當然如果是 or 開頭的, MyBatis 也會把它忽略;此外,在 where 元素中你不需要考慮空格的問 題, MyBatis 會智能的幫你加上。

注意,當 Where 元素后面跟着 <foreach open="AND"> ,並不能去除 foreach 元素中的 AND ,這時可以用trim來去除。

trim

trim 元素的主要功能是可以在自己包含的內容前加上某些前綴,也可以在其后加上某些后綴,與之對應的屬性是 prefix 和 suffix ;可以把包含內容的首部某些內容覆蓋,即忽略,也可以把尾部的某些內容覆蓋,對應的屬性是 prefixOverrides 和 suffixOverrides ;正因為 trim 有這樣的功能,所以我們也可以非常簡單的利用 trim 來代替 where 元素的功能

<trim prefix="where" prefixOverrides="and">  
   ......  
</trim>

trim元素后面跟着 <foreach open="AND"> ,可以去除 foreach 元素中的 AND 。

<update id="dynamicSetTest" parameterType="Blog">  
    update t_blog  
    <trim prefix="set" suffixOverrides=",">  
        <if test="title != null">  
            title = #{title},  
        </if>  

        <if test="content != null">  
             content = #{content},  
        </if>  
    </trim>  
    where id = #{id}  
</update>

 

set

set 元素主要是用在更新操作的時候,它的主要功能和 where 元素其實是差不多的,主要是在包含的語句前輸出一個 set ,然后如果包含的語句是以逗號結束的話將會把該逗號忽略,如果 set 包含的內容為空的話則會出錯。有了 set 元素我們就可以動態的更新那些修改了的 字段。下面是一段示例代碼:

<update id="dynamicSetTest" parameterType="Blog">  
  update t_blog  
  <set>  
      <if test="title != null">  
          title = #{title},  
      </if>  

      <if test="content != null">  
           content = #{content},  
      </if>  
  </set>  
  where id = #{id}  
</update>

 

foreach

foreach 的主要用在構建in 條件中,它可以在SQL 語句中進行迭代一個集合。foreach 元素的屬性主 要有item ,index ,collection ,open ,separator ,close 。item 表示集合中每一個元素進行迭代時的別名,index 指定一個名字,用於表示在迭代過程中,每次迭代到的位置,open 表示該語句以什么開始,separator 表示在每次進行迭代之間以什么 符號作為分隔符,close 表示以什么結束,在使用foreach 的時候最關鍵的也是最容易出錯的就是collection 屬性,該屬性是必須指定的,但是在不同情況下,該屬性的值是不一樣的,主要有一下4 種情況:

1、如果傳入的是單參數且參數類型是一個List 的時候,collection 屬性值為list

<select id="dynamicForeachTest" parameterType="java.util.List" resultType="Blog">  
    select * from t_blog where id in  
    <foreach collection="list" index="index" item="item" open="(" separator="," close=")">  
        #{item}  
    </foreach>  
</select>

要注意的是,無論你JAVA代碼傳入的list名字是什么,在collection中名字必須為list。

2、如果傳入的參數是多個的時候,我們就需要把它們封裝成一個Map 了,當然單參數也可以封裝成map ,實際上如果你在傳入參數的時候,在MyBatis 里面也是會把它封裝成一個Map 的,map 的key 就是參數名,所以這個時候collection 屬性值就是傳入的List 或array 對象在自己封裝的map 里面的key

<select id="exists" parameterType="java.util.HashMap" resultType="java.lang.Integer">  
    select count(*) from TEST test   
    <WHERE>  
        <if test="code != null">  
            AND test.CODE = #{code}   
        </if>  
        <if test="idList !=null ">  
            and test.id in(  
            <foreach item="guard" index="index"  collection="idList" separator=",">  
                 #{guard}  
            </foreach>  
            )  
        </if>  
    </WHERE>  
</select>

 

3、 JAVA對象中的list。和map寫法一樣,只是傳入參數時不同

<select id="exists" parameterType="TestDto" resultType="java.lang.Integer">  
    select count(*) from TEST test   
    <WHERE>  
        <if test="code != null">  
            AND test.CODE = #{code}   
        </if>  
        <if test="idList !=null ">  
            and test.id in(  
            <foreach item="guard" index="index"  collection="idList" separator=",">  
                 #{guard}  
            </foreach>  
            )  
        </if>  
    </WHERE>  
</select>

注意這里的parameterType如果是JAVA對象的話,必須先注入進去,或者加上包的路徑。

注入時 配置如下:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <property name="typeAliasesPackage"   
                value="com.test.model,com.test.dto/>  
        <property name="configLocation" value="classpath:mybatis.xml" />  
        <!-- mapper和resultmap配置路徑 -->  
        <property name="mapperLocations">  
            <list>  
                <!-- 表示在mapper包所有目錄中,以-Mapper.xml結尾所有文件 -->  
                <value>classpath:/mapper/*/*Mapper.xml</value>  
            </list>  
        </property>  
</bean>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM