mybatis框架,细节处理

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值