mybatisplus 设置枚举类 SQL state [HY000]; error code [1366]; Incorrect integer value
1、枚举类
@Getter public enum SexEnum { MALE("1", "男"), FEMALE("2", "女"); @EnumValue private String sex; @JsonValue private String sexName; SexEnum(String sex, String sexName) { this.sex = sex; this.sexName = sexName; } }
2、出现异常原因
SQL: INSERT INTO user ( id, name, email, sex, create_by, create_time, update_by, update_time ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ? ) ### Cause: java.sql.SQLException: Incorrect integer value: 'MALE' for column 'sex' at row 1 ; uncategorized SQLException; SQL state [HY000]; error code [1366]; Incorrect integer value: 'MALE' for column 'sex' at row 1; nested exception is java.sql.SQLException: Incorrect integer value: 'MALE' for column 'sex' at row 1
没有配置
type-enums-package
3、上面都配了,但是有时候扫描不到报错了【头大】
报错内容
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'processing_state' from result set.
Cause: java.lang.IllegalArgumentException: No enum constant com.enums.RuleProcessingStateEnum.1
...
Caused by: org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'processing_state' from result set. Cause: java.lang.IllegalArgumentException:
No enum constant com.enums.RuleProcessingStateEnum.1
...
Caused by: java.lang.IllegalArgumentException: No enum constant com.enums.RuleProcessingStateEnum.1
4、自定义类型转换
@MappedJdbcTypes(JdbcType.INTEGER) @MappedTypes(RuleProcessingStateEnum.class) @Component public class RuleStatusEnum2IntegerType extends BaseTypeHandler<RuleProcessingStateEnum> { @Override public void setNonNullParameter(PreparedStatement preparedStatement, int value, RuleProcessingStateEnum ruleProcessingStateEnum, JdbcType jdbcType) throws SQLException { preparedStatement.setInt(value, Integer.parseInt(ruleProcessingStateEnum.getProcessingState())); } @Override public RuleProcessingStateEnum getNullableResult(ResultSet resultSet, String value) throws SQLException { return resultSet.getInt(value) == 1 ? RuleProcessingStateEnum.DRAFT : RuleProcessingStateEnum.RELEASED; } @Override public RuleProcessingStateEnum getNullableResult(ResultSet resultSet, int value) throws SQLException { return resultSet.getInt(value) == 1 ? RuleProcessingStateEnum.DRAFT : RuleProcessingStateEnum.RELEASED; } @Override public RuleProcessingStateEnum getNullableResult(CallableStatement callableStatement, int value) throws SQLException { return callableStatement.getInt(value) == 1 ? RuleProcessingStateEnum.DRAFT : RuleProcessingStateEnum.RELEASED; } }
蓝天和白云是标配。