MyBatis傳入參數與parameterType

1.   傳入簡單類型java


    Java代碼:apache

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代碼:less

 

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

 

MAPPER :ui

 

 

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.  傳入數組spa


  JAVA代碼:.net

 

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

 

 MAPPER :code

 

 

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倒是是arrayxml

 

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注入攻擊,所以你 不該該容許用戶輸入這些字段,或者一般自行轉義並檢查。

相關文章
相關標籤/搜索