mybatis傳遞參數到mapping.xml

來源:互聯網
上載者:User

標籤:

第一種方案 ,通過序號傳遞

DAO層的函數方法 

Public User selectUser(String name,String area);

對應的Mapper.xml  

<select id="selectUser" resultMap="BaseResultMap">    select  *  from user_user_t   where user_name = #{0} and user_area=#{1}</select>

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

第二種方案,通過map傳遞

此方法採用Map傳多參數.

Dao層的函數方法

Public User selectUser(Map paramMap);

對應的Mapper.xml

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

Service層調用

Private User xxxSelectUser(){Map paramMap=new hashMap();paramMap.put(“userName”,”對應具體的參數值”);paramMap.put(“userArea”,”對應具體的參數值”);User user=xxx. selectUser(paramMap);}

個人認為此方法不夠直觀,見到介面方法不能直接的知道要傳的參數是什麼。

第三種方案,用@param通過單個參數名傳遞,推薦方式

Dao層的函數方法

Public User selectUser(@param(“userName”)Stringname,@param(“userArea”)String area);

對應的Mapper.xml

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

第四種方案,用@param通過參數對象傳遞

Dao層的函數方法

Public User selectUser(@param(“user”)User user,@param(“userArea”)String area);

對應的Mapper.xml

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


每一種方式都可以不需要在xml中寫parameterType屬性

mybatis傳遞參數到mapping.xml

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.