2013-10-14 27 views
4

名單上有在Java中此存儲過程調用:結果集的存儲過程包含LinkedCaseInsensitiveMap <V>

@Autowired 
public ScoreDao(DataSource dataSource) { 
    setDataSource(dataSource); 
    mScoreStoredProcedure = new ScoreStoredProcedure(dataSource); 
} 

public List<Score> loadAllScore(String pUsername, String pUUID, int pLimit) { 
    return mScoreStoredProcedure.execute(pUsername, pUUID, pLimit); 
} 

private class ScoreStoredProcedure extends StoredProcedure { 
    private static final String SPROC_NAME = "loadUserScore"; 

    public ScoreStoredProcedure(DataSource datasource) { 
     super(datasource, SPROC_NAME); 
     declareParameter(new SqlReturnResultSet("#result-set-2", mScoreMapper)); 
     declareParameter(new SqlParameter("vusername", Types.VARCHAR)); 
     declareParameter(new SqlParameter("vuuid", Types.VARCHAR)); 
     declareParameter(new SqlParameter("vlimit", Types.INTEGER)); 
     compile(); 
    } 

    @SuppressWarnings("unchecked") 
    public List<Score> execute(String pUsername, String pUUID, int pLimit){ 
     Map<String,Object> lAllScoreResult = super.execute(pUsername, pUUID, pLimit); 
     List<Score> lAllScore = ((List<Score>)lAllScoreResult.get("#result-set-2")); 
     return lAllScore; 
    } 

} 

,這映射類:

public class ScoreMapper implements RowMapper<Score> { 

private String suffix = ""; 

@Autowired 
private ScoreCreator scoreCreator; 

@Autowired 
private QuestionMapper questionMapper; 

public ScoreMapper(String pSuffix) { 
    suffix = pSuffix; 
} 

public Score mapRow(ResultSet rs, int rowNum) throws SQLException { 
    Score lScore = scoreCreator.createScore(rs.getLong(suffix+"id"), 
      rs.getTimestamp(suffix+"stempel"), rs.getString(suffix+"username"), 
      rs.getInt(suffix+"points"), rs.getInt(suffix+"level"), 
      rs.getString(suffix+"comment"), 
      questionMapper.mapRow(rs, rowNum), rs.getString(suffix+"uuid")); 
    return lScore; 
} 
    } 

我的問題是,結果我StoredProcedure永遠不會投射到List<Score>

相反,它包含一個列表LinkedCaseInsensitiveMap<V>。每個條目都包含來自數據庫的值。

映射器通過Spring正確連線。

簡而言之:我期待結果爲List<Score>。我以爲我用代碼中顯示的方法來處理這個問題。我如何將結果直接投射到我的List<Score>

我跟着這個教程http://www.jtmelton.com/2007/10/30/using-springs-storedprocedure-and-rowmapper-mechanisms/

你能不能幫我看看這個問題?

+0

它看起來像你從來沒有調用過mapRow,你只是將地圖轉換爲列表。 – aglassman

+0

是的Iam也很想知道,但是我按照這個教程http://www.jtmelton.com/2007/10/30/using-springs-storedprocedure-and-rowmapper-mechanisms/ –

+0

好的。好好打擾一下我說的關於調用mapRow的內容,這應該只能在spring api中使用。 – aglassman

回答

3

您可以重命名結果集參數,比方說,「ScoreResultSet」?

所以

declareParameter(new SqlReturnResultSet("ScoreResultSet", mScoreMapper)); 

List<Score> lAllScore = ((List<Score>)lAllScoreResult.get("ScoreResultSet")); 

您當前使用也是名被使用的春天時,它自動生成結果集的名字。

我想你現在可能會返回一個與你聲明的SqlReturnResultSet參數映射的結果集不同的未處理的結果集。

======

編輯:

我剛剛發現你的這個其他問題:Java StoredProcedure with SqlReturnResultSet not working並假設我們談論的是同一個存儲過程:

我懷疑你有兩個選擇存儲過程中,它返回兩個結果集。 您應該聲明兩個(正確命名的)SqlReturnResultSet參數而不是一個。聲明的順序很重要,所以你不能聲明一個聲明,並給它通常分配給第二個聲明的名稱。

+0

您是對的。我的Sp裏有3個選擇。按照正確的順序設置屬性會導致預期的結果。多謝! –

+0

請幫助我幾乎相同的問題http://stackoverflow.com/q/30378061/2323234 – MRK187

0

嘗試鑄造到LinkedList而不是List<Score>

的教程演示如下,是我能想到的唯一的事情,你已經從教程做不同:

List resultList = (LinkedList)results.get(Constants.RESULTSET); 

    //iterate of results list and print 
    for (Iterator it=resultList.iterator(); it.hasNext();) { 
     User user1 = (User)it.next(); 
     System.out.println(user1); 
    } 

    return resultList; 
+0

的默認名稱沒有不適合我的原因。 –

+3

強制轉換爲LinkedList無法提供幫助,因爲LinkedList沒有List接口沒有的其他方法 –