编程知识 cdmana.com

Mybatis plus global update policy and insert query policy

Preface

Recently in use mybatis-plus On a project , Discover the use of updatById Method time , There was a problem updating a field , General business operations are to update fields that are not empty , It turns out that all fields have been updated , This is because mybatis-plus The global update policy leads to , We can solve the problem through the corresponding global configuration

Mybatis-Plus  overall situation Update Update strategy , and insert Insert query policy Look at the official documents , There are three strategies for global database configuration , They are query strategies , Update strategy , And add policies

Click here to enter Official documents

Global database policy configuration

  1. To configure
# Overall strategy mybatis-plus.global-config.db-config.update-strategy=not_emptymybatis-plus.global-config.db-config.insert-strategy=not_emptymybatis-plus.global-config.db-config.select-strategy=not_empty

Optional configuration values , See the source code as follows

package com.baomidou.mybatisplus.annotation;public enum FieldStrategy {    IGNORED,    NOT_NULL,    NOT_EMPTY,    DEFAULT,    NEVER;    private FieldStrategy() {    }}
  1. IGNORED Ignore judgment , All fields are updated and inserted
  2. NOT_NULL Only update and insert non NULL value
  3. NOT_EMPTY Only update and insert non NULL Value and non empty string
  4. NEVER Never update or insert
  5. DEFAULT Default NOT_NULL

The default value , Look at the source

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() {            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;        }

The default value configuration is NOT_NULL

Update policy configuration

That is, we are using updateById() When it comes to methods , Use the default policy when no update policy is specified , by NOT_NULL,

That is, when the object field is NULL It won't be done when set to update , If our field is an empty string, we will set update operation ,

So we can change that our global configuration is not empty not_empty When will it be updated

mybatis-plus.global-config.db-config.update-strategy=not_empty

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

/**     *  The user types      */    @TableField(value = "ADMIN_TYPE_ID",updateStrategy = FieldStrategy.NOT_EMPTY)    private String userType;

Or you can use UpdateWrapper Way to replace updateById

Mybatis-Plus  overall situation Update Update strategy , and insert Insert query policy

Add a policy

Similarly, we are doing inser perhaps save, When it comes to methods , Use the default policy when no update policy is specified , by NOT_NULL,

That is, when the object field is NULL It won't be done when ins add value , If our field is an empty string, we will add a value ,

We can also specify other policies to add

版权声明
本文为[kenx]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/10/20211002145637058e.html

Scroll to Top