Skip to content

枚举类支持

1.枚举类要求

mybatis-mp 支持枚举作为参数进行进行CRUD;要求一下:

1. 普通枚举,以名字存储(继承 Serializable)

2. 非普通枚举,自定义属性存储(继承 EnumSupport)

java
public enum TestEnum implements EnumSupport<String> {

    X1("a1"),X2("a2");

    TestEnum(String code){
        this.code=code;
    }

    private final String code;

    @Override
    public String getCode() {
        return this.code;
    }

枚举类如何使用

1:新增

java
@Table
@Data
public class DefaultValueTest {

    @TableId
    private Integer id;

    @TableField(defaultValue = "{BLANK}")
    private String value1;

    @TableField(defaultValue = "1", updateDefaultValue = "2")
    private Integer value2;

    @TableField(defaultValue = "{NOW}")
    private LocalDateTime createTime;

    private TestEnum value3;
}
java
DefaultValueTestMapper mapper = session.getMapper(DefaultValueTestMapper.class);
DefaultValueTest defaultValueTest = new DefaultValueTest();
defaultValueTest.setValue3(TestEnum.X1);
mapper.save(defaultValueTest);

2:枚举类查询

java
QueryChain.of(mapper)
.in(DefaultValueTest::getValue1,Arrays.asList(TestEnum.X1,TestEnum.X2))
.list();

把它看成普通的参数即可,用户和 int string 都是一样的