SpringBoot+Mybatis-plus: Uses enumeration to receive parameters and return data

Enumeration has been widely used in recent projects. Here we record the benefits, scenarios and ways of using enumeration in projects.

Benefits of enumerations

  1. Readable and easy to understand
  2. Unified parameter type to avoid parameter transmission errors
  3. Thread-safe, globally unique, cannot be modified

Enumeration usage Scenarios

In practical use, enumeration can be used when an object or an attribute needs to have multiple states or descriptions to choose from, such as the gender of the person, the status of payment, the type of error, and so on.

Use of enumerations in projects

There are usually two forms of enumeration in a project

The first form

/** * Gender enumeration **@author hjljy
 */
@Getter
public enum SexEnum {
    /** * Gender enumeration */
    DEFAULT(-1."Confidential"),
    WOMAN(0."Female"),
    MAN(1."Male");

    @EnumValue // marks the value stored to the database
    @JsonValue // mark the value returned by json
    private final Integer code;

    private final String remark;

    SexEnum(Integer code, String remark) {
        this.code = code;
        this.remark = remark; }}Copy the code

Second form

/** * User type enumeration **@author hjljy
 */
public enum SysUserTypeEnum {
    /** * Super administrator */
    SUPER_ADMIN,
    /** * System administrator */
    SYS_ADMIN,
    /** * Common administrator */
    ADMIN,
    /**
     * 普通账号
     */
    NORMAL
}
Copy the code

The first form, usually using custom attributes (code,remark) to judge and save to the database, and the second form, using the SUPER_ADMIN instance directly.

The enumeration fields of the entity class are saved to the database and automatically mapped to enumeration properties when returned

In the case of the first form of enumeration, Mybatis- Plus provides a related processing method for enumeration saving and insinuation. The first step is to respond to the value to be stored in the database with the @enumValue flag. Second, add the configuration to the SpringBoot configuration file

Mybatis -plus: # support wildcard * or; Segmentation switch to own enumerated in the packet typeEnumsPackage: com. Baomidou. Springboot. Entity. EnumsCopy the code

If it is the second form of enumeration, there is no custom field, do not need any operation, directly save and return to the database content is the enumeration type name, such as: SUPER_ADMIN, SYS_ADMIN.

The entity class enumeration field is returned to the front end

The first form of enumeration adds @jsonValue // to the fields that need to be returned as json values note: only one field can be returned, not multiple. In the second form, you don’t have to deal with it.

The entity class enumeration field receives front-end parameters

The first form of enumeration, with the @jsonValue annotation, simply accepts the value of the corresponding field to properly encapsulate it. For example, if the value passed by the current end is ‘1’,’0′,’-1′ will be mapped automatically. In the second form, you don’t have to deal with it.

Notes for Attention

The second form of enumeration uses the name() method when comparing a string, for example:

SysUserTypeEnum.SUPER_ADMIN.name().equels("SUPER_ADMIN")
Copy the code

2 Cannot receive the number type of the front-end, or the result of the mapping of the number type that cannot be received is incorrect or an error is reported, etc., so that the front-end can convert the number type into string type and pass it to the back-end. And the reason for that is because the enumeration type map if it’s a number is going to match by the script, and the script starts at zero, so even though you’re using @jsonValue with Intger code, it’s still going to match by script, so you need to convert it to string.