mybatis傳入混合引數(多個不同型別的引數)
阿新 • • 發佈:2019-02-06
當呼叫介面:
- public List<User> selectUserInIDs(List<Integer> ids,String name);
userMapper.xml的書寫應該為:
- <selectid="selectUserInIDs"resultType="User">
- select * from user where id in
- <foreachcollection="param1"item="item"open="("separator=","close=")">
-
#{item}
- </foreach>
- and name = #{param2}
- </select>
mybatis會自動將多個不同型別的引數改成param1,param2...
或者採用Mybatis的Annotation(@Param):
只需要在Dao層的方法中加入註解,如下
- public List<User> selectUserInIDs(@Param("ids")List<Integer> ids,@Param("user")User user);
-
<selectid="selectUserInIDs"
- select * from user
- <iftest="null != ids">
- where id in
- <foreachcollection="ids"item="item"open="("separator=","close=")">
- #{item}
- </foreach>
- and name = #{user.name}
- </if>
-
</