Mybatis學習筆記

1、mybatis官網

http://mybatis.github.io/mybatis-3/index.html

2、MyBatis傳入參數與parameterType

http://openwares.net/database/mybatis_parametertype.html

http://zhuyuehua.iteye.com/blog/1717525


Mybatis的Mapper文件中的select、insert、update、delete元素中有一個parameterType屬性,用於對應的mapper接口方法接受的參數類型。

可以接受的參數類型有基本類型和複雜類型。

mapper接口方法一般接受一個參數,可以通過使用@Param註釋將多個參數綁定到一個map做爲輸入參數。

    簡單數據類型

    mapper接口方法:       
  

 User selectByPrimaryKey(Integer id);

    sql映射:
        
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
      select
      <include refid="Base_Column_List" />
      from base.tb_user
      where id = #{id,jdbcType=INTEGER}
    </select>


    對於簡單數據類型,sql映射語句中直接#{變量名}這種方式引用就行了,其實這裏的”變量名”可以是任意的。mapper接口方法傳遞過來的值,至於其叫什麼名字其實是不可考也沒必要知道的。
    而且JAVA反射只能獲取方法參數的類型,是無從得知方法參數的名字的。

    比如上面這個示例中,使用#{id}來引用只是比較直觀而已,使用其他名字來引用也是一樣的。所以當在if元素中test傳遞的參數時,就必須要用_parameter來引用這個參數了。像這樣:
           
    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
      select
      <include refid="Base_Column_List" />
      from tb_user
      <if test="_parameter != 0">
      where id = #{id,jdbcType=INTEGER}
      </if>
    </select>



    如果test測試條件中使用id就會提示錯誤,因爲這個參數其實沒有名字,只是一個值或引用而已,只能使用_parameter來引用。
    對象類型

    傳入JAVA複雜對象類型的話,sql映射語句中就可以直接引用對象的屬性名了,這裏的屬性名是實實在在的真實的名字,不是隨意指定的。
    mapper接口方法:
        
    int insert(User user);



    sql映射:
        
    <insert id="insert" parameterType="User" useGeneratedKeys="true" keyProperty="id">
      insert into tb_user (name, sex)
      values (#{name,jdbcType=CHAR}, #{sex,jdbcType=CHAR})



    雖然可以明確的引用對象的屬性名了,但如果要在if元素中測試傳入的user參數,仍然要使用_parameter來引用傳遞進來的實際參數,因爲傳遞進來的User對象的名字是不可考的。如果測試對象的屬性,則直接引用屬性名字就可以了。

    測試user對象:
        
  
 <if test="_parameter != null">

    測試user對象的屬性:       
   
<if test="name != null">

    map類型

    傳入map類型,直接通過#{keyname}就可以引用到鍵對應的值。使用@param註釋的多個參數值也會組裝成一個map數據結構,和直接傳遞map進來沒有區別。

    mapper接口:
        
 
  int updateByExample(@Param("user") User user, @Param("example") UserExample example);

    sql映射:
        
    <update id="updateByExample" parameterType="map" >
      update tb_user
      set id = #{user.id,jdbcType=INTEGER},
      ...
      <if test="_parameter != null" >
        <include refid="Update_By_Example_Where_Clause" />
      </if>

    注意這裏測試傳遞進來的map是否爲空,仍然使用_parameter
    集合類型

        You can pass a List instance or an Array to MyBatis as a parameter object. When you do, MyBatis will automatically wrap it in a Map, and key it by name. List instances will be keyed to the name “list” and array instances will be keyed to the name “array”.

    可以傳遞一個List或Array類型的對象作爲參數,MyBatis會自動的將List或Array對象包裝到一個Map對象中,List類型對象會使用list作爲鍵名,而Array對象會用array作爲鍵名。

    集合類型通常用於構造IN條件,sql映射文件中使用foreach元素來遍歷List或Array元素。

    mapper接口:
        
   
User selectUserInList(List<Interger> idlist);

    sql動態語句映射:
        
   
<select id="selectUserInList" resultType="User">
      SELECT *
      FROM USER
      WHERE ID in
      <foreach item="item" index="index" collection="list"
          open="(" separator="," close=")">
            #{item}
      </foreach>
    </select>

    對象類型中的集合屬性

    對於單獨傳遞的List或Array,在SQL映射文件中映射時,只能通過list或array來引用。但是如果對象類型有屬性的類型爲List或Array,則在sql映射文件的foreach元素中,可以直接使用屬性名字來引用。
    mapper接口:
        
 
  List<User> selectByExample(UserExample example);

    sql映射文件:
        
   
<where >
      <foreach collection="oredCriteria" item="criteria" separator="or" >
        <if test="criteria.valid" >

    在這裏,UserExample有一個屬性叫oredCriteria,其類型爲List,所以在foreach元素裏直接用屬性名oredCriteria引用這個List即可。

    item=”criteria”表示使用criteria這個名字引用每一個集合中的每一個List或Array元素




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