自定义 TypeHandler (oracle)

针对oracle存储过程的使用到的array数组类型或record结构体类型自定义解析器TypeHandler

参考

有些类已经启用,需要寻找替换的

结构体:

@MappedJdbcTypes(JdbcType.STRUCT)
public class RecordTypeHandler extends BaseTypeHandler<Map> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Map parameter, JdbcType jdbcType) throws SQLException {
        OracleConnection connection = ps.getConnection().unwrap(OracleConnection.class);
        String typeName = parameter.remove("typeName").toString();
        StructDescriptor descriptor = StructDescriptor.createDescriptor(typeName, connection);
        STRUCT struct = new STRUCT(descriptor, connection, parameter);
        ps.setObject(i, struct);
    }
    
    ......
    
    @Override
    public Map getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        Object object = cs.getObject(columnIndex);
        return (Map) object;
    }
}

array数组

@MappedJdbcTypes(JdbcType.ARRAY)
public class ArrayTypeHandler extends BaseTypeHandler <Object[]>{
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, Object[] parameter, JdbcType jdbcType) throws SQLException {
        OracleConnection connection = ps.getConnection().unwrap(OracleConnection.class);
        ArrayList<Object> list = new ArrayList<>(Arrays.asList(parameter));
        String typeName = list.remove(0).toString();
        ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor(typeName, connection);
        ARRAY array = new ARRAY(descriptor, connection, list);
        ps.setArray(i, array);
    }
    
......

    @Override
    public Object[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        ResultSet rs = cs.getArray(columnIndex).getResultSet();
        ArrayList<Object> list = new ArrayList<>();
        while (rs.next()){
            list.add(rs.getObject(2));
        }
        return list.toArray();
    }
}

你可能感兴趣的:(Java笔记,oracle,数据库,database)