mybatis傳入混合參數(多個不同類型的參數)

當調用接口:

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

userMapper.xml的書寫應該爲:

<select id="selectUserInIDs" resultType="User">
		select * from user where id in 
		<foreach collection="param1" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
		and name = #{param2}
	</select>

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


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

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

<select id="selectUserInIDs" resultType="User">
		select * from user 
		<if test="null != ids">
		where id in 
		<foreach collection="ids" item="item" open="(" separator="," close=")">
			#{item}
		</foreach>
		and name = #{user.name}
		</if>
	</select>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章