1. 参数是个简单javaBean
在表达式中直接使用字段 ( 可以当做用的属性名 , 实际不是)
<!--
List<User> search1(User user); 参数是个简单javaBean
-->
<select id="search1" parameterType="user" resultType="user">
select * from user
<where>
<if test="username != null and username != ''">
and username like #{username}
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
</where>
</select>
2. 参数是个复杂javaBean
在表达式中直接二级对象. 字段 ( 可以当做用的属性名 , 实际不是)
例如 : QueryVO vo中有个User user属性, user中有username属性 , 那要使用username时, 就写成 user. username ( 不要在前面写vo. )
<!--
List<User> search2(QueryVO vo);; 参数是个复杂javaBean
-->
<select id="search2" parameterType="queryVo" resultType="user">
select * from use
<where>
<if test="idArray != null and idArray.length > 0">
<foreach collection="idArray" open=" and id in(" item="id" separator="," close=")">
#{id}
</foreach>
</if>
<if test="user.username != null and user.username != ''">
and username like #{user.username}
</if>
<if test="user.sex != null and user.sex != ''">
and sex = #{user.sex}
</if>
</where>
</select>
3. 参数是个数组
固定使用array
例如 : <if test="array != null and array.length >0">
<!--
List<User> searchByArray(Integer[] idArray) ; 参数是个数组
-->
<select id="searchByArray" parameterType="arraylist" resultType="user">
select * from use
<where>
<if test="array != null and array.length >0">
<foreach collection="array" open=" and id in(" item="id" separator="," close=")">
#{id}
</foreach>
</if>
</where>
</select>
4. 参数是个集合
固定使用list
例如 : <if test="list != null and list.size() >0">
<!--
List<User> searchByList(List<Integer> idList); 参数是个集合
-->
<select id="searchByList" parameterType="arraylist" resultType="user">
select * from use
<where>
<if test="list != null and list.size() >0">
<foreach collection="list" open=" and id in(" item="id" separator="," close=")">
#{id}
</foreach>
</if>
</where>
</select>
5. 没有封装参数
使用arg0 , arg1来称呼
例如 :
<if test="arg0 != null and arg0 != ''">
<if test="arg1 != null and arg1 != ''">
<!--
List<User> search3(String username, String sex); 没有参数集
-->
<select id="search3" resultType="user">
select * from user
<where>
<if test="arg0 != null and arg0 != ''">
and username like #{arg0}
</if>
<if test="arg1 != null and arg1 != ''">
and sex = #{arg1}
</if>
</where>
</select>
6. 为参数取了别名
用别名来称呼
起别名的方法:
在方法的形参列表中加入@ param注解 ,注解的括号内写别名
List<User> search4 (@Param("username")String username, @Param("sex") String sex);
<!--
List<User> search4(@Param("username")String username, @Param("sex") String sex);
; 为参数取了别名
-->
<select id="search4" resultType="user">
<!--select * from user-->
<include refid="selectUser"/>
<where>
<if test="username != null and username != ''">
and username like #{username}
</if>
<if test="sex != null and sex != ''">
and sex = #{sex}
</if>
</where>
</select>