Home >Java >javaTutorial >How to use BaseTypeHandler custom type converter in Java

How to use BaseTypeHandler custom type converter in Java

WBOY
WBOYforward
2023-04-29 09:46:062119browse

Brief description

The json type was added after mysq5.7. However, during use, if the value in the Json array is less than Integer.MAX_VALUE, it will be converted to the List type during deserialization. Assume If we use the List type, we will encounter a type conversion error exception.

Universal type converter (there are some problems)

This method can return the calss of the object and automatically convert it to the corresponding type. However, when encountering the List type, since the calss can only Obtaining the java.util.List type cannot obtain the type of the object in the List, which will cause the default conversion type of fastJson to be used. That is, when the value in the Json array is less than Integer.MAX_VALUE, it will be converted to the List type during deserialization. . So when you encounter this situation, you can use the following method.

@MappedJdbcTypes(value = JdbcType.VARCHAR)
public class JsonTypeHandler<T extends Object> extends BaseTypeHandler<T> {
    private static ObjectMapper mapper = new ObjectMapper();
    private Class<T> clazz;
    public JsonTypeHandler(Class<T> clazz) {
        if (clazz == null) {
            throw new IllegalArgumentException("Type argument cannot be null");
        }
        this.clazz = clazz;
    }
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, this.toJson(parameter));
    }
    @Override
    public T getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return this.toObject(rs.getString(columnName), clazz);
    }
    @Override
    public T getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return this.toObject(rs.getString(columnIndex), clazz);
    }
    @Override
    public T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return this.toObject(cs.getString(columnIndex), clazz);
    }
    private String toJson(T object) {
        try {
            return mapper.writeValueAsString(object);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    private T toObject(String content, Class<?> clazz) {
        if (content != null && !content.isEmpty()) {
            try {
                return (T) mapper.readValue(content, clazz);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        } else {
            return null;
        }
    }
}

Customized type converter

The specified return value can only be List

@MappedJdbcTypes(value = JdbcType.VARCHAR)
@MappedTypes(List.class)
public class JsonListLongTypeHandler extends BaseTypeHandler<List<Long>> {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, List<Long> parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, JSON.toJSONString(parameter));
    }
    @Override
    public List<Long> getNullableResult(ResultSet rs, String columnName) throws SQLException {
        return this.getLongs(rs.getString(columnName));
    }
    @Override
    public List<Long> getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        return this.getLongs(rs.getString(columnIndex));
    }
    @Override
    public List<Long> getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        return this.getLongs(cs.getString(columnIndex));
    }
    private List<Long> getLongs(String value) {
        if (StringUtils.hasText(value)) {
            try {
                CollectionType type = objectMapper.getTypeFactory().constructCollectionType(ArrayList.class, Long.class);
                Object o1 = objectMapper.readValue(value, type);                List<Long> o = objectMapper.readValue(value, type);
                return o;
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

The above is the detailed content of How to use BaseTypeHandler custom type converter in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete