最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Mybatis模糊查詢和動態(tài)sql語句的用法

 更新時間:2019年03月27日 10:37:23   作者:LLY19960418  
今天小編就為大家分享一篇關(guān)于Mybatis模糊查詢和動態(tài)sql語句的用法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

Mybatis 模糊查詢和動態(tài)sql語句

模糊查詢

對數(shù)據(jù)庫最常用的操作就是查詢了,但是如何使用Mybatis進行模糊查詢呢?下面先看一個簡單的模糊查詢

  <select id="select01" resultMap="BasicResultMap">
   SELECT 
   * 
  FROM
   oa_employee 
  WHERE emp_name LIKE #{asd} 
  </select>

這是一條偽模糊查詢, 因為沒有實現(xiàn)真正的模糊 “%”。參數(shù)為字符串,所以#{}中內(nèi)容不被限制。但是應(yīng)該如何插入 % 字符呢。 我們首先想到的是傳遞字符串參數(shù)時將%插入到字符串中 “張%”,但是這種方法操作略微繁瑣了一些。 下面提供了使用sql方法的策略

 <select id="select01" resultMap="BasicResultMap">
   SELECT 
   * 
  FROM
   oa_employee 
  WHERE emp_name LIKE CONCAT( #{asd} ,'%')
  </select>

另外一種不推薦的寫法給大家

<select id="select01" resultMap="BasicResultMap">
   SELECT 
   * 
  FROM
   oa_employee 
  WHERE emp_name LIKE '${emp_name}%'
  </select>

#{} 是采用預(yù)編譯的寫法,也就是JDBC中的PerpareStatement,這種寫法可以防止sql注入,但${}這種寫法是不采用預(yù)編譯,其中的參數(shù)寫成類中的屬性或者map的key值或者為接口中注解的參數(shù)名。

mybatis 提供了bind 標簽。下面舉個例子

 <select id="select01" resultMap="BasicResultMap">
  <bind name="emp_name" value="'%'+ _parameter.getEmp_name() +'%'"/>
   SELECT 
   * 
  FROM
   oa_employee 
  WHERE emp_name LIKE #{emp_name}
  </select>

他是在#{}表達式自動填入value值,值得注意的是“_parameter.getEmp_name()” 調(diào)用的方法是對象中作為查詢參數(shù)的屬性的get方法

多條件查詢

多種條件查詢的要點是判斷查詢條件是否為空,拼接sql語句。在mybatis中提供了if標簽和where 標簽。 下面來介紹兩種標簽的用法。

if標簽

<select id="select01" resultMap="BasicResultMap"> 
SELECT 
* 
FROM 
oa_employee 
WHERE 1=1 
<if test="emp_name != null and emp_name != ''"> 
and emp_name = #{emp_name } 
</if> 
<if test="emp_sex != null and emp_sex != ''"> 
and sex = #{emp_sex} 
</if> 
</select> 

mybatis 中的if標簽有些類似于EL表達式的使用,test中可以直接寫入類中的屬性或者key值。

where標簽

<select id="select01" resultMap="BasicResultMap">
   SELECT 
   * 
   FROM
   oa_employee 
   <where>
   <if test="emp_name != null and emp_name != ''">
    and emp_name = #{emp_name }
   </if>
   <if test="emp_sex != null and emp_sex != ''">
    and sex = #{emp_sex}
   </if>
   </where>
  </select>

這里的where標簽 替換了前一段代碼的 where 1=1 。 mybatis中的where 標簽會判斷標簽內(nèi)是否有內(nèi)容, 如果有內(nèi)容就自動生成where 并把 where 后面的第一個and +一個空格,or+一個空格 去掉。

choose , when 和 otherwise 標簽

<select id="select01" resultMap="BasicResultMap">
   SELECT 
   * 
   FROM
   oa_employee 
   <where>
   <choose>
    <when test="emp_name != null and emp_name != ''">
       and emp_name = #{emp_name }
    </when>
     <when test="emp_sex != null and emp_sex != ''">
       and sex = #{emp_sex}
    </when>
    <otherwise>
      emp_id = 50
    </otherwise>
   </choose>
   </where>
  </select>

當所有條件不滿足時,執(zhí)行otherwise標簽的內(nèi)容。

trim標簽

<select id="select01" resultMap="BasicResultMap">
   SELECT 
   * 
   FROM
   oa_employee 
    <trim prefix="where" prefixOverrides="and |or ">
    <if test="emp_name != null and emp_name != ''">
      and emp_name = #{emp_name }
    </if>
    <if test="emp_sex != null and emp_sex != ''">
       and sex = #{emp_sex}
    </if>
  </trim>

trim標簽的屬性及其含義

  • - prefix : 標簽之間有內(nèi)容在最前面加入
  • - prefixOverrides: 檢查內(nèi)容的最前面是否匹配,匹配就刪除
  • - suffix: 標簽之間有內(nèi)容在最后面加入
  • - suffixOverrides:檢查內(nèi)容的最后面是否匹配,匹配就刪除

set標簽

set標簽常用于update操作,并且會自動抹掉無關(guān)的,

 <update id="update01" >
  UPDATE 
   oa_employee 
  <set>
    <if test="emp_name != null and emp_name != ''">
       emp_name = #{emp_name}
    </if>
    <if test="emp_sex != null and emp_sex != ''">
      ,sex = #{emp_sex}
    </if>
  </set>
  WHERE emp_id = 50 
  </update>

foreach標簽

foreach 用于處理數(shù)組或者list集合,下面是一個批量添加的例子

 <insert id="insert01">
  INSERT INTO 
  oa_employee 
  ( emp_name, sex, fk_dept_id) 
  VALUES
  <foreach collection="list" item="employee" separator=","> 
   (#{employee.emp_name},#{employee.emp_sex},#{employee.fk_dept_id})
  </foreach>
  </insert>

其中 如果參數(shù)為數(shù)組 則collection只能為“array” 參數(shù)為List集合則collection只能為 “l(fā)ist” item類似JSTL 中的var的作用, 指代容器中的每一個對象。separator=”,”的含義是每條數(shù)據(jù)以 , 分割。 未注明的屬性有 open 和 close 他們的含義是在遍歷開始和結(jié)束時分別添加其內(nèi)容。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

最新評論

商城县| 香港| 治县。| 宝鸡市| 灵山县| 曲沃县| 达孜县| 孟津县| 昌吉市| 巴青县| 百色市| 灌南县| 宁远县| 新余市| 廉江市| 丹阳市| 渭源县| 乐陵市| 瓮安县| 鄄城县| 保靖县| 龙江县| 北安市| 三穗县| 罗平县| 米林县| 门源| 苏尼特右旗| 北海市| 天津市| 阜南县| 句容市| 吉水县| 呼玛县| 鹿泉市| 杭锦后旗| 辉县市| 新和县| 文化| 无极县| 视频|