

或者:

傳入JAVA對象
mapper接口代碼:
public int findUserList(User user);
xml代碼:
<select id="findUserList" parameterType="User" resultType="java.lang.Integer"> SELECT COUNT(*) FROM USER user <where> <if test="code != null"> and user.CODE = #{code} </if> <if test="id != null"> and user.ID = #{id} </if> <if test="idList !=null "> and user.ID in ( <foreach collection="idList" item="id" index="index" separator=","> #{id} </foreach> ) </if> </where> </select>
JAVA對象中有list或array時,foreach中的collection必須是具體list或array的變量名。
比如這里User含有一個名為idList的list,所以User中用idList取值,這點和單獨傳list或array時不太一樣。
注解@Param
注解單一屬性;這個類似於將參數重命名了一次
例子1:
mapper接口代碼:
List<User> selectUserByTime(@Param(value="startdate")String startDate);
xml代碼:
<select id="selectUserByTime" resultType="User" parameterType="java.lang.String"> select * from t_user where create_time >= to_date(#{startdate,jdbcType=VARCHAR},'YYYY-MM-DD') </select>
例子2:
注解javaBean
mapper接口代碼:
List<User> selectUserByTime(@Param(value="dateVO")DateVO dateVO);
xml代碼:
<select id="selectUserByTime" resultType="User" parameterType="DateVO"> select * from t_user where create_time >= to_date(#{dateVO.startDate,jdbcType=VARCHAR},'YYYY-MM-DD') and create_time < to_date(#{dateVO.endDate,jdbcType=VARCHAR},'YYYY-MM-DD') </select>

