MYBATIS 的parameter,MYBATISparameter

來源:互聯網
上載者:User

MYBATIS 的parameter,MYBATISparameter
MYBATIS 的parameter部落格分類:

  • JAVA
mybatisparameter 

 

Parameter
 
1.   傳入簡單類型


    JAVA代碼:

Java代碼  
  1. public User get(Long id) {    
  2.     return (User) getSqlSession().selectOne("com.liulanghan.get" , id);  
  3. }  

 
 MAPPER :

 

Xml代碼  
  1. <select id="findUserListByIdList" parameterType="java.lang.Long" resultType="User">  
  2.         select * from user where  id = #{id};  
  3. </select>  

  
2.   傳入List

 

    JAVA代碼:

 

Java代碼  
  1. public List<Area> findUserListByIdList(List<Long> idList) {  
  2.         return getSqlSession().selectList("com.liulanghan.findUserListByIdList", idList);  
  3.     }  

 

MAPPER :

 

 

Xml代碼  
  1.    <select id="findUserListByIdList" parameterType="java.util.ArrayList" resultType="User">  
  2.     select * from user user  
  3.     <where>  
  4.         user.ID in (  
  5.         <foreach item="guard" index="index" collection="list"  
  6.             separator=","> #{guard} </foreach>  
  7.         )  
  8.     </where>  
  9. </select>   

   
 
 單獨傳入list時,foreach中的collection必須是list,不不管變數的具體名稱是什麼。比如這裡變數名為idList,
 collection卻是是list。
 
3.  傳入數組


  JAVA代碼:

 

Java代碼  
  1. public List<Area> findUserListByIdList(int[] ids) {  
  2.         return getSqlSession().selectList("com.liulanghan.findUserListByIdList", ids);  
  3.     }  

 

 MAPPER :

 

 

Xml代碼  
  1. <select id="findUserListByIdList" parameterType="java.util.HashList" resultType="User">  
  2.     select * from user user  
  3.     <where>  
  4.         user.ID in (  
  5.         <foreach item="guard" index="index" collection="array"  
  6.             separator=","> #{guard} </foreach>  
  7.         )  
  8.     </where>  
  9. </select>     

  
 
 單獨傳入數組時,foreach中的collection必須是array,不不管變數的具體名稱是什麼。比如這裡變數名為ids,
 collection卻是是array

 

4.  傳入map
 
 JAVA代碼:

Java代碼  
  1. public boolean exists(Map<String, Object> map){  
  2.         Object count = getSqlSession().selectOne("com.liulanghan.exists", map);  
  3.         int totalCount = Integer.parseInt(count.toString());  
  4.         return totalCount > 0 ? true : false;  
  5.     }  

  
 MAPPER :

Xml代碼  
  1. <select id="exists" parameterType="java.util.HashMap" resultType="java.lang.Integer">  
  2.         SELECT COUNT(*) FROM USER user  
  3.         <where>  
  4.             <if test="code != null">   
  5.                 and user.CODE = #{code}   
  6.             </if>  
  7.             <if test="id != null">   
  8.                 and user.ID = #{id}   
  9.             </if>  
  10.             <if test="idList !=null ">  
  11.                 and user.ID in (  
  12.                 <foreach item="guard" index="index" collection="idList"  
  13.                     separator=","> #{guard} </foreach>  
  14.                 )  
  15.             </if>  
  16.         </where>  
  17.     </select>  

 

 MAP中有list或array時,foreach中的collection必須是具體list或array的變數名。比如這裡MAP含有一個
    名為idList的list,所以MAP中用idList取值,這點和單獨傳list或array時不太一樣。
 
 
5. 傳入JAVA對象
 
 JAVA代碼:

Java代碼  
  1. public boolean findUserListByDTO(UserDTO userDTO){  
  2.         Object count = getSqlSession().selectOne("com.liulanghan.exists", userDTO);  
  3.         int totalCount = Integer.parseInt(count.toString());  
  4.         return totalCount > 0 ? true : false;  
  5.     }  

  
 MAPPER :

Xml代碼  
  1. <select id="findUserListByDTO" parameterType="UserDTO" resultType="java.lang.Integer">  
  2.         SELECT COUNT(*) FROM USER user  
  3.         <where>  
  4.             <if test="code != null">   
  5.                 and user.CODE = #{code}   
  6.             </if>  
  7.             <if test="id != null">   
  8.                 and user.ID = #{id}   
  9.             </if>  
  10.             <if test="idList !=null ">  
  11.                 and user.ID in (  
  12.                 <foreach item="guard" index="index" collection="idList"  
  13.                     separator=","> #{guard} </foreach>  
  14.                 )  
  15.             </if>  
  16.         </where>  
  17.     </select>  

 

    JAVA對象中有list或array時,foreach中的collection必須是具體list或array的變數名。比如這裡UserDTO含有一個
    名為idList的list,所以UserDTO中用idList取值,這點和單獨傳list或array時不太一樣。

 

6.取值


 由上面可以看出,取值的時候用的是#{}。它具體的意思是告訴MyBatis建立一個預先處理語句參數。
 使用JDBC,這樣的一個參數在SQL中會由一個“?”來標識,並被傳遞到一個新的預先處理語句中,就像這樣:

 

Java代碼  
  1. // Similar JDBC code, NOT MyBatis…  
  2. String selectPerson = “SELECT * FROM PERSON WHERE ID=?”;  
  3. PreparedStatement ps = conn.prepareStatement(selectPerson);  
  4. ps.setInt(1,id);  

  
    可以看到這個寫法比較簡單,MyBatis為我們做了很多預設的事情,具體的寫法應該如下:

 

Xml代碼  
  1. #{property,javaType=int,jdbcType=NUMERIC,typeHandler=MyTypeHandler,mode=OUT,resultMap=User}  

 

 property:屬性名稱,即代碼傳入的變數名。
 javaType:該欄位在JAVA中的類型,比如int。
 jdbcType:該欄位在JDBC中的類型,比如NUMERIC。
 typeHandler:類型處理器
 mode:參數類型為IN,OUT或INOUT參數
 resultMap:結果。
 
 還好,MyBatis比較體諒我們,一般我們只需寫一個屬性名稱即可,如#{id},其他的如javaType和typeHandlerMybatis
 會自動幫我們填好。可是這樣有時也會出問題,比如出現CLOB欄位時。
 
 由於JAVA代碼中的String類型對應的預設typeHandler為StringTypeHandler,當用String類型處理時,如果String長度超過一定長度,就會報如下錯誤:


 setString can only process strings of less than 32766 chararacters

 

 解決辦法是指定該屬性的typeHandler,如下:


 #{message,typeHandler=org.apache.ibatis.type.ClobTypeHandler}

 

 我們也可以自訂typeHandler來處理需要的資料,具體這裡詳述。
 
 JDBC類型是僅僅需要對插入,更新和刪除操作可能為空白的列進行處理。這是JDBC的需要,而不是MyBatis的。一般不需要配置
 
 mode、resultMap一般不需要,在寫預存程序時會用到,這裡不詳述。
 
7.字串替換

 

 一般情況下,我們採用#{}取值,產生預先處理語句,但是有時我們可能不希望Mybatis來幫我們預先處理,比如ORDER BY時,可以
 採用如下寫法:
 
 ORDER BY ${columnName}
 
 這裡MyBatis不會修改或逸出字元串。而是直接拼接到SQL字串後面。
 
 重要:接受從使用者輸出的內容並提供給語句中不變的字串,這樣做是不安全的。這會導致潛在的SQL注入攻擊,因此你
 不應該允許使用者輸入這些欄位,或者通常自行轉義並檢查。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.