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

How to use BaseTypeHandler custom type converter in Java

WBOY
Release: 2023-04-29 09:46:06
forward
2097 people have browsed it

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;
        }
    }
}
Copy after login

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;
    }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template