SpringBoot - MyBatis-Plus - UpdateWrapper、LambdaUpdateWrapper和LambdaUpdateChainWrapper的用法(四)

写在前面

SpringBoot - MyBatis-Plus - QueryWrapper、LambdaQueryWrapper和LambdaQueryChainWrapper的用法(二)的用法类似,另外UpdateWrapper、LambdaUpdateWrapper和LambdaUpdateChainWrapper还提供了:set、setSql和setEntity的方法。

UpdateWrapper

// 1. 查询条件:商品标题中包含'学生白色丝袜'并且状态为上架的商品
UpdateWrapper<MallxSpu> updateWrapper = new UpdateWrapper<>();
updateWrapper.like("title","学生白色丝袜").eq("statue",1);

// 2. 将满足条件的商品添加标签
MallxSpu spu = new MallxSpu();
spu.setTag("丝袜");

// 3. 执行更新
int result = mallxSpuMapper.update(spu, updateWrapper);

LambdaUpdateWrapper

方式一:
// 1. 查询条件:商品标题中包含'学生白色丝袜'并且状态为上架的商品
LambdaUpdateWrapper<MallxSpu> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.like(MallxSpu::getTitle,"学生白色丝袜").eq(MallxSpu::getStatus,1);

// 2. 将满足条件的商品添加标签
MallxSpu spu = new MallxSpu();
spu.setTag("丝袜");

// 3. 执行更新
int result = mallxSpuMapper.update(spu, updateWrapper);
方式二(使用UpdateWrapper的set方法直接设置字段为待修改的值):
// 1. 查询条件:商品标题中包含'学生白色丝袜'并且状态为上架的商品,并将符合条件的商品打上标签。
LambdaUpdateWrapper<MallxSpu> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.like(MallxSpu::getTitle,"学生白色丝袜").eq(MallxSpu::getStatus,1).set(MallxSpu::getTag, "丝袜");

// 2. 执行更新
int result = mallxSpuMapper.update(null, updateWrapper);
方式三(使用UpdateWrapper的set方法和对象混合使用,设置字段为待修改的值):
// 1. 查询条件:商品标题中包含'学生白色丝袜'并且状态为上架的商品,并将符合条件的商品打上标签。
LambdaUpdateWrapper<MallxSpu> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.like(MallxSpu::getTitle,"学生白色丝袜").eq(MallxSpu::getStatus,1).set(MallxSpu::getTag, "丝袜");

// 2. 将满足条件的商品的库存改为10000
MallxSpu spu = new MallxSpu();
spu.setStock(10000);

// 3. 执行更新
int result = mallxSpuMapper.update(spu, updateWrapper);
方式四(使用UpdateWrapper的setSql方法设置字段为待修改的值):
// 1. 查询条件:商品标题中包含'学生白色丝袜'并且状态为上架的商品,并将符合条件的商品打上标签,同时修改库存。
LambdaUpdateWrapper<MallxSpu> updateWrapper = new LambdaUpdateWrapper<>();
updateWrapper.like(MallxSpu::getTitle,"学生白色丝袜").eq(MallxSpu::getStatus,1).setSql("tag = '丝袜'").setSql("stock = 10000");

// 2. 执行更新
int result = mallxSpuMapper.update(null, updateWrapper);

LambdaUpdateChainWrapper

方式一:
// 查询条件:商品标题中包含'学生白色丝袜'并且状态为上架的商品,并将符合条件的商品打上标签,同时修改库存,并执行更新操作。
boolean flag = new LambdaUpdateChainWrapper<>(mallxSpuMapper)
        .like(MallxSpu::getTitle,"学生白色丝袜")
        .eq(MallxSpu::getStatus,1)
        .setSql("tag = '丝袜'")
        .setSql("stock = 10000")
        .update();
方式二:
// 1. 将满足条件的商品的库存改为10000
MallxSpu spu = new MallxSpu();
spu.setStock(10000);

// 2. 查询条件:商品标题中包含'学生白色丝袜'并且状态为上架的商品,并将符合条件的商品打上标签,同时修改库存,并执行更新操作。
boolean flag = new LambdaUpdateChainWrapper<>(mallxSpuMapper)
        .like(MallxSpu::getTitle,"学生白色丝袜")
        .eq(MallxSpu::getStatus,1)
        .setSql("tag = '丝袜'")
        .update(spu);
  • 14
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cloneme01

谢谢您的支持与鼓励!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值