Mybatis Plus 3.1.0枚举类处理器重写TypeHandler

我的环境

Springboot 2.13 + Mybatis Plus 3.1 + Oracle 11g(驱动版本oracle6)
1、我尝试用 Mybatis Plus 3.1 以上版本 如 3.23.3 时,oracle6 驱动无法适配
2、枚举类处理器
      a) org.apache.ibatis.type.EnumOrdinalTypeHandler 使用时不会报错,但是通过索引处理的值不对,返回的是枚举类下标的值
示列:

数据库:
Mybatis Plus 3.1.0枚举类处理器重写TypeHandler_第1张图片
枚举类:
Mybatis Plus 3.1.0枚举类处理器重写TypeHandler_第2张图片
返回结果:
Mybatis Plus 3.1.0枚举类处理器重写TypeHandler_第3张图片
发现返回的结果是枚举类的下标对应的值,所以下面需要重写,如果使用的是mybatis plus 3.3直接在枚举类的code使用@EnumValue注解

      b) org.apache.ibatis.type.EnumTypeHandler 使用时直接报错

重写处理器
  • [a] 定义一个枚举类的接口类:
/**
 * 定义枚举类接口, 后面需要实现
 **/
public interface EnumCodeService {
     
    int code();
}
  • [b] 枚举类帮组类,通过code返回对应的数据:
// 帮组类
public class EnumCodeUtil {
     
    public static <E extends Enum<E> & EnumCodeService> E manageCode(Class<E> enumClass, int code) {
     
        E[] enumConstants = enumClass.getEnumConstants();
        for (E e : enumConstants) {
     
            if (e.code() == code)
                return e;
        }
        return null;
    }
}
  • [c] 重写BaseTypeHandler处理器:
import com.cocosum.weixin.chat.utils.enums.EnumCodeService;
import com.cocosum.weixin.chat.utils.enums.EnumCodeUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 重写枚举类处理器(统一处理)
 **/
public class EnumTypeHandlerConfig<E extends Enum<E> & EnumCodeService> extends BaseTypeHandler<EnumCodeService> {
     

    private final Class<E> type;
    private final E[] enums;

    public EnumTypeHandlerConfig(Class<E> type) {
     
        if (type == null) {
     
            throw new IllegalArgumentException("Type argument cannot be null");
        } else {
     
            this.type = type;
            this.enums = type.getEnumConstants();
            if (this.enums == null) {
     
                throw new IllegalArgumentException(type.getSimpleName() + " does not represent an enum type.");
            }
        }
    }

    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, EnumCodeService enumCode, JdbcType jdbcType) throws SQLException {
     
        preparedStatement.setInt(i, enumCode.code());
    }

    @Override
    public EnumCodeService getNullableResult(ResultSet resultSet, String s) throws SQLException {
     
        return EnumCodeUtil.manageCode(type, resultSet.getInt(s));
    }

    @Override
    public EnumCodeService getNullableResult(ResultSet resultSet, int i) throws SQLException {
     
        return EnumCodeUtil.manageCode(type, resultSet.getInt(i));
    }

    @Override
    public EnumCodeService getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
     
        return EnumCodeUtil.manageCode(type, callableStatement.getInt(i));
    }
}
测试

创建一个枚举类:


import com.domain.xxxx.xxxx.utils.enums.EnumCodeService;
import lombok.AllArgsConstructor;
import lombok.Getter;

/**
 * 用户状态枚举类
 **/
@Getter
@AllArgsConstructor
public enum UserStatus implements EnumCodeService {
     

    NORMAL(1, "正常"),

    DISABLE(0, "禁用");

    private Integer code;

    private String remark;

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

配置文件:

mybatis-plus:
  # ........省略其它配置...........
  # 枚举类扫描包 要保存到数据的字段加上注解EnumCode,不加注解保存的是枚举值。
  type-enums-package: com.domain.project
  configuration:
    # 自定义枚举类统一处理器
    default-enum-type-handler: com.xxx.framework.config.EnumTypeHandlerConfig

注意 default-enum-type-handler: 配置你刚刚重写的处理器

最后返回结果:正确
返回结果正确

你可能感兴趣的:(Spring,boot,java)