MyBatis

對原生態jdbc程序中問題總結


1、數據庫連接,使用時就創建,不使用立即釋放,對數據庫進行頻繁連接開啓和關閉,造成數據庫資源浪費,影響 數據庫性能。

設想:使用數據庫連接池管理數據庫連接。

 

2、將sql語句硬編碼java代碼中,如果sql 語句修改,需要重新編譯java代碼,不利於系統維護。

設想:將sql語句配置在xml配置文件中,即使sql變化,不需要對java代碼進行重新編譯。

 

 

3、向preparedStatement中設置參數,對佔位符號位置和設置參數值,硬編碼在java代碼中,不利於系統維護。

設想:將sql語句及佔位符號和參數全部配置在xml中。

 

4、從resutSet中遍歷結果集數據時,存在硬編碼,將獲取表的字段進行硬編碼,,不利於系統維護。

設想:將查詢的結果集,自動映射成java對象。

 

mybatis框架


2.1 mybatis是什麼?

 

mybatis是一個持久層的框架,是apache下的頂級項目。

mybatis託管到goolecode下,再後來託管到github(https://github.com/mybatis/mybatis-3/releases)

 

mybatis讓程序將主要精力放在sql上,通過mybatis提供的映射方式,自由靈活生成(半自動化,大部分需要程序員編寫sql)滿足需要sql語句。

 

mybatis可以將向 preparedStatement中的輸入參數自動進行輸入映射,將查詢結果集靈活映射成java對象。(輸出映射

2.2 mybatis框架


wKioL1d89CuSwcWbAAEHfOvyOr4693.jpg


更多學習筆記下載鏈接

http://down.51cto.com/data/2227257

http://down.51cto.com/data/2227258



MyBatis的傳入參數parameterType類型

(轉載自http://blog.csdn.net/u010235716/article/details/51698422)

1. MyBatis的傳入參數parameterType類型分兩種

   1. 1. 基本數據類型:int,string,long,Date;

   1. 2. 複雜數據類型:類和Map


2. 如何獲取參數中的值:

   2.1  基本數據類型:#{參數} 獲取參數中的值

   2.2  複雜數據類型:#{屬性名}  ,map中則是#{key}


3.案例:

 

 3.1 基本數據類型案例

[html] view plain copy 在CODE上查看代碼片派生到我的代碼片

  1. <sql id="Base_Column_List" >  

  2.     id, car_dept_name, car_maker_name, icon,car_maker_py,hot_type  

  3.   </sql>  

  4.   <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >  

  5.     select   

  6.     <include refid="Base_Column_List" />  

  7.     from common_car_make  

  8.     where id = #{id,jdbcType=BIGINT}  

  9.   </select>  


 3.2 複雜類型--map類型     

[html] view plain copy 在CODE上查看代碼片派生到我的代碼片

  1. <select id="queryCarMakerList" resultMap="BaseResultMap" parameterType="java.util.Map">  

  2.         select  

  3.         <include refid="Base_Column_List" />  

  4.         from common_car_make cm  

  5.         where 1=1  

  6.         <if test="id != null">  

  7.             and  cm.id = #{id,jdbcType=DECIMAL}  

  8.         </if>  

  9.         <if test="carDeptName != null">  

  10.             and  cm.car_dept_name = #{carDeptName,jdbcType=VARCHAR}  

  11.         </if>  

  12.         <if test="carMakerName != null">  

  13.             and  cm.car_maker_name = #{carMakerName,jdbcType=VARCHAR}  

  14.         </if>  

  15.         <if test="hotType != null" >  

  16.            and  cm.hot_type = #{hotType,jdbcType=BIGINT}  

  17.         </if>  

  18.         ORDER BY cm.id  

  19.     </select>  


  3.3 複雜類型--類類型

[html] view plain copy 在CODE上查看代碼片派生到我的代碼片

  1. <update id="updateByPrimaryKeySelective" parameterType="com.epeit.api.model.CommonCarMake" >  

  2.    update common_car_make  

  3.    <set >  

  4.      <if test="carDeptName != null" >  

  5.        car_dept_name = #{carDeptName,jdbcType=VARCHAR},  

  6.      </if>  

  7.      <if test="carMakerName != null" >  

  8.        car_maker_name = #{carMakerName,jdbcType=VARCHAR},  

  9.      </if>  

  10.      <if test="icon != null" >  

  11.        icon = #{icon,jdbcType=VARCHAR},  

  12.      </if>  

  13.      <if test="carMakerPy != null" >  

  14.            car_maker_py = #{carMakerPy,jdbcType=VARCHAR},  

  15.      </if>  

  16.      <if test="hotType != null" >  

  17.            hot_type = #{hotType,jdbcType=BIGINT},  

  18.      </if>  

  19.    </set>  

  20.    where id = #{id,jdbcType=BIGINT}  

  21.  </update>  


3.4 複雜類型--map中包含數組的情況


[html] view plain copy 在CODE上查看代碼片派生到我的代碼片

  1. <select id="selectProOrderByOrderId" resultType="com.epeit.api.model.ProOrder" parameterType="java.util.HashMap" >  

  2.         select sum(pro_order_num) proOrderNum,product_id productId,promotion_id promotionId  

  3.         from pro_order  

  4.         where 1=1  

  5.         <if test="orderIds != null">  

  6.           and  

  7.             <foreach collection="orderIds" item="item" open="order_id IN(" separator="," close=")">  

  8.                 #{item,jdbcType=BIGINT}  

  9.             </foreach>  

  10.         </if>  

  11.         GROUP BY product_id,promotion_id  

  12.     </select>  




MyBatis的動態SQL詳解(轉載)


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

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

 

  • if

  • choose(when,otherwise)

  • trim

  • where

  • set

  • foreach

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

Xml代碼  收藏代碼

  1. <select id="dynamicIfTest" parameterType="Blog" resultType="Blog">  

  2.     select * from t_blog where 11 = 1  

  3.     <if test="title != null">  

  4.         and title = #{title}  

  5.     </if>  

  6.     <if test="content != null">  

  7.         and content = #{content}  

  8.     </if>  

  9.     <if test="owner != null">  

  10.         and owner = #{owner}  

  11.     </if>  

  12. </select>  

這條語句的意思非常簡單,如果你提供了title參數,那麼就要滿足title=#{title},同樣如果你提供了Content和Owner的時候,它們也需要滿足相應的條件,之後就是返回滿足這些條件的所有Blog,這是非常有用的一個功能,以往我們使用其他類型框架或者直接使用JDBC的時候, 如果我們要達到同樣的選擇效果的時候,我們就需要拼SQL語句,這是極其麻煩的,比起來,上述的動態SQL就要簡單多了。

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

Xml代碼  收藏代碼

  1. <select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">  

  2.     select * from t_blog where 11 = 1   

  3.     <choose>  

  4.         <when test="title != null">  

  5.             and title = #{title}  

  6.         </when>  

  7.         <when test="content != null">  

  8.             and content = #{content}  

  9.         </when>  

  10.         <otherwise>  

  11.             and owner = "owner1"  

  12.         </otherwise>  

  13.     </choose>  

  14. </select>  

when元素表示當when中的條件滿足的時候就輸出其中的內容,跟JAVA中的switch效果差不多的是按照條件的順序,當when中有條件滿足的時候,就會跳出choose,即所有的when和otherwise條件中,只有一個會輸出,當所有的我很條件都不滿足的時候就輸出otherwise中的內容。所以上述語句的意思非常簡單, 當title!=null的時候就輸出and titlte = #{title},不再往下判斷條件,當title爲空且content!=null的時候就輸出and content = #{content},當所有條件都不滿足的時候就輸出otherwise中的內容。

where語句的作用主要是簡化SQL語句中where中的條件判斷的,先看一個例子,再解釋一下where的好處。

Xml代碼  收藏代碼

  1. <select id="dynamicWhereTest" parameterType="Blog" resultType="Blog">  

  2.     select * from t_blog   

  3.     <where>  

  4.         <if test="title != null">  

  5.             title = #{title}  

  6.         </if>  

  7.         <if test="content != null">  

  8.             and content = #{content}  

  9.         </if>  

  10.         <if test="owner != null">  

  11.             and owner = #{owner}  

  12.         </if>  

  13.     </where>  

  14. </select>  

 where元素的作用是會在寫入where元素的地方輸出一個where,另外一個好處是你不需要考慮where元素裏面的條件輸出是什麼樣子的,MyBatis會智能的幫你處理,如果所有的條件都不滿足那麼MyBatis就會查出所有的記錄,如果輸出後是and 開頭的,MyBatis會把第一個and忽略,當然如果是or開頭的,MyBatis也會把它忽略;此外,在where元素中你不需要考慮空格的問題,MyBatis會智能的幫你加上。像上述例子中,如果title=null, 而content != null,那麼輸出的整個語句會是select * from t_blog where content = #{content},而不是select * from t_blog where and content = #{content},因爲MyBatis會智能的把首個and 或 or 給忽略。

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

Xml代碼  收藏代碼

  1. <select id="dynamicTrimTest" parameterType="Blog" resultType="Blog">  

  2.     select * from t_blog   

  3.     <trim prefix="where" prefixOverrides="and |or">  

  4.         <if test="title != null">  

  5.             title = #{title}  

  6.         </if>  

  7.         <if test="content != null">  

  8.             and content = #{content}  

  9.         </if>  

  10.         <if test="owner != null">  

  11.             or owner = #{owner}  

  12.         </if>  

  13.     </trim>  

  14. </select>  

 

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

Xml代碼  收藏代碼

  1. <update id="dynamicSetTest" parameterType="Blog">  

  2.     update t_blog  

  3.     <set>  

  4.         <if test="title != null">  

  5.             title = #{title},  

  6.         </if>  

  7.         <if test="content != null">  

  8.             content = #{content},  

  9.         </if>  

  10.         <if test="owner != null">  

  11.             owner = #{owner}  

  12.         </if>  

  13.     </set>  

  14.     where id = #{id}  

  15. </update>  

 上述示例代碼中,如果set中一個條件都不滿足,即set中包含的內容爲空的時候就會報錯。

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

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

  2. 如果傳入的是單參數且參數類型是一個array數組的時候,collection的屬性值爲array

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























發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章