美文网首页
mybatis-plus逻辑删除

mybatis-plus逻辑删除

作者: 墨色尘埃 | 来源:发表于2018-11-19 17:08 被阅读142次

    springboot整合mybatis-plus

            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>2.2.0</version>
            </dependency>
    

    1、application.properties 添加一下配置

    mybatis-plus:
      mapper-locations: classpath:com/cicdi/invoice/mapping/**/*Mapper.xml
      typeAliasesPackage: com.cicdi.invoice.**.model
      global-config:
        id-type: 2
        db-column-underline: true #驼峰下划线转换
        logic-delete-value: 101003  #配置逻辑删除字段为1是删除
        logic-not-delete-value: 101001 #配置逻辑删除字段为101001是未删除
        sql-injector: com.baomidou.mybatisplus.mapper.LogicSqlInjector #逻辑删除配置
        meta-object-handler: com.cicdi.invoice.common.config.mybatis.MetaObjectHandlerConfig
      configuration:
        map-underscore-to-camel-case: true
    

    2、实体类添加注解,标记该字段为逻辑删除字段

        @TableLogic  //逻辑删除注解
        @JsonIgnore  //忽略
        @TableField(fill = FieldFill.INSERT)  //新增的时候自动插入,MetaObjectHandlerConfig配置文件
        private Integer status;
    

    3、然后调用mybatis-plus的delete相关方法即是逻辑删除

        @RequestMapping(value = "/delete", method = RequestMethod.POST)
        @Transactional
        public ResponseObj<Boolean> delete(@ModelAttribute BaseInvoice model) throws Exception {
    //        String userId = CommonUtils.getUserId();
            String userId = "huangjihai";
    
            //只允许删除自己的发票
            boolean result = service.delete(new EntityWrapper<BaseInvoice>()
                    .eq("INVOICE_ID", model.getInvoiceId())
                    .eq("USER_ID", userId)
                    .eq("USED", 0));
            //修改t_file表status状态
            boolean delete = invoiceFileService.delete(new EntityWrapper<InvoiceFile>()
                    .eq("ASSOCIATE_ID", model.getInvoiceId())
                    .eq("STATUS", 101001));
            if (result && delete)
                return new ResponseObj<>(result, RetCode.SUCCESS);
            return new ResponseObj<>(result, RetCode.FAIL);
        }
    

    执行的sql语句:
    UPDATE T_INVOICE SET `status`=101003 WHERE `status`=101001 AND (INVOICE_ID = 1063004582892011522 AND USER_ID = 'huangjihai' AND USED = 0)
    UPDATE t_file SET `status`=101003 WHERE `status`=101001 AND (ASSOCIATE_ID = 1063004582892011522 AND STATUS = 101001)
    那么非逻辑删除呢

    mybatis-plus更新:更新部分字段,不影响其他字段在表里的值

    UPDATE T_INVOICE SET invoice_name='上海圆迈贸易有限公司', invoice_remarks='订单号:81452438092' WHERE `status`=101001 AND (USER_ID = 'huangjihai' AND INVOICE_ID = 1064813605193740289)
    

    相关文章

      网友评论

          本文标题:mybatis-plus逻辑删除

          本文链接:https://www.haomeiwen.com/subject/dgeafqtx.html