1.进行判断 mapper.xml 的写法
select count(*) from t_send_message
and send_user_id = #{sendUserId}
and receive_user_id = #{receiveUserId}
:
<select id="totalMessage" parameterType="Map" resultType="int">
select count(*) from t_send_message
<trim prefix="where" prefixOverrides="and |or">
<if test="sendUserId != null">
and send_user_id = #{sendUserId}
</if>
<if test="receiveUserId != null">
and receive_user_id = #{receiveUserId}
</if>
</trim>
</select>
2.进行批量删除:
<select id="totalMessage" parameterType="Map" resultType="int">
select count(*) from t_send_message
<trim prefix="where" prefixOverrides="and |or">
<if test="sendUserId != null">
and send_user_id = #{sendUserId}
</if>
<if test="receiveUserId != null">
and receive_user_id = #{receiveUserId}
</if>
</trim>
</select>
3.排序和传递参数;
<select id="getTotalSearchPd" parameterType="Map" resultType="int">
SELECT count(*) from t_product
LEFT JOIN t_resource
on t_product.resource_ids=t_resource.resource_id
LEFT JOIN (SELECT min(pic_id) pic_id,product_id,locate FROM t_product_pic group by product_id) a
on t_product.product_id=a.product_id
LEFT JOIN datadict d on t_product.product_status=d.code
Left JOIN t_company e on t_product.company_id=e.company_id
where d.catalog='productstatus'
<if test="resource_id!=null">
and resource_ids=#{resource_id}
</if>
<!-- 此处传入参数注意几个参数的名称,以及下面的#应当换成$,否则会出现index错误 -->
<if test="keys!=null">
<foreach collection="keys" item="String" separator=" " index="index">
and product_name like '%${String}%' or storeName like '%${String}%' or resource_name like '%${String}%' or resource_desc like '%${String}%'
</foreach>
</if>
<if test="district!=null">
and storeName like '%${district}%'
</if>
<!-- 此处一定要注意,传递排序参数时使用$而不是# -->
<if test="order_method!=null and order_type!=null">
order by ${order_method} ${order_type}
</if>
</select>
注意传递参数的写法:
<insert id="addCommentNews" parameterType="CommentNews">
insert into t_comment_news(user_id,t_content) values (#{0},#{1})
</insert>