mybatis传入混合参数(多个不同类型的参数)

当调用接口:

[java]  view plain  copy
  1. public List<User> selectUserInIDs(List<Integer> ids,String name);  

userMapper.xml的书写应该为:

[html]  view plain  copy
  1. <select id="selectUserInIDs" resultType="User">  
  2.         select * from user where id in   
  3.         <foreach collection="param1" item="item" open="(" separator="," close=")">  
  4.             #{item}  
  5.         </foreach>  
  6.         and name = #{param2}  
  7.     </select>  

mybatis会自动将多个不同类型的参数改成param1,param2...


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

[java]  view plain  copy
  1. public List<User> selectUserInIDs(@Param("ids")List<Integer> ids,@Param("user")User user);  

[html]  view plain  copy
  1. <select id="selectUserInIDs" resultType="User">  
  2.         select * from user   
  3.         <if test="null != ids">  
  4.         where id in   
  5.         <foreach collection="ids" item="item" open="(" separator="," close=")">  
  6.             #{item}  
  7.         </foreach>  
  8.         and name = #{user.name}  
  9.         </if>  
  10.     </select>  

猜你喜欢

转载自blog.csdn.net/no_can_no_bb_/article/details/78952649
今日推荐