MyBatis參數設置

3. MyBatis中的參數


        我個人理解,MyBatis中得參數分爲傳入參數和返回參數,傳入參數就是在Mapper接口中定義abstract方法中傳入的參數,返回參數就是該抽象方法的返回值。

在這兒我分別給大家介紹傳入參數和返回參數,本篇博客就給大家介紹下傳入參數,下面一篇日誌專門介紹返回參數。


3.1 傳入參數


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


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


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

3.1.1 簡單類型

public Posts getPostsById(int id);

<!-- 這裏的id必須和PostsMapper接口中的接口方法名相同,不然運行的時候也要報錯 --> 
    <select id="getPostsById" resultType="Posts" parameterType="int"> 
        select * from posts where id=#{id} 
    </select>

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

而且JAVA反射只能獲取方法參數的類型,是無從得知方法參數的名字的。


比如上面這個示例中,使用#{id}來引用只是比較直觀而已,使用其他名字來引用也是一樣的。所以當在if元素中test傳遞的參數時,就必須要用_parameter來引用這個參數了。像這樣:

    <select id="getPostsById" resultType="Posts" parameterType="int"> 
        select * from posts 
        <if test="_parameter!=0">
        		where id=#{id}
        </if>
        <if test="_parameter==0">
        		limit 0,1
        </if> 
    </select>


以上案例中的_parameter參數指代的是接口中相應方法的入參的名稱。


3.1.2 對象類型


傳入JAVA複雜對象類型的話,sql映射語句中就可以直接引用對象的屬性名了,這裏的屬性名是實實在在的真實的名字,不是隨意指定的。

Mapper中的接口方法:

public void addPosts(Posts posts);


sql映射:

    <insert id="addPosts" parameterType="Posts"> 
       insert into posts(title,context) values(#{title},#{context}) 
       <!-- 這裏sql結尾不能加分號,否則報“ORA-00911”的錯誤 --> 
    </insert>


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


3.1.3 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



3.1.4 集合類型


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接口:

public void batchUpdate(List<Posts> list);


SQL映射:

   <update id="batchUpdate" parameterType="java.util.List">
   		update posts set  badcount=3,goodcount=5 where id in
   		<foreach collection="list" item="item" open="(" close=")" index="index" separator=",">
   		     	 #{item.id}
   		</foreach>
   </update>


3.1.5 對象類型中的集合屬性


對於單獨傳遞的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" >

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