MyBatis动态增改,多字段模糊查询

示例:
insert

    insert into bargain_products
    <trim prefix="(" suffix=")" suffixOverrides=",">
        <if test="id != null and id != ''">id,</if>
        <if test="userid != null and userid != ''">user_id,</if>
        <if test="productName != null and productName != ''">product_name,</if>
        <if test="type != null and type != ''">type,</if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
        <if test="id != null and id != ''">#{id},</if>
        <if test="userid != null and userid != ''">#{userid},</if>
        <if test="productName != null and productName != ''">#{productName},</if>
        <if test="type != null and type != ''">#{type},</if>
    </trim>

update

update bargain_products
    <trim prefix="set" suffixOverrides=",">
        <if test="productName != null and productName != ''">product_name = #{productName},</if>
        <if test="type != null and type != ''">type = #{type},</if>
        <if test="texture != null and texture != ''">texture = #{texture},</if>
    </trim>
    where id = #{id}

select

 SELECT
        bp.id,
        bp.user_id userid,
        bp.product_name productName,
        bp.type,
        DATE_FORMAT(bp.create_time,'%Y-%m-%d') createTime
    FROM
        bargain_products bp
    LEFT JOIN sys_user sus ON bp.user_id = sus.userid
    <where>
        <if test="secondLevel != null and secondLevel != ''">
            sus.second_level = #{secondLevel}
        </if>
        <if test="fuzzyQuery != null and fuzzyQuery != '' ">
            and CONCAT(bp.product_name, bp.type) LIKE "%"#{fuzzyQuery}"%"
        </if>
    </where>
    ORDER BY bp.create_time DESC

多字段模糊查询中如果有一个字段为null,则返回的也是NULL,那么这一条记录可能就会被错过,怎么处理呢,我这边使用的是IFNULL进行判断

<if test="fuzzyQuery != null and fuzzyQuery != '' ">
       and CONCAT(IFNULL(bp.product_name,''), IFNULL( bp.type,'')) LIKE "%"#{fuzzyQuery}"%" 
</if>

delete
要么就直接删

	DELETE
	FROM
		bargain_products
	WHERE
		id = 'BP-6ec77c53464f43afbe55b23d4a4e7b7d'

如果需要加别名的格式

DELETE bp FROM bargain_products bp WHERE bp.id ='BP-6ec77c5346'
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值