• MybatisPlus系列【增、删、改、查常用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;
        }
  • 相关阅读:
    python文件处理-读、写
    Python三元运算和lambda表达式
    可变、不可变数据类型和hash
    Python常见字符编码间的转换
    Python常用字符编码
    spring cloud 使用spring cloud bus自动刷新配置
    spring cloud config--client
    spring cloud Config--server
    git 小乌龟安装教程
    spring cloud Eureka常见问题总结
  • 原文地址:https://www.cnblogs.com/hujunwei/p/16512123.html
Copyright © 2020-2023  润新知