如何从executeFunction访问多个返回值?

时间:2017-06-09 19:51:32

标签: java jdbc spring-jdbc

我已经看到了一些从simpleJdbcCall.executeFunction获取单个原始结果的例子。我需要知道如何访问一组结果。 execute会返回Map。您必须向SqlOutParameter提供executeFunction并告诉它要返回的类型。好吧,我将返回映射到MyCustomClass.class,它返回null。我知道该函数将返回3 BIGINT个参数值。

public class MyDao {
    private final JdbcTemplate theTemplate;;

    //@Autowired ctor

    public long createCategory(final CategoryEntity category) {
        final SimpleJdbcCall jdbcCall = getJdbcCall();

        return mapResult(jdbcCall.executeFunction(MyResultEntity.class, fromEntity(category)));
    }

    private long mapResult(MyResultEntity result) {
        return result.getOut_id();//FAIL
    }

    private SqlParameterSource fromEntity(final MyEntity category) {
        Map<String,Object> params = new HashMap<>();

        params.put("in_name", category.getName());
        params.put("in_description", category.getDescription());

        SqlParameterSource result = new MapSqlParameterSource(params);

        return result;
    }

    private SimpleJdbcCall getJdbcCall() {
        //I need the value returned by "out_id"
        SimpleJdbcCall jdbcCall = new SimpleJdbcCall(theTemplate)
                .withSchemaName("mySchema")
                .withFunctionName("my_function")
                .declareParameters(
                        new SqlOutParameter("out_id", Types.BIGINT),
                        new SqlOutParameter("out_2", Types.BIGINT),
                        new SqlOutParameter("out_3", Types.BIGINT)
                );

                return jdbcCall;
    }
}

1 个答案:

答案 0 :(得分:1)

我发现您也可以使用execute来调用函数。然后只需使用Map即可获得结果:

public long createCategory(final MyEntity category) {
    final SimpleJdbcCall jdbcCall = getJdbcCall();

    Map<String, Object> results = jdbcCall.execute(fromEntity(category));
    return (long) results.get("out_id");
}