SpringBoot和mybatis项目中使用枚举

枚举基类

package com.lovefly.common.enums.base;

import com.fasterxml.jackson.databind.JsonNode;

/**
 * @author james.h.fu
 * @date 2017/5/25
 */
public interface ByteBaseEnum {
    String getDescription();

    Byte getValue();

    static  & ByteBaseEnum> E fromValue(Class enumClass, Byte value) {
        E[] enumConstants = enumClass.getEnumConstants();
        for (E e : enumConstants) {
            if (e.getValue().equals(value)) {
                return e;
            }
        }
        return null;
    }

    static  & ByteBaseEnum> E fromJsonNode(Class enumClass, JsonNode node) {
        if (node == null) {
            return null;
        }
        if (!node.canConvertToInt()) {
            throw new IllegalArgumentException("enum value must to be integer.");
        }
        return ByteBaseEnum.fromValue(enumClass, Byte.valueOf(node.toString()));
    }
}
package com.lovefly.common.enums.base;

import com.fasterxml.jackson.databind.JsonNode;

/**
 * @author james.h.fu
 * @date 2017/5/25
 */
public interface IntBaseEnum {
    String getDescription();

    Integer getValue();

    static  & IntBaseEnum> E fromValue(Class enumClass, Integer value) {
        E[] enumConstants = enumClass.getEnumConstants();
        for (E e : enumConstants) {
            if (e.getValue().equals(value)) {
                return e;
            }
        }
        return null;
    }

    static  & IntBaseEnum> E fromJsonNode(Class enumClass, JsonNode node) {
        if (node == null) {
            return null;
        }
        if (!node.canConvertToInt()) {
            throw new IllegalArgumentException("enum value must to be integer.");
        }
        return IntBaseEnum.fromValue(enumClass, Integer.valueOf(node.toString()));
    }
}
package com.lovefly.common.enums.base;

import com.fasterxml.jackson.databind.JsonNode;

/**
 * @author james.h.fu
 * @date 2017/5/25
 */
public interface StringBaseEnum {
    String getDescription();

    String getValue();

    static  & StringBaseEnum> E fromValue(Class enumClass, String value) {
        E[] enumConstants = enumClass.getEnumConstants();
        for (E e : enumConstants) {
            if (e.getValue().equals(value)) {
                return e;
            }
        }
        return null;
    }

    static  & StringBaseEnum> E fromJsonNode(Class enumClass, JsonNode node) {
        if (node == null) {
            return null;
        }
        return StringBaseEnum.fromValue(enumClass, node.toString());
    }
}

枚举定义

/**
 * 菜单类型枚举
 *
 * @author : jamesfu
 * @date : 22/2/2022 22:10
 */
public enum MenuTypeEnum implements ByteBaseEnum {
    Category((byte) 0, "目录"),
    Page((byte) 1, "目录"),
    ;

    private Byte value;
    private String description;

    MenuTypeEnum(Byte value, String description) {
        this.value = value;
        this.description = description;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @JsonValue
    @Override
    public Byte getValue() {
        return value;
    }

    @JsonCreator
    public static MenuTypeEnum fromJsonNode(JsonNode node) {
        return ByteBaseEnum.fromJsonNode(MenuTypeEnum.class, node);
    }
}
package com.lovefly.common.enums;


import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.JsonNode;
import com.lovefly.common.enums.base.IntBaseEnum;

/**
 * @author James.H.Fu
 * @date 2017/5/24
 */
public enum CommonStatusEnum implements IntBaseEnum {

    /**
     * 无效
     */
    Invalid(0) {
        @Override
        public String getDescription() {
            return "无效";
        }
    },
    /**
     * 有效
     */
    Valid(1) {
        @Override
        public String getDescription() {
            return "有效";
        }
    },
    /**
     * 已删除
     */
    Deleted(-999) {
        @Override
        public String getDescription() {
            return "已删除";
        }
    };

    private Integer value;

    CommonStatusEnum(Integer value) {
        this.value = value;
    }

    @JsonValue
    @Override
    public Integer getValue() {
        return this.value;
    }

    @JsonCreator
    public static CommonStatusEnum fromJsonNode(JsonNode node) {
        return IntBaseEnum.fromJsonNode(CommonStatusEnum.class, node);
    }
}
/**
 * 收费类型枚举
 *
 * @author : jamesfu
 * @date : 22/2/2022 22:10
 */
public enum FeeTypeEnum implements StringBaseEnum {
    Water("W", "目录"),
    Electricity("E", "目录"),
    Gas("G", "目录"),
    ;

    private String value;
    private String description;

    FeeTypeEnum(String value, String description) {
        this.value = value;
        this.description = description;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @JsonValue
    @Override
    public String getValue() {
        return value;
    }

    @JsonCreator
    public static FeeTypeEnum fromJsonNode(JsonNode node) {
        return StringBaseEnum.fromJsonNode(FeeTypeEnum.class, node);
    }
}

mybatis类型处理器

public class ByteEnumTypeHandler & ByteBaseEnum> extends BaseTypeHandler {
    private Class clazz;

    public ByteEnumTypeHandler(Class enumType) {
        if (enumType == null) {
            throw new IllegalArgumentException("Type argument cannot be null");
        }

        this.clazz = enumType;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, ByteBaseEnum parameter, JdbcType jdbcType)
            throws SQLException {
        ps.setShort(i, parameter.getValue());
    }

    @Override
    public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return ByteBaseEnum.fromValue(clazz, rs.getByte(columnName));
    }

    @Override
    public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return ByteBaseEnum.fromValue(clazz, rs.getByte(columnIndex));
    }

    @Override
    public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return ByteBaseEnum.fromValue(clazz, cs.getByte(columnIndex));
    }
}
public class IntEnumTypeHandler & IntBaseEnum> extends BaseTypeHandler {
    private Class clazz;

    public IntEnumTypeHandler(Class enumType) {
        if (enumType == null) {
            throw new IllegalArgumentException("Type argument cannot be null");
        }

        this.clazz = enumType;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, IntBaseEnum parameter, JdbcType jdbcType)
            throws SQLException {
        ps.setInt(i, parameter.getValue());
    }

    @Override
    public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return IntBaseEnum.fromValue(clazz, rs.getInt(columnName));
    }

    @Override
    public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return IntBaseEnum.fromValue(clazz, rs.getInt(columnIndex));
    }

    @Override
    public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return IntBaseEnum.fromValue(clazz, cs.getInt(columnIndex));
    }
}
public class StringEnumTypeHandler & StringBaseEnum> extends BaseTypeHandler {
    private Class clazz;

    public StringEnumTypeHandler(Class enumType) {
        if (enumType == null) {
            throw new IllegalArgumentException("Type argument cannot be null");
        }

        this.clazz = enumType;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, StringBaseEnum parameter, JdbcType jdbcType)
            throws SQLException {
        ps.setString(i, parameter.getValue());
    }

    @Override
    public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return StringBaseEnum.fromValue(clazz, rs.getString(columnName));
    }

    @Override
    public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return StringBaseEnum.fromValue(clazz, rs.getString(columnIndex));
    }

    @Override
    public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return StringBaseEnum.fromValue(clazz, cs.getString(columnIndex));
    }
}

mybatis.xml中配置



    
        
    
    
        
        
        
    
    
      
      
      
    

mybatis mapper文件中resultMap定义


        
 

你可能感兴趣的:(SpringBoot和mybatis项目中使用枚举)