preface

Recently, when I used Mybatis – Plus to do a project, I found that there was a problem when I used updatById method to update a certain field. General business operations are to update non-empty fields, but I found that all fields were updated, which was caused by the global update strategy of Mybatis – Plus. We can solve this problem through corresponding global configuration

According to the official document, there are three global database configuration policies: query policy, update policy, and add policy

Click here for official documentation

Global database policy configuration

  1. configuration
# Global strategy
mybatis-plus.global-config.db-config.update-strategy=not_empty
mybatis-plus.global-config.db-config.insert-strategy=not_empty
mybatis-plus.global-config.db-config.select-strategy=not_empty
Copy the code

Optional configuration values, see the source code below

package com.baomidou.mybatisplus.annotation;

public enum FieldStrategy {
    IGNORED,
    NOT_NULL,
    NOT_EMPTY,
    DEFAULT,
    NEVER;

    private FieldStrategy(a) {}}Copy the code
  1. IGNORED IGNORED all fields are updated and inserted
  2. NOT_NULL updates and inserts only non-NULL values
  3. NOT_EMPTY updates and inserts only non-null and non-empty strings
  4. NEVER Updates and inserts are NEVER performed
  5. The DEFAULT DEFAULT NOT_NULL

Default value, see the source code

public static class DbConfig {
        private IdType idType;
        private String tablePrefix;
        private String schema;
        private String columnFormat;
        private String propertyFormat;
        private boolean tableUnderline;
        private boolean capitalMode;
        private IKeyGenerator keyGenerator;
        private String logicDeleteField;
        private String logicDeleteValue;
        private String logicNotDeleteValue;
        private FieldStrategy insertStrategy;
        private FieldStrategy updateStrategy;
        private FieldStrategy selectStrategy;

        public DbConfig(a) {
            this.idType = IdType.ASSIGN_ID;
            this.tableUnderline = true;
            this.capitalMode = false;
            this.logicDeleteValue = "1";
            this.logicNotDeleteValue = "0";
            this.insertStrategy = FieldStrategy.NOT_NULL;
            this.updateStrategy = FieldStrategy.NOT_NULL;
            this.selectStrategy = FieldStrategy.NOT_NULL;
        }
Copy the code

The default value is NOT_NULL

Updating policy Configuration

When we use the updateById() method, we use the default policy NOT_NULL if no update policy is specified.

So we don’t update the set when the field is NULL, we update the set if the field is an empty string,

So we can change our global configuration to update when not_EMPTY is not null

mybatis-plus.global-config.db-config.update-strategy=not_empty
Copy the code

You can also specify the field update policy separately in the required fields

/** * User type */
    @TableField(value = "ADMIN_TYPE_ID",updateStrategy = FieldStrategy.NOT_EMPTY)
    private String userType;
Copy the code

Or you can replace updateById with UpdateWrapper

Add a policy

Similarly, when we do inser or save, we use the default policy NOT_NULL if no update policy is specified,

That is, when the object field is NULL, ins does not append values. If the object field is an empty string, INS does append values.

We can also specify other policies for adding operations