2015-02-09 14 views
5

我發現了幾個例子,說明如何連接定製ResultHandler到的MyBatis查詢:如何連接ResultHandler在MyBatis的XML映射

例如https://code.google.com/p/mybatis/wiki/ResultHandlerExample

不幸的是,該示例中給出的ResultHandler永遠不會被調用。 (作爲最後的評論已經指出)

所以我搜索了一個解決方案,並發現這一點:MyBatis - ResultHandler is not invoked

但因爲我使用的MyBatis這不太適合我的問題了XML風格的方式,而不是API風格的方式。所以,在我的情況我沒有

SqlSession session = MyBatisConnectionFactory.getSqlSessionFactory().openSession(true); 

有沒有連接我的自定義處理程序的XML文件的方式,例如<resultMap />奧德<select />節點?

回答

7

您可以在映射器ResultHandler定義方法:

public interface YourMapper { 
    void getObjects(@Param("param1") Object param1, ResultHandler handler); 
} 

然後你可以使用它:

List<Object> getObjects(object param1) { 
    YourResultHandler resultHandler = new YourResultHandler(); 
    yourMapper.getObjects(param1, resultHandler); 
    return resultHandler.getResults(); 
} 
+0

正如我上面寫的我正在尋找如何在使用自定義ResultMapper XML方式,而不是以javacode方式 – 2015-03-06 17:25:00

+2

您問了如何使用ResultHandler - 這個答案是正確的。 – Larry 2016-04-29 21:52:49