我希望在从数据库反序列化Enum时具有一定的健壮性。基本上,数据库"alpha", "Alpha", "ALPHA"中的条目都应该反序列化为ALPHA枚举。如何用MyBatis 3.4.6来完成这一任务?
Java枚举类:
import com.fasterxml.jackson.annotation.JsonProperty;
public enum Greek {
@JsonProperty("Alpha") ALPHA,
@JsonProperty("Beta") BETA
}MyBatis映射器与服务
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface EZRatingMapper {
List<Greek> getGreeks();
}
public class GreekService {
private final GreekMapper greekMapper;
@Inject
public GreekService(GreekMapper dbMapper){
this.greekMapper = dbMapper;
}
@Transactional
public List<Greek> getGreeks() {
return greekMapper.getGreeks();
}
}MyBatis xml:
从
greek\_table中选择希腊语
对于Jsons的反序列化,我使用Jackson 2.9.1实现了所需的健壮性,例如:
import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper mapper = new ObjectMapper();
mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_ENUMS, true);
Greek greek = mapper.readValue(jsonString, Greek.class);发布于 2019-03-15 10:53:16
修改SQL查询的最简单选项。在查询中将枚举值转换为大写:
<select id="getGreeks" resultType="Greek">
SELECT UPPER(greek) greek FROM `greek_table`
</select>在这种情况下不能直接使用但在其他情况下可以使用的另一个选项是使用自定义类型处理程序。
在当前的mybatis版本(3.5.0)中,不可能为返回的主实体指定类型处理程序,因此对于返回枚举值的查询,不能使用自定义类型处理程序。但是在许多情况下,当枚举是映射器返回的对象的字段时,它就工作了。
您可以创建自己的类型处理程序,如下所示:
public class GreekEnumTypeHandler extends BaseTypeHandler<Greek> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
if (jdbcType == null) {
ps.setString(i, parameter.name());
} else {
ps.setObject(i, parameter.name(), jdbcType.TYPE_CODE);
}
}
@Override
public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
String s = rs.getString(columnName);
return s == null ? null : Greek.valueOf(s.toUpperCase());
}
@Override
public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String s = rs.getString(columnIndex);
return s == null ? null : Greek.valueOf(s.toUpperCase());
}
@Override
public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String s = cs.getString(columnIndex);
return s == null ? null : Greek.valueOf(s.toUpperCase());
}
}然后像这样在地图上使用它:
<resultMap id="entityMap" type="Entity">
<id property="id" column="id"/>
<result property="greek" column="greek" typeHandler="GreekEnumTypeHandler"/>
</resultMap>
<select id="getEntity" resultMap="entityMap">
SELECT id, greek FROM entity
</select>https://stackoverflow.com/questions/55138785
复制相似问题