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 使用时不会报错,但是通过索引处理的值不对,返回的是枚举类下标的值
示列:

数据库:
在这里插入图片描述
枚举类:
John
返回结果:
John
发现返回的结果是枚举类的下标对应的值,所以下面需要重写,如果使用的是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: 配置你刚刚重写的处理器

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cocosum

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值