1. 程式人生 > >mybatis傳入混合引數(多個不同型別的引數)

mybatis傳入混合引數(多個不同型別的引數)

當呼叫介面:

  1. public List<User> selectUserInIDs(List<Integer> ids,String name);  

userMapper.xml的書寫應該為:
  1. <selectid="selectUserInIDs"resultType="User">
  2.         select * from user where id in   
  3.         <foreachcollection="param1"item="item"open="("separator=","close=")">
  4.             #{item}  
  5.         </foreach>
  6.         and name = #{param2}  
  7.     </select>

mybatis會自動將多個不同型別的引數改成param1,param2...

或者採用Mybatis的Annotation(@Param):

只需要在Dao層的方法中加入註解,如下

  1. public List<User> selectUserInIDs(@Param("ids")List<Integer> ids,@Param("user")User user);  

  1. <selectid="selectUserInIDs"
    resultType="User">
  2.         select * from user   
  3.         <iftest="null != ids">
  4.         where id in   
  5.         <foreachcollection="ids"item="item"open="("separator=","close=")">
  6.             #{item}  
  7.         </foreach>
  8.         and name = #{user.name}  
  9.         </if>
  10.     </
    select>