枚举基类
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 <E extends Enum<?> & ByteBaseEnum> E fromValue(Class<E> enumClass, Byte value) {
E[] enumConstants = enumClass.getEnumConstants();
for (E e : enumConstants) {
if (e.getValue().equals(value)) {
return e;
}
}
return null;
}
static <E extends Enum<?> & ByteBaseEnum> E fromJsonNode(Class<E> 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 <E extends Enum<?> & IntBaseEnum> E fromValue(Class<E> enumClass, Integer value) {
E[] enumConstants = enumClass.getEnumConstants();
for (E e : enumConstants) {
if (e.getValue().equals(value)) {
return e;
}
}
return null;
}
static <E extends Enum<?> & IntBaseEnum> E fromJsonNode(Class<E> 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 <E extends Enum<?> & StringBaseEnum> E fromValue(Class<E> enumClass, String value) {
E[] enumConstants = enumClass.getEnumConstants();
for (E e : enumConstants) {
if (e.getValue().equals(value)) {
return e;
}
}
return null;
}
static <E extends Enum<?> & StringBaseEnum> E fromJsonNode(Class<E> 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<E extends Enum<?> & ByteBaseEnum> extends BaseTypeHandler<ByteBaseEnum> {
private Class<E> clazz;
public ByteEnumTypeHandler(Class<E> 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<E extends Enum<?> & IntBaseEnum> extends BaseTypeHandler<IntBaseEnum> {
private Class<E> clazz;
public IntEnumTypeHandler(Class<E> 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<E extends Enum<?> & StringBaseEnum> extends BaseTypeHandler<StringBaseEnum> {
private Class<E> clazz;
public StringEnumTypeHandler(Class<E> 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中配置
<!DOCTYPE configuration
PUBLIC "-//config.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<package name="com.yzw.mohurd.ow.entity"/>
<package name="com.yzw.mohurd.ow.enums"/>
<package name="com.yzw.mohurd.ow.dto"/>
</typeAliases>
<typeHandlers>
<typeHandler handler="com.xxx.ow.enums.typehandler.ByteEnumTypeHandler"
javaType="com.xxx.ow.enums.MenuTypeEnum"/>
<typeHandler handler="com.xxx.ow.enums.typehandler.IntEnumTypeHandler"
javaType="com.xxx.ow.enums.CommonStatusEnum"/>
<typeHandler handler="com.xxx.ow.enums.typehandler.StringEnumTypeHandler"
javaType="com.xxx.ow.enums.FeeTypeEnum"/>
</typeHandlers>
</configuration>
mybatis mapper文件中resultMap定义
<resultMap id="ArticleEsQueryResultMap" type="com.xxx.ow.dto.article.ArticleEsQueryResult">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="MenuType"
property="menuType"
jdbcType="TINYINT"
javaType="com.xxx.ow.enums.MenuTypeEnum"
<result column="CommonStatus"
property="commonStatus"
jdbcType="INTEGER"
javaType="com.xxx.ow.enums.CommonStatusEnum"
<result column="FeeType"
property="feeType"
jdbcType="VARCHAR"
javaType="com.xxx.ow.enums.FeeTypeEnum"
</resultMap>
网友评论