Mybatis傳多個參數(三種解決方案),mybatis解決方案

來源:互聯網
上載者:User

Mybatis傳多個參數(三種解決方案),mybatis解決方案

據我目前接觸到的傳多個參數的方案有三種。

第一種方案

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傳多參數.

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);}

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

第三種方案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>

個人覺得這種方法比較好,能讓開發人員看到dao層方法就知道該傳什麼樣的參數,比較直觀,個人推薦用此種方案。














mybatis傳遞多種參數問題?

mybatis 沒用過,Ibatis 用過一段時間。
報的錯就是你傳進去的
map.put("productType", a);
productType 參數找不到 對應參數。

如果不像傳參數進去可以考慮在 getScrollPage 中加入動態Where 條件。
參考資料:x
 
MyBatis 怎傳遞多個參數

在MyBatis中可以用以下的方式來傳遞多個參數1. 用java.util.Map來傳遞, Code 如下public List getAllUsersByUserName(String username, int start, int limit){ Map params = new HashMap(3); params.put("username",username); params.put("start",start); params.put("limit",limit); return userMapper.getAllUsersByUserName(params); } 對應的XXMapper.xml檔案中如下: SELECT u.* FROM User u WHERE u.username LIKE #{username} '%' LIMIT #{start}, #{limit} 2. 用JavaBean的方式來傳遞, Code如下:public List getUsersByUserName(String username){ User user = new User(); user.setUsername(username); return userMapper.getUserByUsername(user); }對應的XXMapper.xml檔案中如下: SELECT u.* FROM User u WHERE u.username = #{username} OK, 就介紹這兩種方法吧!!!
 

相關文章

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.