【问题标题】:How to access multiple return values from executeFunction?如何从 executeFunction 访问多个返回值?
【发布时间】:2017-06-09 19:51:31
【问题描述】:

我见过很多从simpleJdbcCall.executeFunction 获取单个原始结果的示例。我需要知道如何访问一组结果。 execute 返回 Map。您必须将SqlOutParameter 提供给executeFunction 并告诉它要返回的类型。好吧,我将返回映射到MyCustomClass.class,它返回null。我知道该函数将返回 3 BIGINT out 参数值。

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;
    }
}

【问题讨论】:

    标签: java jdbc spring-jdbc


    【解决方案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");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 2014-12-29
      • 2012-01-12
      • 1970-01-01
      • 2018-06-25
      相关资源
      最近更新 更多