刘光辉
3 小时以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package jnpf.dmsDatabase;
 
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.*;
 
/** Explicitly selected by version-property result mappings, never a global JSON handler. */
public class DmsJsonNodeTypeHandler extends BaseTypeHandler<JsonNode> {
    private static final ObjectMapper JSON = new ObjectMapper();
    @Override public void setNonNullParameter(PreparedStatement s, int i, JsonNode value, JdbcType type) throws SQLException {
        s.setObject(i, value.toString(), Types.OTHER);
    }
    @Override public JsonNode getNullableResult(ResultSet rs, String column) throws SQLException { return read(rs.getString(column)); }
    @Override public JsonNode getNullableResult(ResultSet rs, int column) throws SQLException { return read(rs.getString(column)); }
    @Override public JsonNode getNullableResult(CallableStatement s, int column) throws SQLException { return read(s.getString(column)); }
    private JsonNode read(String value) throws SQLException {
        if (value == null) return null;
        try { return JSON.readTree(value); }
        catch (java.io.IOException e) { throw new SQLException("Invalid version extended properties", e); }
    }
}