Skip to content

The configuration field of BaseTypeHandler is always null #1203

@L-YDXL

Description

@L-YDXL

I customize a class JsonTypeHandler, which inherits BaseTypeHandler. In this class I want to use the inside attribute configuration, but this property is null.

MyBatis version

3.4.5

Database vendor and version

Test case or example project

public class JsonTypeHandler<E> extends BaseTypeHandler<E> {

    private Class<E> type;

    private static final String OBJECT_MAPPER_KEY = ObjectMapper.class.getName();


    public JsonTypeHandler(Class<E> type) {
        if (type == null) {
            throw new IllegalArgumentException("Type argument cannot be null");
        }
        this.type = type;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
        try {
            ObjectMapper objectMapper = (ObjectMapper) configuration.getVariables().get(OBJECT_MAPPER_KEY);
            ps.setString(i, objectMapper.writeValueAsString(parameter));
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String s = rs.getString(columnName);
        return s == null ? null : getE(s);
    }

    @Override
    public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String s = rs.getString(columnIndex);
        return s == null ? null : getE(s);
    }

    private E getE(String s) {
        ObjectMapper objectMapper = (ObjectMapper) configuration.getVariables().get(OBJECT_MAPPER_KEY);
        try {
            return objectMapper.readValue(s, type);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String s = cs.getString(columnIndex);
        return s == null ? null : getE(s);
    }
}

Expected result

BaseTypeHandler#configuration is not null

Actual result

BaseTypeHandler#configuration is null

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions