或者:
传入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>