1 Preface
During our development process, we often need to use some numbers to represent the state. For example: 1-Normal, 0-Frozen. However, this cannot achieve the meaning of seeing the name, especially when there are many states. Of course, we can solve it by defining constant classes and other methods, but here I will introduce the solution to enumeration and the enumeration processor.
In fact, this enumeration processor Mybatis is available, but it is said to be average, so we mainly introduce Mybatis plus here.
2 How to use
2.1 Add configuration in
mybatis-plus: configuration: default-enum-type-handler:
2.2 Defining an enumeration class
@Getter public enum UserStatus { NORMAL(1, "normal"), FROZEN(2, "freeze"), ; @EnumValue @JsonValue private final int value; private final String desc; //describe UserStatus(int value, String desc) { = value; = desc; } }
Explanation of the meaning of the annotation:
①@EnumValue: The tag value is a parameter written to the database. After all, it is still stored in integers in our database.
②@JsonValue: If you do not add this annotation, the front end can only get NORMAL or FROZEN strings, as follows:
"status": "NORMAL"
Adding will result in the value (1 or 2), as follows:
"status": 1
2.3 Used in entity classes and assignments
In entity class:
public class User { //Other codes.. /** *Usage status (1 normal 2 freeze) */ private UserStatus status; }
When assigning:
lambdaUpdate() //Other codes... .set(remainBalance == 0, User::getStatus, ) .update();
This is the article about the specific use of Mybatis plus enumeration processor. For more related contents of Mybatis plus enumeration processor, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!