[Mybatis] - 动态SQL中参数类型不同 , 称呼的方法不同

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>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值