MybatisPlus 通用枚举无法正确取值

正常使用mybatisplus

     <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.0.4</version>
        </dependency>

使用后发现项目中原先对枚举值的转换存在异常:

 

ERROR com.shein.common.handler.SheinExceptionHandler 统一异常处理,接口:..../page,
异常信息:org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'apply_type' from result set.
Cause: java.lang.IllegalArgumentException: No enum constant com......enums.ApplyTypeEnum.2

官网给的解释:

https://github.com/baomidou/mybatis-plus/issues/522

两种解决方案

  1.降级对应的mybatisPlus 版本 由 3.0.4 ->3.0-RC3

  2.若项目中使用了 spring-boot-devtools  移除对 spring-boot-devtools 的依赖

转载于:https://www.cnblogs.com/weixiaotao/p/11528197.html

MyBatis-PlusMyBatis 的一个增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。在使用 MyBatis-Plus 时,可以实现枚举类型与数据库中字段的自动转换,通常通过自定义枚举转换器来实现。 1. 创建枚举类,定义枚举常量以及对应的数据库存储。 2. 实现 `IEnum` 接口(如果使用的 MyBatis-Plus 版本是 3.1.2 之后的版本,则需要实现 `IEnum` 接口的泛型版本 `IEnum<T>`),并重写 `getValue()` 方法返回枚举对应的数据库。 3. 实现 `TypeHandler` 接口,在 `setParameter` 方法中处理如何将枚举类型转换为数据库能够存储的,在 `getResult` 方法中处理如何将数据库转换为枚举类型。 下面是一个简单的示例: ```java public enum SexEnum implements IEnum<Integer> { MAN(1, "男"), WOMAN(2, "女"); private int value; private String desc; SexEnum(int value, String desc) { this.value = value; this.desc = desc; } @Override public Integer getValue() { return value; } // 这里还可以根据需要添加其他方法,比如根据描述获取枚举等。 } ``` 然后注册枚举转换器: ```java @Configuration public class MybatisPlusConfig { @Bean public MybatisPlusInterceptor mybatisPlusInterceptor() { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); interceptor.addInnerInterceptor(new PaginationInnerInterceptor()); // 添加枚举转换器插件 interceptor.addInnerInterceptor(new EnumMateTypeHandler()); return interceptor; } } // 枚举转换器示例 public class EnumMateTypeHandler<E extends IEnum<?>> extends BaseTypeHandler<E> { private Class<E> type; public EnumMateTypeHandler(Class<E> type) { if (type == null) { throw new IllegalArgumentException("Type argument cannot be null"); } this.type = type; } @Override public void setParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException { if (parameter == null) { ps.setNull(i, Types.VARCHAR); } else { ps.setString(i, String.valueOf(parameter.getValue())); } } @Override public E getResult(ResultSet rs, String columnName) throws SQLException { int value = rs.getInt(columnName); return rs.wasNull() ? null : (E) IEnum.valueOf(type, value); } @Override public E getResult(ResultSet rs, int columnIndex) throws SQLException { int value = rs.getInt(columnIndex); return rs.wasNull() ? null : (E) IEnum.valueOf(type, value); } @Override public E getResult(CallableStatement cs, int columnIndex) throws SQLException { int value = cs.getInt(columnIndex); return cs.wasNull() ? null : (E) IEnum.valueOf(type, value); } } ``` 在实际使用中,可能还需要根据具体的数据库连接和版本进行一些微调。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值