其他分享
首页 > 其他分享> > Mybatis-Plus系列---【增、删、改、查常用Demo】

Mybatis-Plus系列---【增、删、改、查常用Demo】

作者:互联网

1.新增

    @PostMapping("/add")
    public R<Boolean> insert(@RequestBody BusinessCustomer businessCustomer) {
        return add(this.businessCustomerService.save(businessCustomer));
    }

2.批量新增

3.删除

    @DeleteMapping("/del/{id}")
    public R<Boolean> delete(@PathVariable("id") Long id) {
        return delete(this.businessCustomerService.removeById(id));
    }

4.批量删除

    @DeleteMapping("/del")
    public R<Boolean> delete(List<Long> ids) {
        return delete(this.businessCustomerService.removeByIds(ids));
    }

扩展:逻辑删除的两种实现方案

1.把逻辑删除用修改完成逻辑删除;

2.在application.yml中全局配置,仍然使用正常的删除方法即可。

mybatis-plus:
  global-config:
    db-config:
      logic-delete-field: del # 全局逻辑删除的实体字段名
      logic-delete-value: 1 # 逻辑已删除值(默认为1)
      logic-not-delete-value: 0 # 逻辑未删除值(默认为0)

5.修改

#通过id修改
User user = new User();
user.setId(23214234);
user.setName("张三");
user.setAge(18);
updateById(user);

#通过指定条件修改-写法一
#update user set age=19 where name = "zhangsan"
LambdaUpdateWrapper<User> updateWrapper = new LambdaWrapper<>();
updateWrapper.eq(User::getName,"zhangsan");
updateWrapper.set(User::getAge,19);
update(updateWrapper);

#通过指定条件修改-写法二
LambdaUpdateWrapper<BusinessField> lambdaUpdateWrapper = Wrappers.lambdaUpdate(BusinessField.class)
.set(BusinessField::getInvCode, businessField.getInvCode())
.set(BusinessField::getInvNum, businessField.getInvNum())
.set(BusinessField::getInvDate, businessField.getInvDate())
.set(BusinessField::getUpdateTime, LocalDateTime.now())
.set(BusinessField::getUpdateUser, SecureUtil.getUserId())
.eq(BusinessField::getBusinessGroupNo, businessField.getBusinessGroupNo());
update(lambdaUpdateWrapper);

6.批量修改

7.查询

        #条件查询
        LambdaQueryWrapper<FinanceAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(param.getAccount()), FinanceAccount::getAccount, param.getAccount());
        queryWrapper.eq(StrUtil.isNotBlank(param.getInOutType()), FinanceAccount::getInOutType, param.getInOutType());
        queryWrapper.or().eq(StrUtil.isNotBlank(param.getInOutType()), FinanceAccount::getInOutType, "LY");
        queryWrapper.orderByDesc(FinanceAccount::getSort).orderByDesc(FinanceAccount::getCreateTime);
        List<FinanceAccount> records = this.list(queryWrapper);

8.分页查询

    #动态条件分页查询
    @Override
    public Page<FinanceAccount> pageAll(Page page, FinanceAccountParam param) {
        LambdaQueryWrapper<FinanceAccount> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StrUtil.isNotBlank(param.getAccountName()), FinanceAccount::getAccountName, param.getAccountName());
        queryWrapper.eq(StrUtil.isNotBlank(param.getInOutType()), FinanceAccount::getInOutType, param.getInOutType());
        queryWrapper.eq(FinanceAccount::getDel, CommonConstant.DEL_USING);
        queryWrapper.orderByDesc(FinanceAccount::getSort).orderByDesc(FinanceAccount::getCreateTime);
        Page<FinanceAccount> voPage = this.page(page, queryWrapper);return voPage;
    }

 

标签:queryWrapper,set,Demo,FinanceAccount,param,---,getInOutType,Plus,BusinessField
来源: https://www.cnblogs.com/hujunwei/p/16512123.html