spring boot mybatis 枚举错误
mybatis-plus:
#枚举
typeHandlersPackage: com.xxx.common.module.handler
查询代码,不能转换
public LsAppEntity queryByAppKey(String appKey) {
return this.getOne(new QueryWrapper<LsAppEntity>()
.eq("app_key", appKey)
.ne("status", StatusEnum.删除)); }
/**
* Enum - 状态
*
* @author xxx
* @version 1.0.0
*/
@JSONType(serializeEnumAsJavaBean = true)
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
public enum StatusEnum implements BaseEnum<StatusEnum, Integer> {
/** 删除 */
删除(-1, "删除"),
/** 无效 */
无效(0, "无效"),
/** 有效 */
有效(1, "有效");
/**
* 值.
*/
private Integer value;
/**
* 名称.
*/
private String name ;
static {
subClass.add(StatusEnum.class);
}
StatusEnum(Integer value, String name) {
this.value = value;
this.name = name;
}
@Override
public Integer getValue() {
return value;
}
public void setValue(Integer value) {
this.value = value;
}
@Override
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//根据key获取枚举
public static StatusEnum getEnumByValue(Integer value){
if (null == value){
return null;
}
for(StatusEnum e: StatusEnum.values()){
if(e.getValue().equals(value)){
return e;
}
}
return null;
}
}
import com.xxx.common.module.enums.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
@version 1.0.0
*/
@Slf4j
@MappedTypes(value = {StatusEnum.class})
public final class BaseEnumTypeHandler extends BaseTypeHandler {
private Class type;
private E[] enums;
public BaseEnumTypeHandler() {
}
public BaseEnumTypeHandler(Class type) {
if (type == null) {
throw new IllegalArgumentException("Type argument cannot be null");
}
this.type = type;
this.enums = this.type.getEnumConstants();
if (this.enums == null) {
throw new IllegalArgumentException(type.getSimpleName() + " does not represent an enum type.");
}
}
@Override
public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
//BaseTypeHandler 进行非空校验
log.debug("index : {}, parameter : {},jdbcType : {} ", i, parameter.getValue(), jdbcType);
if (jdbcType == null) {
ps.setObject(i, parameter.getValue());
} else {
ps.setObject(i, parameter.getValue(), jdbcType.TYPE_CODE);
}
}
@Override
public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
Object code = rs.getObject(columnName);
if (rs.wasNull()) {
return null;
}
return getEnmByCode(code);
}
@Override
public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Object code = rs.getObject(columnIndex);
if (rs.wasNull()) {
return null;
}
return getEnmByCode(code);
}
@Override
public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Object code = cs.getObject(columnIndex);
if (cs.wasNull()) {
return null;
}
return getEnmByCode(code);
}
private E getEnmByCode(Object code) {
if (code == null) {
throw new NullPointerException("the result code is null " + code);
}
if (code instanceof Integer) {
for (E e : enums) {
if (e.getValue() == code) {
return e;
}
}
throw new IllegalArgumentException("Unknown enumeration type , please check the enumeration code : " + code);
}
if (code instanceof String) {
for (E e : enums) {
if (code.equals(e.getValue())) {
return e;
}
}
throw new IllegalArgumentException("Unknown enumeration type , please check the enumeration code : " + code);
}
throw new IllegalArgumentException("Unknown enumeration type , please check the enumeration code : " + code);
}
}
2020-01-20 02:45:35.879 ERROR 2472 --- [nio-8081-exec-1] c.l.exception.RRExceptionHandler : nested exception is org.apache.ibatis.executor.result.ResultMapException: Error attempting to get column 'status' from result set. Cause: java.lang.IllegalArgumentException: No enum constant com.xxx.common.module.enums.StatusEnum.1
看一下jar包中的枚举类是否有存在,若是没有,就是打包有问题,不是最新的编译打包