Lambda简化条件语句(让代码更优雅)

1. LambdaQueryWrapper  和  Wrappers.lambdaQuery (推荐使用)  条件查询

List<ProductInfoEntity> productInfoList = productInfoMapper.selectList(new LambdaQueryWrapper<ProductInfoEntity>()
.eq(ProductInfoEntity::getId, id)
.eq(ProductInfoEntity::getState, 1));

//官方推荐使用这个
 List<ProductInfoEntity> productInfoList = this.baseMapper.selectList(Wrappers.lambdaQuery(ProductInfoEntity.class)
 .eq(true, ProductInfoEntity::getId, id))
 .eq(ProductInfoEntity::getState, 1));

2. LambdaQueryChainWrapper 链式条件查询

  List<ProductInfoEntity> productInfoList = new LambdaQueryChainWrapper<>(productInfoMapper)
  .eq(ProductInfoEntity::getId, id)
  .eq(ProductInfoEntity::getState, 1)
  .list();

3. LambdaUpdateWrapper  和  Wrappers.lambdaUpdate (推荐使用)  条件更新

 productInfoMapper.update(null, new LambdaUpdateWrapper<ProductInfoEntity>()
 .set(ProductInfoEntity::getProductName,"哈哈哈哈哈")
 .eq(ProductInfoEntity::getId, id));
 
 //官方推荐使用这个
 this.baseMapper.update(null,Wrappers.lambdaUpdate(ProductInfoEntity.class)
 .set(ProductInfoEntity::getProductName,"哈哈哈哈哈")
 .eq(ProductInfoEntity::getId, id));

4.lambdaUpdateChainWrapper 链式条件更新

new LambdaUpdateChainWrapper<>(productInfoMapper)
.set(ProductInfoEntity::getProductName,"哈哈哈哈哈")
.eq(ProductInfoEntity::getId, id)
.update();

MyBatis-Plus (also known as MyBatis+) is an enhanced version of the MyBatis framework, which is a popular lightweight persistence framework for Java. MyBatis-Plus provides additional features and utilities to simplify development with MyBatis. The "GE" you mentioned is likely referring to the "greater than or equal to" comparison operator in SQL. MyBatis-Plus supports this operator through its QueryWrapper or LambdaQueryWrapper classes. You can use the ge method to specify the column and the value to compare with. Here is an example of using the "greater than or equal to" operator in MyBatis-Plus: ```java import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.service.additional.query.impl.LambdaQueryChainWrapper; // Using QueryWrapper QueryWrapper<User> queryWrapper = Wrappers.query(); queryWrapper.ge("age", 18); // Using LambdaQueryWrapper LambdaQueryChainWrapper<User> lambdaQueryWrapper = Wrappers.lambdaQuery(); lambdaQueryWrapper.ge(User::getAge, 18); ``` In the above example, we create a QueryWrapper or LambdaQueryWrapper and use the ge method to specify that the "age" column should be greater than or equal to 18. This can be used in conjunction with other query conditions to build more complex queries. Please note that this is just a simple example, and there are many more features and capabilities provided by MyBatis-Plus. Feel free to ask if you have any more questions!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值