java 如何在SimpleJdbcCall中从#result-set-1获取值

f45qwnt8  于 2023-05-21  发布在  Java
关注(0)|答案(3)|浏览(206)

在执行SimpleJdbcCall时,我得到两个参数#result-set-1,#update-count-1

MapSqlParameterSource parameterSource = new MapSqlParameterSource();
parameterSource.addValue("name", "something");
Map<String, Object> resultFromProcedure = cstmt.execute(parameterSource);

#result-set-1有变量

[{
 id = 123,
 name = "something",
 accountnumber = 123456,
 balance = 789999
}]

一切都很好直到我试图进入

resultFromProcedure.get("accountnumber")

结果为null。问题是如何访问#result-set-1中的值

bxgwgixi

bxgwgixi1#

如果我理解正确的话; Map<String, Object> resultFromProcedure由两个条目组成,分别具有密钥#result-set-1#update-count-1
#result-set-1的对象是一个有4个成员变量的对象。(如果是String,则需要将Json转换为Java Object(Example))
因此,您对resultFromProcedure.get("accountnumber")的调用试图使用键accountnumber获取对象,但map不包含该键。你需要先获取#result-set-1的对象,例如:

SomeDTO someDTO = resultFromProcedure.get("#result-set-1");

然后你可以打电话

someDTO.getAccountNumber();
kzmpq1sx

kzmpq1sx2#

由于可能有多个结果集,SimpleJdbcCall返回另一个Map中的arraylist中的对象,标记为“#result-set-1”。要访问其中的值,请尝试以下操作:

ArrayList arrayList = new ArrayList();
arrayList = (ArrayList) resultFromProcedure.get("#result-set-1");
Map resultMap = (Map) arrayList.get(0);

System.out.println("Account Number: " + resultMap.get("accountnumber"));
y53ybaqx

y53ybaqx3#

下面是一个针对“#result-set-1”的Rowmapper解决方案

// Parameters
    SqlParameter[] parameters = { new SqlParameter("MYINPUT1", Types.VARCHAR), // Example input parameter
            new SqlParameter("MYINPUT2", Types.VARCHAR), // Example input parameter
            new SqlParameter("INPUT", Types.VARCHAR), // Input Parameters 
            new SqlOutParameter("RC", Types.VARCHAR), // maybe Return Code 
            new SqlOutParameter("OUTPUT", Types.VARCHAR), // Result-Set Mapping - this will solve the LinkedCaseInsensitiveMap
            new SqlReturnResultSet("#result-set-1", (RowMapper<YourPojo>) (rs, rowNum) -> {
                YourPojo example = new YourPojo();
                YourPojo.setWhatEver(rs.getInt("WHATEVER"));
                YourPojo.setWhenEver(rs.getInt("DATE"));
                return example;
            }) };

     SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(jdbcTemplate).withProcedureName("MYSP001").withSchemaName("SYSPROC").declareParameters(parameters);

    // Inputvalues
    Map<String, String> inParamMap = new HashMap<>();
    inParamMap.put("INPUT1", "inputvalueExample");      
    inParamMap.put("INPUT2", "inputvalueExample2");
    SqlParameterSource source = new MapSqlParameterSource(inParamMap);
    Map<String, Object> resultFromSP = simpleJdbcCall.execute(source);
    List<YourPojo> mylist = (List<YourPojo>) resultFromSP.get("#result-set-1");

相关问题