Mybatis的Dao向mapper傳多個參數(三種解決方案)

第一種方案 

DAO層的函數方法 java

[sql] view plain copysql

  1. Public User selectUser(String name,String area);  

    對應的Mapper.xml  app

[sql] view plain copy函數

  1. <select id="selectUser" resultMap="BaseResultMap" parameterType="java.lang.String">  
  2.     select  *  from user_user_t   where user_name = #{0} and user_area=#{1}  
  3. </select>  

其中,#{0}表明接收的是dao層中的第一個參數,#{1}表明dao層中第二參數,更多參數一致日後加便可。spa

第二種方案

此方法採用Map傳多參數..net

Dao層的函數方法xml

[sql] view plain copyblog

  1. Public User selectUser(Map paramMap);  

對應的Mapper.xml接口

[sql] view plain copyip

  1. <select id=" selectUser" resultMap="BaseResultMap">  
  2.    select  *  from user_user_t   where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}  
  3. </select>  

Service層調用

[sql] view plain copy

  1. Private User xxxSelectUser(){  
  2. Map paramMap=new hashMap();  
  3. paramMap.put(「userName」,」對應具體的參數值」);  
  4. paramMap.put(「userArea」,」對應具體的參數值」);  
  5. User user=xxx. selectUser(paramMap);}  

我的認爲此方法不夠直觀,見到接口方法不能直接的知道要傳的參數是什麼。

第三種方案

Dao層的函數方法

[sql] view plain copy

  1. Public User selectUser(@param(「userName」)Stringname,@param(「userArea」)String area);  

對應的Mapper.xml

[sql] view plain copy

  1. <select id=" selectUser" resultMap="BaseResultMap">  
  2.    select  *  from user_user_t   where user_name = #{userName,jdbcType=VARCHAR} and user_area=#{userArea,jdbcType=VARCHAR}  
  3. </select>   

我的以爲這種方法比較好,能讓開發者看到dao層方法就知道該傳什麼樣的參數,比較直觀,我的推薦用此種方案。

相關文章
相關標籤/搜索